Skip to content

Merge release 0.12.2 into 0.13.x #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.12.2 - 2021-02-16


-----

### Release Notes for [0.12.2](https://github.com/open-code-modeling/php-code-ast/milestone/29)

0.12.x bugfix release (patch)

### 0.12.2

- Total issues resolved: **1**
- Total pull requests resolved: **0**
- Total contributors: **1**

#### bug

- [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


-----

### Release Notes for [0.12.1](https://github.com/open-code-modeling/php-code-ast/milestone/27)

0.12.x bugfix release (patch)

### 0.12.1

- Total issues resolved: **1**
- Total pull requests resolved: **0**
- Total contributors: **1**

#### bug

- [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


Expand Down
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
Expand All @@ -22,16 +22,16 @@ $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => 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')
->setImplements('\\Iterator', 'Bar');

$nodeTraverser = new PhpParser\NodeTraverser();

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

print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
```
Expand Down Expand Up @@ -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
<?php
$expected = <<<'EOF'
$parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7);
$printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);

$expected = <<<'EOF'
<?php

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

$ast = $parser->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
```
6 changes: 4 additions & 2 deletions src/Builder/ClassPropertyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -148,6 +148,8 @@ public function setTyped(bool $typed): self
{
$this->typed = $typed;

$this->propertyGenerator->setTyped($typed);

return $this;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Code/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
33 changes: 33 additions & 0 deletions tests/Builder/ClassMethodBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php

declare (strict_types=1);
namespace My\Awesome\Service;

class TestClass
{
public function setActive(bool $active = null) : void
{
}
}
EOF;

$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
}
}
7 changes: 5 additions & 2 deletions tests/Builder/ClassPropertyBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand All @@ -60,7 +60,10 @@ public function it_generates_property_for_empty_class(): void

class TestClass
{
private string $aggregateId;
/**
* @var string
*/
private $aggregateId;
}
EOF;

Expand Down
8 changes: 5 additions & 3 deletions tests/Code/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function setUp(): void
public function it_generates_property_with_doc_block(): void
{
$property = new PropertyGenerator('sourceFolder', 'string');
$property->setTyped(false);
$property->setDocBlockComment('source folder');

$expectedOutput = <<<'EOF'
Expand All @@ -52,7 +53,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()]));
Expand All @@ -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');
$property = new PropertyGenerator('sourceFolder', 'string', true);
$property->setTyped(false);
$property->setDocBlockComment('source folder');
$property->overrideDocBlock(new DocBlock('Awesome'));

Expand All @@ -73,7 +75,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()]));
Expand Down