-
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
Changes from 1 commit
69c1a6d
4599456
dba0802
7de595d
1ff72d3
8287f03
b2b53b0
17733fa
51b3b71
3740689
2c6fcd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace olvlvl\ComposerAttributeCollector; | ||
|
||
|
||
use Composer\IO\IOInterface; | ||
use PhpParser\Parser; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ParameterAttributeCollector | ||
{ | ||
private IOInterface $io; | ||
private Parser $parser; | ||
|
||
public function __construct(IOInterface $io, Parser $parser) | ||
{ | ||
$this->io = $io; | ||
$this->parser = $parser; | ||
} | ||
|
||
/** | ||
* @return array<TransientTargetMethodParameter> | ||
*/ | ||
public function collectAttributes(\ReflectionFunctionAbstract $reflectionFunctionAbstract): array | ||
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. I have build this collector on |
||
{ | ||
$funcParameterAttributes = []; | ||
foreach($reflectionFunctionAbstract->getParameters() as $parameter) { | ||
$attributes = $this->getParameterAttributes($parameter); | ||
$functionName = $reflectionFunctionAbstract->name; | ||
$parameterName = $parameter->name; | ||
assert($functionName !== ''); | ||
assert($parameterName !== ''); | ||
|
||
$paramLabel = ''; | ||
if ($reflectionFunctionAbstract instanceof \ReflectionMethod) { | ||
$paramLabel = $reflectionFunctionAbstract->class .'::'.$functionName .'(' . $parameterName .')'; | ||
Check failure on line 38 in src/ParameterAttributeCollector.php
|
||
} elseif ($reflectionFunctionAbstract instanceof \ReflectionFunction) { | ||
$paramLabel = $functionName . '(' . $parameterName .')'; | ||
} | ||
|
||
foreach($attributes as $attribute) { | ||
$this->io->debug("Found attribute {$attribute->getName()} on $paramLabel"); | ||
|
||
$funcParameterAttributes[] = new TransientTargetMethodParameter( | ||
$attribute->getName(), | ||
$attribute->getArguments(), | ||
$functionName, | ||
$parameterName | ||
); | ||
} | ||
} | ||
|
||
return $funcParameterAttributes; | ||
} | ||
|
||
/** | ||
* @return \ReflectionAttribute<object>[] | ||
*/ | ||
private function getParameterAttributes(\ReflectionParameter $parameterReflection): array | ||
{ | ||
if (PHP_VERSION_ID >= 80000) { | ||
return $parameterReflection->getAttributes(); | ||
} | ||
|
||
// todo: implement PHPParser based inspection | ||
throw new \LogicException(); | ||
staabm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ final class Plugin implements PluginInterface, EventSubscriberInterface | |
{ | ||
public const CACHE_DIR = '.composer-attribute-collector'; | ||
public const VERSION_MAJOR = 2; | ||
public const VERSION_MINOR = 0; | ||
public const VERSION_MINOR = 1; | ||
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. use new version to make sure a fresh
|
||
|
||
/** | ||
* @uses onPostAutoloadDump | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace olvlvl\ComposerAttributeCollector; | ||
|
||
/** | ||
* @readonly | ||
* | ||
* @template T of object | ||
*/ | ||
final class TargetMethodParameter | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @var T | ||
*/ | ||
public object $attribute; | ||
/** | ||
* @var class-string | ||
*/ | ||
public string $class; | ||
/** | ||
* @var non-empty-string | ||
*/ | ||
public string $method; | ||
/** | ||
* @var non-empty-string | ||
*/ | ||
public string $name; | ||
/** | ||
* @param T $attribute | ||
* @param class-string $class | ||
* The name of the target class. | ||
* @param non-empty-string $name | ||
* The name of the target parameter. | ||
* @param non-empty-string $method | ||
* The name of the target method. | ||
*/ | ||
public function __construct(object $attribute, string $class, string $name, string $method) | ||
{ | ||
$this->attribute = $attribute; | ||
$this->class = $class; | ||
$this->name = $name; | ||
$this->method = $method; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.