Skip to content

Commit d0a8cc6

Browse files
committed
Use DocBlock classes and allow override of doc block
1 parent aae15e8 commit d0a8cc6

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

src/Code/DocBlock/DocBlock.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public function generate(): string
6363
$comment .= ' * ' . $tag->generate() . "\n";
6464
}
6565

66-
return \preg_replace("/ \* \n/", " *\n", $comment) . "\n */";
66+
$comment = \preg_replace("/ \* \n/", " *\n", $comment);
67+
68+
if (\count($this->tags) === 0) {
69+
return \trim($comment) . '/';
70+
}
71+
72+
return $comment . "\n */";
6773
}
6874
}

src/Code/PropertyGenerator.php

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace OpenCodeModeling\CodeAst\Code;
1212

13+
use OpenCodeModeling\CodeAst\Code\DocBlock\DocBlock;
14+
use OpenCodeModeling\CodeAst\Code\DocBlock\Tag\VarTag;
1315
use PhpParser\Comment\Doc;
1416
use PhpParser\Node;
1517
use PhpParser\Node\Stmt\Property;
@@ -49,6 +51,11 @@ final class PropertyGenerator extends AbstractMemberGenerator
4951
*/
5052
private $typeDocBlockHint;
5153

54+
/**
55+
* @var DocBlock|null
56+
*/
57+
private $docBlock;
58+
5259
public function __construct(
5360
string $name = null,
5461
string $type = null,
@@ -87,7 +94,7 @@ public function getType(): ?TypeGenerator
8794
return $this->type;
8895
}
8996

90-
public function docBlockComment(): ?string
97+
public function getDocBlockComment(): ?string
9198
{
9299
return $this->docBlockComment;
93100
}
@@ -97,6 +104,16 @@ public function setDocBlockComment(?string $docBlockComment): void
97104
$this->docBlockComment = $docBlockComment;
98105
}
99106

107+
/**
108+
* Ignores generation of the doc block and uses provided doc block instead.
109+
*
110+
* @param DocBlock $docBlock
111+
*/
112+
public function overrideDocBlock(DocBlock $docBlock): void
113+
{
114+
$this->docBlock = $docBlock;
115+
}
116+
100117
/**
101118
* @param ValueGenerator|mixed $defaultValue
102119
* @param string $defaultValueType
@@ -142,37 +159,6 @@ public function setTypeDocBlockHint(string $typeDocBlockHint): void
142159

143160
public function generate(): Property
144161
{
145-
$docBlockType = $this->type->isNullable()
146-
? $this->type->type() . '|null'
147-
: $this->type->type();
148-
149-
if ($typeHint = $this->getTypeDocBlockHint()) {
150-
$docBlockType = $typeHint;
151-
}
152-
153-
$propComment = <<<EOF
154-
/**
155-
* @var {$docBlockType}
156-
*/
157-
EOF;
158-
if ($this->docBlockComment) {
159-
$multiLineDocBlockComment = \trim(\preg_replace("/\n/", "\n * ", $this->docBlockComment));
160-
161-
$propComment = <<<EOF
162-
/**
163-
* {$multiLineDocBlockComment}
164-
*
165-
* @var {$docBlockType}
166-
*/
167-
EOF;
168-
}
169-
170-
$attributes = [];
171-
172-
if ($this->typed === false || $this->docBlockComment) {
173-
$attributes = ['comments' => [new Doc($propComment)]];
174-
}
175-
176162
return new Property(
177163
$this->flags,
178164
[
@@ -181,8 +167,30 @@ public function generate(): Property
181167
$this->defaultValue ? $this->defaultValue->generate() : null
182168
),
183169
],
184-
$attributes,
170+
$this->generateAttributes(),
185171
$this->typed ? $this->type->generate() : null
186172
);
187173
}
174+
175+
private function generateAttributes(): array
176+
{
177+
$attributes = [];
178+
179+
if ($this->docBlock) {
180+
return ['comments' => [new Doc($this->docBlock->generate())]];
181+
}
182+
183+
if ($this->typed === false || $this->docBlockComment) {
184+
$docBlockType = new VarTag($this->type->types());
185+
186+
if ($typeHint = $this->getTypeDocBlockHint()) {
187+
$docBlockType->setTypes($typeHint);
188+
}
189+
$docBlock = new DocBlock($this->docBlockComment, $docBlockType);
190+
191+
$attributes = ['comments' => [new Doc($docBlock->generate())]];
192+
}
193+
194+
return $attributes;
195+
}
188196
}

tests/Code/PropertyGeneratorTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace OpenCodeModelingTest\CodeAst\Code;
66

7+
use OpenCodeModeling\CodeAst\Code\DocBlock\DocBlock;
78
use OpenCodeModeling\CodeAst\Code\PropertyGenerator;
89
use PhpParser\Parser;
910
use PhpParser\ParserFactory;
@@ -50,6 +51,27 @@ public function it_generates_property_with_doc_block(): void
5051
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$property->generate()]));
5152
}
5253

54+
/**
55+
* @test
56+
*/
57+
public function it_generates_property_with_overridden_doc_block(): void
58+
{
59+
$property = new PropertyGenerator('sourceFolder', 'string');
60+
$property->setDocBlockComment('source folder');
61+
$property->overrideDocBlock(new DocBlock('Awesome'));
62+
63+
$expectedOutput = <<<'EOF'
64+
<?php
65+
66+
/**
67+
* Awesome
68+
*/
69+
private $sourceFolder;
70+
EOF;
71+
72+
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$property->generate()]));
73+
}
74+
5375
/**
5476
* @test
5577
*/
@@ -97,7 +119,7 @@ public function it_generates_property_with_long_doc_block(): void
97119
* standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a
98120
* type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
99121
* remaining essentially unchanged.
100-
*
122+
*
101123
* It is a long established fact that a reader will be distracted by the readable content of a page when looking at
102124
* its layout.
103125
*

0 commit comments

Comments
 (0)