Skip to content

Commit 29c0ae8

Browse files
committed
ClassPropertyBuilder with typed=false not working - #69
1 parent f642f10 commit 29c0ae8

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

src/Builder/ClassPropertyBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function fromNode(Node\Stmt\Property $node, bool $typed = true): s
8080
$self->type = $type;
8181
$self->visibility = $node->flags;
8282
$self->typed = $typed;
83-
$self->propertyGenerator = new PropertyGenerator($self->name, $self->type);
83+
$self->propertyGenerator = new PropertyGenerator($self->name, $self->type, $typed);
8484

8585
$defaultValue = $node->props[0]->default;
8686

@@ -129,7 +129,7 @@ public static function fromScratch(string $name, string $type, bool $typed = tru
129129
$self->type = $type;
130130
$self->typed = $typed;
131131
$self->visibility = ClassConstGenerator::FLAG_PRIVATE;
132-
$self->propertyGenerator = new PropertyGenerator($self->name, $self->type);
132+
$self->propertyGenerator = new PropertyGenerator($self->name, $self->type, $typed);
133133

134134
return $self;
135135
}

tests/Builder/ClassMethodBuilderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,37 @@ public function setActive() : void
412412

413413
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($this->parser->parse(''))));
414414
}
415+
416+
/**
417+
* @test
418+
*/
419+
public function it_generates_method_with_args_and_default_value(): void
420+
{
421+
$ast = $this->parser->parse('');
422+
423+
$methodBuilder = ClassMethodBuilder::fromScratch('setActive')->setReturnType('void');
424+
$methodBuilder->setParameters(ParameterBuilder::fromScratch('active', 'bool')->setDefaultValue(null));
425+
426+
$classFactory = ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
427+
$classFactory->setMethods($methodBuilder);
428+
429+
$nodeTraverser = new NodeTraverser();
430+
$classFactory->injectVisitors($nodeTraverser, $this->parser);
431+
432+
$expected = <<<'EOF'
433+
<?php
434+
435+
declare (strict_types=1);
436+
namespace My\Awesome\Service;
437+
438+
class TestClass
439+
{
440+
public function setActive(bool $active = null) : void
441+
{
442+
}
443+
}
444+
EOF;
445+
446+
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
447+
}
415448
}

tests/Builder/ClassPropertyBuilderTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function it_generates_property_for_empty_class(): void
4545
$ast = $this->parser->parse('');
4646

4747
$classFactory = ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
48-
$classFactory->setProperties(ClassPropertyBuilder::fromScratch('aggregateId', 'string'));
48+
$classFactory->setProperties(ClassPropertyBuilder::fromScratch('aggregateId', 'string', false));
4949

5050
$this->assertTrue($classFactory->hasProperty('aggregateId'));
5151

@@ -60,7 +60,10 @@ public function it_generates_property_for_empty_class(): void
6060
6161
class TestClass
6262
{
63-
private string $aggregateId;
63+
/**
64+
* @var string
65+
*/
66+
private $aggregateId;
6467
}
6568
EOF;
6669

tests/Code/PropertyGeneratorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function setUp(): void
4141
*/
4242
public function it_generates_property_with_doc_block(): void
4343
{
44-
$property = new PropertyGenerator('sourceFolder', 'string');
44+
$property = new PropertyGenerator('sourceFolder', 'string', false);
4545
$property->setDocBlockComment('source folder');
4646

4747
$expectedOutput = <<<'EOF'
@@ -52,7 +52,7 @@ public function it_generates_property_with_doc_block(): void
5252
*
5353
* @var string
5454
*/
55-
private string $sourceFolder;
55+
private $sourceFolder;
5656
EOF;
5757

5858
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$property->generate()]));
@@ -63,7 +63,7 @@ public function it_generates_property_with_doc_block(): void
6363
*/
6464
public function it_generates_property_with_overridden_doc_block(): void
6565
{
66-
$property = new PropertyGenerator('sourceFolder', 'string');
66+
$property = new PropertyGenerator('sourceFolder', 'string', false);
6767
$property->setDocBlockComment('source folder');
6868
$property->overrideDocBlock(new DocBlock('Awesome'));
6969

@@ -73,7 +73,7 @@ public function it_generates_property_with_overridden_doc_block(): void
7373
/**
7474
* Awesome
7575
*/
76-
private string $sourceFolder;
76+
private $sourceFolder;
7777
EOF;
7878

7979
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$property->generate()]));

0 commit comments

Comments
 (0)