From f642f109c0ad8824ba13ff092d0f46350400ecfa Mon Sep 17 00:00:00 2001 From: Open Code Modeling Bot Date: Fri, 12 Feb 2021 14:17:25 +0000 Subject: [PATCH 1/6] Bumps changelog version to 0.12.1 Updates the CHANGELOG.md file to add a changelog entry for a new 0.12.1 version. --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9facd07..b785c83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,28 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 0.12.1 - TBD + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 0.12.0 - 2021-02-12 From 29c0ae816aa40f8e1198ec22484ea232b054b7a3 Mon Sep 17 00:00:00 2001 From: Sandro Keil Date: Mon, 15 Feb 2021 14:29:19 +0100 Subject: [PATCH 2/6] ClassPropertyBuilder with typed=false not working - #69 --- src/Builder/ClassPropertyBuilder.php | 4 +-- tests/Builder/ClassMethodBuilderTest.php | 33 ++++++++++++++++++++++ tests/Builder/ClassPropertyBuilderTest.php | 7 +++-- tests/Code/PropertyGeneratorTest.php | 8 +++--- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Builder/ClassPropertyBuilder.php b/src/Builder/ClassPropertyBuilder.php index 99e2cc0..0cd45a8 100644 --- a/src/Builder/ClassPropertyBuilder.php +++ b/src/Builder/ClassPropertyBuilder.php @@ -80,7 +80,7 @@ public static function fromNode(Node\Stmt\Property $node, bool $typed = true): s $self->type = $type; $self->visibility = $node->flags; $self->typed = $typed; - $self->propertyGenerator = new PropertyGenerator($self->name, $self->type); + $self->propertyGenerator = new PropertyGenerator($self->name, $self->type, $typed); $defaultValue = $node->props[0]->default; @@ -129,7 +129,7 @@ public static function fromScratch(string $name, string $type, bool $typed = tru $self->type = $type; $self->typed = $typed; $self->visibility = ClassConstGenerator::FLAG_PRIVATE; - $self->propertyGenerator = new PropertyGenerator($self->name, $self->type); + $self->propertyGenerator = new PropertyGenerator($self->name, $self->type, $typed); return $self; } diff --git a/tests/Builder/ClassMethodBuilderTest.php b/tests/Builder/ClassMethodBuilderTest.php index 87bec2e..81c4324 100644 --- a/tests/Builder/ClassMethodBuilderTest.php +++ b/tests/Builder/ClassMethodBuilderTest.php @@ -412,4 +412,37 @@ public function setActive() : void $this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($this->parser->parse('')))); } + + /** + * @test + */ + public function it_generates_method_with_args_and_default_value(): void + { + $ast = $this->parser->parse(''); + + $methodBuilder = ClassMethodBuilder::fromScratch('setActive')->setReturnType('void'); + $methodBuilder->setParameters(ParameterBuilder::fromScratch('active', 'bool')->setDefaultValue(null)); + + $classFactory = ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service'); + $classFactory->setMethods($methodBuilder); + + $nodeTraverser = new NodeTraverser(); + $classFactory->injectVisitors($nodeTraverser, $this->parser); + + $expected = <<<'EOF' +assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); + } } diff --git a/tests/Builder/ClassPropertyBuilderTest.php b/tests/Builder/ClassPropertyBuilderTest.php index 5d3720c..15b7a87 100644 --- a/tests/Builder/ClassPropertyBuilderTest.php +++ b/tests/Builder/ClassPropertyBuilderTest.php @@ -45,7 +45,7 @@ public function it_generates_property_for_empty_class(): void $ast = $this->parser->parse(''); $classFactory = ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service'); - $classFactory->setProperties(ClassPropertyBuilder::fromScratch('aggregateId', 'string')); + $classFactory->setProperties(ClassPropertyBuilder::fromScratch('aggregateId', 'string', false)); $this->assertTrue($classFactory->hasProperty('aggregateId')); @@ -60,7 +60,10 @@ public function it_generates_property_for_empty_class(): void class TestClass { - private string $aggregateId; + /** + * @var string + */ + private $aggregateId; } EOF; diff --git a/tests/Code/PropertyGeneratorTest.php b/tests/Code/PropertyGeneratorTest.php index d1b31a3..28fb8e4 100644 --- a/tests/Code/PropertyGeneratorTest.php +++ b/tests/Code/PropertyGeneratorTest.php @@ -41,7 +41,7 @@ public function setUp(): void */ public function it_generates_property_with_doc_block(): void { - $property = new PropertyGenerator('sourceFolder', 'string'); + $property = new PropertyGenerator('sourceFolder', 'string', false); $property->setDocBlockComment('source folder'); $expectedOutput = <<<'EOF' @@ -52,7 +52,7 @@ public function it_generates_property_with_doc_block(): void * * @var string */ -private string $sourceFolder; +private $sourceFolder; EOF; $this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$property->generate()])); @@ -63,7 +63,7 @@ public function it_generates_property_with_doc_block(): void */ public function it_generates_property_with_overridden_doc_block(): void { - $property = new PropertyGenerator('sourceFolder', 'string'); + $property = new PropertyGenerator('sourceFolder', 'string', false); $property->setDocBlockComment('source folder'); $property->overrideDocBlock(new DocBlock('Awesome')); @@ -73,7 +73,7 @@ public function it_generates_property_with_overridden_doc_block(): void /** * Awesome */ -private string $sourceFolder; +private $sourceFolder; EOF; $this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$property->generate()])); From b96f331b4e69ad175e0eb9043ab0970ba3d4665d Mon Sep 17 00:00:00 2001 From: Open Code Modeling Bot Date: Mon, 15 Feb 2021 13:33:36 +0000 Subject: [PATCH 3/6] 0.12.1 readiness Updates the CHANGELOG.md to set the release date. --- CHANGELOG.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b785c83..2b64b4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,27 +2,24 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 0.12.1 - TBD +## 0.12.1 - 2021-02-15 -### Added -- Nothing. +----- -### Changed +### Release Notes for [0.12.1](https://github.com/open-code-modeling/php-code-ast/milestone/27) -- Nothing. +0.12.x bugfix release (patch) -### Deprecated - -- Nothing. +### 0.12.1 -### Removed - -- Nothing. +- Total issues resolved: **1** +- Total pull requests resolved: **0** +- Total contributors: **1** -### Fixed +#### bug -- Nothing. + - [69: ClassPropertyBuilder with typed=false not working](https://github.com/open-code-modeling/php-code-ast/issues/69) thanks to @sandrokeil ## 0.12.0 - 2021-02-12 From 69983b01cceb16b79f575a02f8601d866171b7f1 Mon Sep 17 00:00:00 2001 From: Open Code Modeling Bot Date: Mon, 15 Feb 2021 13:33:53 +0000 Subject: [PATCH 4/6] Bumps changelog version to 0.12.2 Updates the CHANGELOG.md file to add a changelog entry for a new 0.12.2 version. --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b64b4c..db66cc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,28 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 0.12.2 - TBD + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 0.12.1 - 2021-02-15 From e983a37656744796f7668a85e1f68c475a252117 Mon Sep 17 00:00:00 2001 From: Sandro Keil Date: Tue, 16 Feb 2021 21:30:19 +0100 Subject: [PATCH 5/6] Typed is not considered via setTyped() method in ClassPropertyBuilder - #71 --- README.md | 32 +++++++++++++++------------- src/Builder/ClassPropertyBuilder.php | 2 ++ src/Code/PropertyGenerator.php | 12 +++++++++++ tests/Code/PropertyGeneratorTest.php | 6 ++++-- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 4212058..59bf481 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,14 @@ PHP code generation based on AST. ## Installation ```bash -$ composer require open-code-modeling/php-code-ast --dev +$ composer require open-code-modeling/php-code-ast ``` ## Usage > See unit tests in `tests` folder for comprehensive examples. -Let's start with a straightforward example of generating a class with the `ClassFactory`: +Let's start with a straightforward example of generating a class with the `ClassBuilder`: ```php true]); $ast = $parser->parse(''); -$classFactory = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service'); -$classFactory +$classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service'); +$classBuilder ->setFinal(true) ->setExtends('BaseClass') ->setNamespaceImports('Foo\\Bar') @@ -31,7 +31,7 @@ $classFactory $nodeTraverser = new PhpParser\NodeTraverser(); -$classFactory->injectVisitors($nodeTraverser, $parser); +$classBuilder->injectVisitors($nodeTraverser, $parser); print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast))); ``` @@ -93,12 +93,15 @@ Now, change the body of the `toInt()` method to something else. You will see tha ### Reverse usage -It is also possible to create a factory class from parsed PHP AST. You can create an instance of `OpenCodeModeling\CodeAst\Factory\ClassFactory` by -calling `OpenCodeModeling\CodeAst\Factory\ClassFactory::fromNodes()`. +It is also possible to create a factory class from parsed PHP AST. You can create an instance of +`OpenCodeModeling\CodeAst\Builder\ClassBuilder` by calling `OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes()`. ```php create(PhpParser\ParserFactory::ONLY_PHP7); +$printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]); + +$expected = <<<'EOF' parse($expected); -$classFactory = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast); - -$classFactory->getName(); // TestClass -$classFactory->getExtends(); // BaseClass -$classFactory->isFinal(); // true -$classFactory->isStrict(); // true -$classFactory->isAbstract(); // false +$classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast); +$classBuilder->getName(); // TestClass +$classBuilder->getExtends(); // BaseClass +$classBuilder->isFinal(); // true +$classBuilder->isStrict(); // true +$classBuilder->isAbstract(); // false ``` diff --git a/src/Builder/ClassPropertyBuilder.php b/src/Builder/ClassPropertyBuilder.php index 0cd45a8..6ee4511 100644 --- a/src/Builder/ClassPropertyBuilder.php +++ b/src/Builder/ClassPropertyBuilder.php @@ -148,6 +148,8 @@ public function setTyped(bool $typed): self { $this->typed = $typed; + $this->propertyGenerator->setTyped($typed); + return $this; } diff --git a/src/Code/PropertyGenerator.php b/src/Code/PropertyGenerator.php index a8dd824..ba6a87b 100644 --- a/src/Code/PropertyGenerator.php +++ b/src/Code/PropertyGenerator.php @@ -147,6 +147,18 @@ public function setTypeDocBlockHint(?string $typeDocBlockHint): self return $this; } + public function setTyped(bool $typed): self + { + $this->typed = $typed; + + return $this; + } + + public function isTyped(): bool + { + return $this->typed; + } + public function generate(): Property { return new Property( diff --git a/tests/Code/PropertyGeneratorTest.php b/tests/Code/PropertyGeneratorTest.php index 28fb8e4..6753177 100644 --- a/tests/Code/PropertyGeneratorTest.php +++ b/tests/Code/PropertyGeneratorTest.php @@ -41,7 +41,8 @@ public function setUp(): void */ public function it_generates_property_with_doc_block(): void { - $property = new PropertyGenerator('sourceFolder', 'string', false); + $property = new PropertyGenerator('sourceFolder', 'string'); + $property->setTyped(false); $property->setDocBlockComment('source folder'); $expectedOutput = <<<'EOF' @@ -63,7 +64,8 @@ public function it_generates_property_with_doc_block(): void */ public function it_generates_property_with_overridden_doc_block(): void { - $property = new PropertyGenerator('sourceFolder', 'string', false); + $property = new PropertyGenerator('sourceFolder', 'string', true); + $property->setTyped(false); $property->setDocBlockComment('source folder'); $property->overrideDocBlock(new DocBlock('Awesome')); From 79dd990b8a90568e4d3ac1676d7a0331053d207f Mon Sep 17 00:00:00 2001 From: Open Code Modeling Bot Date: Tue, 16 Feb 2021 20:34:14 +0000 Subject: [PATCH 6/6] 0.12.2 readiness Updates the CHANGELOG.md to set the release date. --- CHANGELOG.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db66cc4..5019e31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,27 +2,24 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 0.12.2 - TBD +## 0.12.2 - 2021-02-16 -### Added - -- Nothing. - -### Changed -- Nothing. +----- -### Deprecated +### Release Notes for [0.12.2](https://github.com/open-code-modeling/php-code-ast/milestone/29) -- Nothing. +0.12.x bugfix release (patch) -### Removed +### 0.12.2 -- Nothing. +- Total issues resolved: **1** +- Total pull requests resolved: **0** +- Total contributors: **1** -### Fixed +#### bug -- Nothing. + - [71: Typed is not considered via setTyped() method in ClassPropertyBuilder](https://github.com/open-code-modeling/php-code-ast/issues/71) thanks to @sandrokeil ## 0.12.1 - 2021-02-15