-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support PHP 8 attributes and readonly class / property - Close #73
- Loading branch information
1 parent
4a5e7e0
commit 1bec5d8
Showing
30 changed files
with
1,542 additions
and
842 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
namespace OpenCodeModeling\CodeAst\Builder; | ||
|
||
trait AbstractTrait | ||
{ | ||
private bool $abstract = false; | ||
|
||
public function isAbstract(): bool | ||
{ | ||
return $this->abstract; | ||
} | ||
|
||
public function setAbstract(bool $abstract): self | ||
{ | ||
$this->abstract = $abstract; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModeling\CodeAst\Builder; | ||
|
||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use PhpParser\PrettyPrinterAbstract; | ||
|
||
final class AttributeBuilder | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
private array $args; | ||
|
||
private function __construct(string $name, ...$args) | ||
{ | ||
$this->name = $name; | ||
$this->args = $args; | ||
} | ||
|
||
public static function fromScratch(string $name, ...$args): self | ||
{ | ||
return new self($name, ...$args); | ||
} | ||
|
||
public static function fromNode(Attribute $attribute, PrettyPrinterAbstract $printer = null): self | ||
{ | ||
if (null === $printer) { | ||
$printer = new Standard(['shortArraySyntax' => true]); | ||
} | ||
return new self($attribute->name->toString(), ...array_map(static fn(Arg $arg) => $printer->prettyPrint([$arg]), $attribute->args)); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getArgs(): array | ||
{ | ||
return $this->args; | ||
} | ||
|
||
public function setArgs(...$args): void | ||
{ | ||
$this->args = $args; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository | ||
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md | ||
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenCodeModeling\CodeAst\Builder; | ||
|
||
trait AttributeTrait | ||
{ | ||
/** | ||
* @var AttributeBuilder[] | ||
*/ | ||
protected array $attributes = []; | ||
|
||
public function setAttributes(AttributeBuilder ...$attributes): self | ||
{ | ||
$this->attributes = $attributes; | ||
|
||
return $this; | ||
} | ||
|
||
public function addAttribute(AttributeBuilder $attribute): self | ||
{ | ||
$this->attributes[] = $attribute; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return AttributeBuilder[] | ||
*/ | ||
public function getAttributes(): array | ||
{ | ||
return $this->attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.