Skip to content

Commit e983a37

Browse files
committed
Typed is not considered via setTyped() method in ClassPropertyBuilder - #71
1 parent 69983b0 commit e983a37

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ PHP code generation based on AST.
55
## Installation
66

77
```bash
8-
$ composer require open-code-modeling/php-code-ast --dev
8+
$ composer require open-code-modeling/php-code-ast
99
```
1010

1111
## Usage
1212

1313
> See unit tests in `tests` folder for comprehensive examples.
1414
15-
Let's start with a straightforward example of generating a class with the `ClassFactory`:
15+
Let's start with a straightforward example of generating a class with the `ClassBuilder`:
1616

1717
```php
1818
<?php
@@ -22,16 +22,16 @@ $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);
2222

2323
$ast = $parser->parse('');
2424

25-
$classFactory = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
26-
$classFactory
25+
$classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
26+
$classBuilder
2727
->setFinal(true)
2828
->setExtends('BaseClass')
2929
->setNamespaceImports('Foo\\Bar')
3030
->setImplements('\\Iterator', 'Bar');
3131

3232
$nodeTraverser = new PhpParser\NodeTraverser();
3333

34-
$classFactory->injectVisitors($nodeTraverser, $parser);
34+
$classBuilder->injectVisitors($nodeTraverser, $parser);
3535

3636
print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
3737
```
@@ -93,12 +93,15 @@ Now, change the body of the `toInt()` method to something else. You will see tha
9393

9494
### Reverse usage
9595

96-
It is also possible to create a factory class from parsed PHP AST. You can create an instance of `OpenCodeModeling\CodeAst\Factory\ClassFactory` by
97-
calling `OpenCodeModeling\CodeAst\Factory\ClassFactory::fromNodes()`.
96+
It is also possible to create a factory class from parsed PHP AST. You can create an instance of
97+
`OpenCodeModeling\CodeAst\Builder\ClassBuilder` by calling `OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes()`.
9898

9999
```php
100100
<?php
101-
$expected = <<<'EOF'
101+
$parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7);
102+
$printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);
103+
104+
$expected = <<<'EOF'
102105
<?php
103106

104107
declare (strict_types=1);
@@ -114,12 +117,11 @@ EOF;
114117

115118
$ast = $parser->parse($expected);
116119

117-
$classFactory = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast);
118-
119-
$classFactory->getName(); // TestClass
120-
$classFactory->getExtends(); // BaseClass
121-
$classFactory->isFinal(); // true
122-
$classFactory->isStrict(); // true
123-
$classFactory->isAbstract(); // false
120+
$classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast);
124121

122+
$classBuilder->getName(); // TestClass
123+
$classBuilder->getExtends(); // BaseClass
124+
$classBuilder->isFinal(); // true
125+
$classBuilder->isStrict(); // true
126+
$classBuilder->isAbstract(); // false
125127
```

src/Builder/ClassPropertyBuilder.php

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ public function setTyped(bool $typed): self
148148
{
149149
$this->typed = $typed;
150150

151+
$this->propertyGenerator->setTyped($typed);
152+
151153
return $this;
152154
}
153155

src/Code/PropertyGenerator.php

+12
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public function setTypeDocBlockHint(?string $typeDocBlockHint): self
147147
return $this;
148148
}
149149

150+
public function setTyped(bool $typed): self
151+
{
152+
$this->typed = $typed;
153+
154+
return $this;
155+
}
156+
157+
public function isTyped(): bool
158+
{
159+
return $this->typed;
160+
}
161+
150162
public function generate(): Property
151163
{
152164
return new Property(

tests/Code/PropertyGeneratorTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function setUp(): void
4141
*/
4242
public function it_generates_property_with_doc_block(): void
4343
{
44-
$property = new PropertyGenerator('sourceFolder', 'string', false);
44+
$property = new PropertyGenerator('sourceFolder', 'string');
45+
$property->setTyped(false);
4546
$property->setDocBlockComment('source folder');
4647

4748
$expectedOutput = <<<'EOF'
@@ -63,7 +64,8 @@ public function it_generates_property_with_doc_block(): void
6364
*/
6465
public function it_generates_property_with_overridden_doc_block(): void
6566
{
66-
$property = new PropertyGenerator('sourceFolder', 'string', false);
67+
$property = new PropertyGenerator('sourceFolder', 'string', true);
68+
$property->setTyped(false);
6769
$property->setDocBlockComment('source folder');
6870
$property->overrideDocBlock(new DocBlock('Awesome'));
6971

0 commit comments

Comments
 (0)