-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Support for PHP 8.0 class attributes generation #145
Open
jakublabno
wants to merge
4
commits into
laminas:4.8.x
Choose a base branch
from
jakublabno:class-attributes-support
base: 4.8.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
94ab27f
Add attributes generator (for classes only)
jlabno f0c043f
Add attributes generator (for classes only)
jlabno e606216
https://github.com/laminas/laminas-code/pull/145
jakublabno 2e07cf3
Add attributes generator (for classes only)
jakublabno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ jobs: | |
- "highest" | ||
- "locked" | ||
php-version: | ||
- "7.4" | ||
- "8.0" | ||
- "8.1" | ||
operating-system: | ||
|
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator; | ||
|
||
use Laminas\Code\Generator\AttributeGenerator\AttributeAssembler; | ||
use Laminas\Code\Generator\AttributeGenerator\AttributePrototype; | ||
use Laminas\Code\Generator\AttributeGenerator\AttributeWithArgumentsAssembler; | ||
use Laminas\Code\Generator\AttributeGenerator\SimpleAttributeAssembler; | ||
use ReflectionAttribute; | ||
use ReflectionClass; | ||
|
||
final class AttributeGenerator implements GeneratorInterface | ||
{ | ||
private array $assemblers; | ||
|
||
private function __construct(AttributeAssembler ...$assembler) | ||
{ | ||
$this->assemblers = $assembler; | ||
} | ||
|
||
public function generate(): string | ||
{ | ||
$generatedAttributes = array_map(fn(AttributeAssembler $attributeAssembler) => $attributeAssembler->assemble(), | ||
$this->assemblers, | ||
); | ||
|
||
return implode(AbstractGenerator::LINE_FEED, $generatedAttributes); | ||
} | ||
|
||
public static function fromPrototype(AttributePrototype ...$attributePrototype): self | ||
{ | ||
$assemblers = []; | ||
|
||
foreach ($attributePrototype as $prototype) { | ||
$assemblers[] = self::negotiateAssembler($prototype); | ||
} | ||
|
||
return new self(...$assemblers); | ||
} | ||
|
||
public static function fromReflection(ReflectionClass $reflectionClass): self | ||
{ | ||
$attributes = $reflectionClass->getAttributes(); | ||
$assemblers = []; | ||
|
||
foreach ($attributes as $attribute) { | ||
$assembler = self::negotiateAssembler($attribute); | ||
|
||
$assemblers[] = $assembler; | ||
} | ||
|
||
return new self(...$assemblers); | ||
} | ||
|
||
public static function fromArray(array $definitions): self | ||
{ | ||
$assemblers = []; | ||
|
||
foreach ($definitions as $definition) { | ||
@list($attributeName, $attributeArguments) = $definition; | ||
|
||
$prototype = new AttributePrototype($attributeName, $attributeArguments ?? []); | ||
|
||
$assemblers[] = self::negotiateAssembler($prototype); | ||
} | ||
|
||
return new self(...$assemblers); | ||
} | ||
|
||
private static function negotiateAssembler(ReflectionAttribute|AttributePrototype $reflectionPrototype): AttributeAssembler | ||
{ | ||
$hasArguments = !empty($reflectionPrototype->getArguments()); | ||
|
||
if ($hasArguments) { | ||
return new AttributeWithArgumentsAssembler($reflectionPrototype); | ||
} | ||
|
||
return new SimpleAttributeAssembler($reflectionPrototype); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Generator/AttributeGenerator/AbstractAttributeAssembler.php
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator; | ||
|
||
use ReflectionAttribute; | ||
|
||
abstract class AbstractAttributeAssembler implements AttributeAssembler | ||
{ | ||
public function __construct(private ReflectionAttribute $attributePrototype) | ||
{ | ||
} | ||
|
||
final protected function getName(): string | ||
{ | ||
return $this->attributePrototype->getName(); | ||
} | ||
|
||
final protected function getArguments(): array | ||
{ | ||
return $this->attributePrototype->getArguments(); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator; | ||
|
||
use Laminas\Code\Generator\ValueAssembler; | ||
|
||
interface AttributeAssembler extends ValueAssembler | ||
{ | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator; | ||
|
||
//TODO Enum in PHP8.1 | ||
/** | ||
* @internal | ||
*/ | ||
final class AttributePart | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's mark all internally used symbols as |
||
{ | ||
public const T_ATTR_START = '#['; | ||
public const T_ATTR_END = ']'; | ||
|
||
public const T_ATTR_ARGUMENTS_LIST_START = '('; | ||
public const T_ATTR_ARGUMENTS_LIST_END = ')'; | ||
|
||
public const T_ATTR_ARGUMENTS_LIST_ASSIGN_OPERAND = ': '; | ||
public const T_ATTR_ARGUMENTS_LIST_SEPARATOR = ', '; | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator; | ||
|
||
use ReflectionAttribute; | ||
|
||
final class AttributePrototype extends ReflectionAttribute | ||
{ | ||
public function __construct(private string $attributeName, private array $arguments = []) | ||
{ | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->attributeName; | ||
} | ||
|
||
public function getArguments(): array | ||
{ | ||
return $this->arguments; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Generator/AttributeGenerator/AttributeWithArgumentsAssembler.php
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator; | ||
|
||
use Laminas\Code\Generator\AttributeGenerator\Exception\NestedAttributesAreNotSupportedException; | ||
|
||
final class AttributeWithArgumentsAssembler extends AbstractAttributeAssembler | ||
{ | ||
public function assemble(): string | ||
{ | ||
$attributeName = $this->getName(); | ||
|
||
$attributeDefinition = AttributePart::T_ATTR_START . $attributeName . AttributePart::T_ATTR_ARGUMENTS_LIST_START; | ||
|
||
$this->generateArguments($attributeDefinition); | ||
|
||
return $attributeDefinition . AttributePart::T_ATTR_END; | ||
} | ||
|
||
private function generateArguments(string &$output): void | ||
{ | ||
$argumentsList = []; | ||
|
||
foreach ($this->getArguments() as $argumentName => $argumentValue) { | ||
$argumentsList[] = $argumentName . AttributePart::T_ATTR_ARGUMENTS_LIST_ASSIGN_OPERAND . $this->formatArgumentValue($argumentValue); | ||
} | ||
|
||
$output .= implode(AttributePart::T_ATTR_ARGUMENTS_LIST_SEPARATOR, $argumentsList); | ||
$output .= AttributePart::T_ATTR_ARGUMENTS_LIST_END; | ||
} | ||
|
||
private function formatArgumentValue(mixed $argument): mixed | ||
{ | ||
switch (true) { | ||
case is_string($argument): | ||
return "'$argument'"; | ||
case is_bool($argument): | ||
return $argument ? 'true' : 'false'; | ||
case is_array($argument): | ||
throw NestedAttributesAreNotSupportedException::create(); | ||
default: | ||
return $argument; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Generator/AttributeGenerator/Exception/NestedAttributesAreNotSupportedException.php
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator\Exception; | ||
|
||
use Laminas\Code\Generator\Exception\RuntimeException; | ||
|
||
final class NestedAttributesAreNotSupportedException extends RuntimeException | ||
{ | ||
public static function create(): self | ||
{ | ||
return new self('Nested attributes are not supported yet'); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Generator/AttributeGenerator/Exception/NoSuitableArgumentAssemlberFoundException.php
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator\Exception; | ||
|
||
use Laminas\Code\Generator\Exception\RuntimeException; | ||
|
||
class NoSuitableArgumentAssemblerFoundException extends RuntimeException | ||
{ | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Generator/AttributeGenerator/Exception/NotEmptyArgumentListException.php
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator\Exception; | ||
|
||
use Laminas\Code\Generator\Exception\RuntimeException; | ||
|
||
final class NotEmptyArgumentListException extends RuntimeException | ||
{ | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Generator/AttributeGenerator/SimpleAttributeAssembler.php
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Code\Generator\AttributeGenerator; | ||
|
||
use Laminas\Code\Generator\AttributeGenerator\Exception\NotEmptyArgumentListException; | ||
use ReflectionAttribute; | ||
|
||
final class SimpleAttributeAssembler extends AbstractAttributeAssembler | ||
{ | ||
public function __construct(ReflectionAttribute $attributePrototype) | ||
{ | ||
parent::__construct($attributePrototype); | ||
|
||
$this->assertAttributeWithoutArguments(); | ||
} | ||
|
||
public function assemble(): string | ||
{ | ||
$attributeName = $this->getName(); | ||
|
||
return AttributePart::T_ATTR_START . $attributeName . AttributePart::T_ATTR_END; | ||
} | ||
|
||
private function assertAttributeWithoutArguments(): void | ||
{ | ||
$arguments = $this->getArguments(); | ||
|
||
if (!empty($arguments)) { | ||
throw new NotEmptyArgumentListException('Argument list has to be empty'); | ||
} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to drop all
::fromArray()
methods as per #153This API will be dropped once I have the green light on #153