forked from olvlvl/composer-attribute-collector
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement ParameterAttributeCollector #2
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
69c1a6d
Implement ParameterAttributeCollector
clxmstaab 4599456
Support attributes on parameters of hooked properties
staabm dba0802
remove hooked property support
staabm 7de595d
extract AttributeGroupsReflector
staabm 1ff72d3
extract CachedParser
staabm 8287f03
Implement ParameterAttributeCollector->getParameterAttributes() via A…
staabm b2b53b0
Delete FakeMethod.php
staabm 17733fa
fix build
staabm 51b3b71
cs
staabm 3740689
re-use cached parser
staabm 2c6fcd4
Update ClassAttributeCollector.php
staabm 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 hidden or 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 hidden or 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,51 @@ | ||
<?php | ||
|
||
namespace olvlvl\ComposerAttributeCollector; | ||
|
||
use PhpParser\ConstExprEvaluationException; | ||
use PhpParser\ConstExprEvaluator; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use ReflectionAttribute; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class AttributeGroupsReflector | ||
{ | ||
/** | ||
* @param Node\AttributeGroup[] $attrGroups | ||
* @return ReflectionAttribute<object>[] | ||
*/ | ||
public function attrGroupsToAttributes(array $attrGroups): array | ||
{ | ||
$evaluator = new ConstExprEvaluator(function (Expr $expr) { | ||
if ($expr instanceof Expr\ClassConstFetch && $expr->class instanceof Node\Name && $expr->name instanceof Node\Identifier) { | ||
return constant(sprintf('%s::%s', $expr->class->toString(), $expr->name->toString())); | ||
} | ||
|
||
throw new ConstExprEvaluationException("Expression of type {$expr->getType()} cannot be evaluated"); | ||
}); | ||
|
||
$attributes = []; | ||
foreach ($attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attr) { | ||
$argValues = []; | ||
foreach ($attr->args as $i => $arg) { | ||
if ($arg->name === null) { | ||
$argValues[$i] = $evaluator->evaluateDirectly($arg->value); | ||
continue; | ||
} | ||
|
||
$argValues[$arg->name->toString()] = $evaluator->evaluateDirectly($arg->value); | ||
} | ||
$attributes[] = new FakeAttribute( | ||
$attr->name, | ||
$argValues, | ||
); | ||
} | ||
} | ||
|
||
return $attributes; // @phpstan-ignore return.type | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,45 @@ | ||
<?php | ||
|
||
namespace olvlvl\ComposerAttributeCollector; | ||
|
||
use Composer\IO\IOInterface; | ||
use PhpParser\Node; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitor\NameResolver; | ||
use PhpParser\Parser; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class CachedParser | ||
{ | ||
private Parser $parser; | ||
|
||
/** @var array<string, Node[]> */ | ||
private array $parserCache = []; | ||
|
||
public function __construct(Parser $parser) | ||
{ | ||
$this->parser = $parser; | ||
} | ||
|
||
/** | ||
* @return Node[] | ||
*/ | ||
public function parse(string $file): array | ||
{ | ||
if (isset($this->parserCache[$file])) { | ||
return $this->parserCache[$file]; | ||
} | ||
$contents = file_get_contents($file); | ||
if ($contents === false) { | ||
return []; | ||
} | ||
|
||
$ast = $this->parser->parse($contents); | ||
assert($ast !== null); | ||
$nameTraverser = new NodeTraverser(new NameResolver()); | ||
|
||
return $this->parserCache[$file] = $nameTraverser->traverse($ast); | ||
} | ||
} |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.