-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Step 6 Upgrade] Add PHP 8+ Attributes support (#130)
[Step 6 Upgrade] Add PHP 8+ Attributes support
- Loading branch information
1 parent
b8e46fa
commit bf823ac
Showing
20 changed files
with
553 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"ignore": [ | ||
"src/*", | ||
"tests/*", | ||
"docs/*" | ||
] | ||
|
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,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* Parser Reflection API | ||
* | ||
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> | ||
* | ||
* This source file is subject to the license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Go\ParserReflection; | ||
|
||
use Go\ParserReflection\ValueResolver\NodeExpressionResolver; | ||
use ReflectionAttribute as BaseReflectionAttribute; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\Node\Stmt\PropertyProperty; | ||
|
||
/** | ||
* ref original usage https://3v4l.org/duaQI | ||
*/ | ||
class ReflectionAttribute extends BaseReflectionAttribute | ||
{ | ||
public function __construct( | ||
private string $attributeName, | ||
private ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant|ReflectionFunction|ReflectionParameter $reflector, | ||
private array $arguments, | ||
private bool $isRepeated, | ||
) { | ||
} | ||
|
||
public function getNode(): Node\Attribute | ||
{ | ||
/** @var Class_|ClassMethod|PropertyProperty|ClassConst|Function_|Param $node */ | ||
$node = $this->reflector->getNode(); | ||
|
||
// attrGroups only exists in Property Stmt | ||
if ($node instanceof PropertyProperty) { | ||
$node = $this->reflector->getTypeNode(); | ||
} | ||
|
||
$nodeExpressionResolver = new NodeExpressionResolver($this); | ||
foreach ($node->attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attr) { | ||
if ($attr->name->toString() !== $this->attributeName) { | ||
continue; | ||
} | ||
|
||
$arguments = []; | ||
foreach ($attr->args as $arg) { | ||
$nodeExpressionResolver->process($arg->value); | ||
$arguments[] = $nodeExpressionResolver->getValue(); | ||
} | ||
|
||
if ($arguments !== $this->arguments) { | ||
continue; | ||
} | ||
|
||
return $attr; | ||
} | ||
} | ||
|
||
throw new ReflectionException('ReflectionAttribute should be initiated from Go\ParserReflection Reflection classes'); | ||
} | ||
|
||
public function isRepeated(): bool | ||
{ | ||
return $this->isRepeated; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getArguments(): array | ||
{ | ||
return $this->arguments; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->attributeName; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getTarget(): int | ||
{ | ||
throw new \RuntimeException(sprintf('cannot get target from %s', $this::class)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function newInstance(): object | ||
{ | ||
throw new \RuntimeException(sprintf('cannot create new instance from %s', $this::class)); | ||
} | ||
} |
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
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
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
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* Parser Reflection API | ||
* | ||
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> | ||
* | ||
* This source file is subject to the license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Go\ParserReflection\Traits; | ||
|
||
use Go\ParserReflection\ReflectionAttribute; | ||
use Go\ParserReflection\ReflectionProperty; | ||
use Go\ParserReflection\ValueResolver\NodeExpressionResolver; | ||
|
||
trait AttributeResolverTrait | ||
{ | ||
/** | ||
* @return ReflectionAttribute[] | ||
*/ | ||
public function getAttributes(?string $name = null, int $flags = 0): array | ||
{ | ||
if ($this instanceof ReflectionProperty) { | ||
$node = $this->getTypeNode(); | ||
} else { | ||
$node = $this->getNode(); | ||
} | ||
|
||
$node = $this->getNode(); | ||
$attributes = []; | ||
$nodeExpressionResolver = new NodeExpressionResolver($this); | ||
|
||
foreach ($node->attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attr) { | ||
$arguments = []; | ||
foreach ($attr->args as $arg) { | ||
$nodeExpressionResolver->process($arg->value); | ||
$arguments[] = $nodeExpressionResolver->getValue(); | ||
} | ||
|
||
if ($name === null) { | ||
$attributes[] = new ReflectionAttribute($attr->name->toString(), $this, $arguments, $this->isAttributeRepeated($attr->name->toString(), $node->attrGroups)); | ||
|
||
continue; | ||
} | ||
|
||
if ($name !== $attr->name->toString()) { | ||
continue; | ||
} | ||
|
||
$attributes[] = new ReflectionAttribute($name, $this, $arguments, $this->isAttributeRepeated($name, $node->attrGroups)); | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
private function isAttributeRepeated(string $attributeName, array $attrGroups): bool | ||
{ | ||
$count = 0; | ||
|
||
foreach ($attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attr) { | ||
if ($attr->name->toString() === $attributeName) { | ||
++$count; | ||
} | ||
} | ||
} | ||
|
||
return $count >= 2; | ||
} | ||
} |
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
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.