Skip to content

Commit 346dd45

Browse files
committed
Support method description and param doc block in fromNode() - Close #81
1 parent c7b0067 commit 346dd45

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/Builder/ClassMethodBuilder.php

+24
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,36 @@ public static function fromNode(Node\Stmt\ClassMethod $node, bool $typed = true,
120120
$comments = \explode("\n", $comments[0]->getReformattedText());
121121

122122
foreach ($comments as $comment) {
123+
if ($comment === '/**' || $comment === ' */') {
124+
continue;
125+
}
123126
if (0 === \strpos($comment, ' * @return ')) {
124127
$self->setReturnTypeDocBlockHint(\substr($comment, 11));
128+
continue;
129+
}
130+
if (0 === \strpos($comment, ' * @param ')) {
131+
if ($self->docBlockComment === null) {
132+
$self->setDocBlockComment('');
133+
}
134+
continue;
135+
}
136+
if ($comment === ' *') {
137+
$self->setDocBlockComment($self->getDocBlockComment() . PHP_EOL);
138+
continue;
139+
}
140+
if (0 === \strpos($comment, ' * ')) {
141+
if ($self->docBlockComment === null) {
142+
$self->setDocBlockComment('');
143+
}
144+
$self->setDocBlockComment($self->getDocBlockComment() . \substr($comment, 3) . PHP_EOL);
125145
}
126146
}
127147
}
128148

149+
if ($self->docBlockComment !== null) {
150+
$self->docBlockComment = \trim($self->docBlockComment);
151+
}
152+
129153
return $self;
130154
}
131155

tests/Builder/ClassMethodBuilderTest.php

+62
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,66 @@ public function setActive(bool $active = null) : void
445445

446446
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
447447
}
448+
449+
/**
450+
* @test
451+
*/
452+
public function it_generates_method_with_doc_block_from_builder(): void
453+
{
454+
$ast = $this->parser->parse('');
455+
456+
$docBlockComment = <<<'EOF'
457+
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
458+
standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a
459+
type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
460+
remaining essentially unchanged.
461+
462+
It is a long established fact that a reader will be distracted by the readable content of a page when looking at
463+
its layout.
464+
EOF;
465+
466+
$methodBuilder = ClassMethodBuilder::fromScratch('setActive')->setReturnType('void');
467+
$methodBuilder->setDocBlockComment($docBlockComment);
468+
$methodBuilder->setParameters(ParameterBuilder::fromScratch('active', 'bool')->setDefaultValue(null)->setTypeDocBlockHint('bool'));
469+
470+
$classBuilder = ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
471+
$classBuilder->setMethods($methodBuilder);
472+
473+
$nodeTraverser = new NodeTraverser();
474+
$classBuilder->injectVisitors($nodeTraverser, $this->parser);
475+
476+
$expected = <<<'EOF'
477+
<?php
478+
479+
declare (strict_types=1);
480+
namespace My\Awesome\Service;
481+
482+
class TestClass
483+
{
484+
/**
485+
* Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
486+
* standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a
487+
* type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
488+
* remaining essentially unchanged.
489+
*
490+
* It is a long established fact that a reader will be distracted by the readable content of a page when looking at
491+
* its layout.
492+
*
493+
* @param bool $active
494+
*/
495+
public function setActive(bool $active = null) : void
496+
{
497+
}
498+
}
499+
EOF;
500+
501+
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
502+
503+
$classBuilder = ClassBuilder::fromNodes(...$this->parser->parse($expected));
504+
505+
$nodeTraverser = new NodeTraverser();
506+
$classBuilder->injectVisitors($nodeTraverser, $this->parser);
507+
508+
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse([])));
509+
}
448510
}

0 commit comments

Comments
 (0)