Skip to content

Commit ebe3dc9

Browse files
committed
Rename MethodParameter as Parameter
to follow PHP's attribute target naming scheme, although function parameters are not supported yet
1 parent 7d8596e commit ebe3dc9

14 files changed

+95
-88
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ _discover_ attribute targets in a codebase—for known targets you can use refle
2222
- Can cache discoveries to speed up consecutive runs.
2323

2424
> [!NOTE]
25-
> Currently, the plugin supports class, method, and property targets.
25+
> Currently, the plugin supports class, method, property, and parameter targets.
2626
> You're welcome to [contribute](CONTRIBUTING.md) if you're interested in expending its support.
2727
2828

@@ -59,8 +59,8 @@ foreach (Attributes::findTargetProperties(Column::class) as $target) {
5959
var_dump($target->attribute, $target->class, $target->name);
6060
}
6161

62-
// Find the target method-parameters of the UserInput attribute.
63-
foreach (Attributes::findTargetMethodParameters(UserInput::class) as $target) {
62+
// Find the target method parameters of the UserInput attribute.
63+
foreach (Attributes::findTargetParameters(UserInput::class) as $target) {
6464
var_dump($target->attribute, $target->class, $target->method, $target->name);
6565
}
6666

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<exclude-pattern>tests/bootstrap.php</exclude-pattern>
1919
</rule>
2020
<rule ref="Generic.Files.LineLength.TooLong">
21+
<exclude-pattern>src/Collection.php</exclude-pattern>
2122
<exclude-pattern>tests/*</exclude-pattern>
2223
</rule>
2324
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">

src/Attributes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public static function findTargetProperties(string $attribute): array
6666
*
6767
* @param class-string<T> $attribute
6868
*
69-
* @return TargetMethodParameter<T>[]
69+
* @return TargetParameter<T>[]
7070
*/
71-
public static function findTargetMethodParameters(string $attribute): array
71+
public static function findTargetParameters(string $attribute): array
7272
{
73-
return self::getCollection()->findTargetMethodParameters($attribute);
73+
return self::getCollection()->findTargetParameters($attribute);
7474
}
7575

7676
/**
@@ -106,11 +106,11 @@ public static function filterTargetProperties(callable $predicate): array
106106
/**
107107
* @param callable(class-string $attribute, class-string $class, string $property, string $method):bool $predicate
108108
*
109-
* @return array<TargetMethodParameter<object>>
109+
* @return array<TargetParameter<object>>
110110
*/
111-
public static function filterTargetMethodParameters(callable $predicate): array
111+
public static function filterTargetParameters(callable $predicate): array
112112
{
113-
return self::getCollection()->filterTargetMethodParameters($predicate);
113+
return self::getCollection()->filterTargetParameters($predicate);
114114
}
115115

116116
/**

src/ClassAttributeCollector.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
*/
1414
class ClassAttributeCollector
1515
{
16-
private ParameterAttributeCollector $methodParameterCollector;
16+
private ParameterAttributeCollector $parameterAttributeCollector;
1717

1818
public function __construct(
1919
private Logger $log,
2020
) {
21-
$this->methodParameterCollector = new ParameterAttributeCollector($this->log);
21+
$this->parameterAttributeCollector = new ParameterAttributeCollector($this->log);
2222
}
2323

2424
/**
@@ -28,7 +28,7 @@ public function __construct(
2828
* array<TransientTargetClass>,
2929
* array<TransientTargetMethod>,
3030
* array<TransientTargetProperty>,
31-
* array<TransientTargetMethodParameter>,
31+
* array<TransientTargetParameter>,
3232
* }
3333
*
3434
* @throws ReflectionException
@@ -59,15 +59,15 @@ public function collectAttributes(string $class): array
5959

6060
/** @var array<TransientTargetMethod> $methodAttributes */
6161
$methodAttributes = [];
62-
/** @var array<TransientTargetMethodParameter> $methodParameterAttributes */
63-
$methodParameterAttributes = [];
62+
/** @var array<TransientTargetParameter> $parameterAttributes */
63+
$parameterAttributes = [];
6464

6565
foreach ($classReflection->getMethods() as $methodReflection) {
6666
$this->collectMethodAndParameterAttributes(
6767
$class,
6868
$methodReflection,
6969
$methodAttributes,
70-
$methodParameterAttributes,
70+
$parameterAttributes,
7171
);
7272
}
7373

@@ -92,7 +92,7 @@ public function collectAttributes(string $class): array
9292
}
9393
}
9494

95-
return [ $classAttributes, $methodAttributes, $propertyAttributes, $methodParameterAttributes ];
95+
return [ $classAttributes, $methodAttributes, $propertyAttributes, $parameterAttributes ];
9696
}
9797

9898
/**
@@ -125,16 +125,15 @@ private static function isAttributeIgnored(ReflectionAttribute $attribute): bool
125125

126126
/**
127127
* @param array<TransientTargetMethod> $methodAttributes
128-
* @param array<TransientTargetMethodParameter> $methodParameterAttributes
128+
* @param array<TransientTargetParameter> $parameterAttributes
129129
*
130130
* @return void
131131
*/
132132
private function collectMethodAndParameterAttributes(
133133
string $class,
134134
\ReflectionMethod $methodReflection,
135135
array &$methodAttributes,
136-
array &$methodParameterAttributes
137-
,
136+
array &$parameterAttributes,
138137
): void {
139138
foreach ($methodReflection->getAttributes() as $attribute) {
140139
if (self::isAttributeIgnored($attribute)) {
@@ -152,10 +151,9 @@ private function collectMethodAndParameterAttributes(
152151
);
153152
}
154153

155-
$parameterAttributes = $this->methodParameterCollector->collectAttributes($methodReflection);
156-
157-
if (count($parameterAttributes)) {
158-
$methodParameterAttributes = array_merge($methodParameterAttributes, $parameterAttributes);
159-
}
154+
$parameterAttributes = array_merge(
155+
$parameterAttributes,
156+
$this->parameterAttributeCollector->collectAttributes($methodReflection),
157+
);
160158
}
161159
}

src/Collection.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ final class Collection
2222
* @param array<class-string, array<array{ mixed[], class-string, non-empty-string }>> $targetProperties
2323
* Where _key_ is an attribute class and _value_ an array of arrays
2424
* where 0 are the attribute arguments, 1 is a target class, and 2 is the target property.
25-
* @param array<class-string, array<array{ mixed[], class-string, non-empty-string, non-empty-string }>> $targetMethodParameters
25+
* @param array<class-string, array<array{ mixed[], class-string, non-empty-string, non-empty-string }>> $targetParameters
2626
* Where _key_ is an attribute class and _value_ an array of arrays where 0 are the
2727
* attribute arguments, 1 is a target class, 2 is the target method, and 3 is the target parameter.
2828
*/
2929
public function __construct(
3030
private array $targetClasses,
3131
private array $targetMethods,
3232
private array $targetProperties,
33-
private array $targetMethodParameters,
33+
private array $targetParameters,
3434
) {
3535
}
3636

@@ -118,13 +118,13 @@ private static function createMethodAttribute(
118118
*
119119
* @param class-string<T> $attribute
120120
*
121-
* @return array<TargetMethodParameter<T>>
121+
* @return array<TargetParameter<T>>
122122
*/
123-
public function findTargetMethodParameters(string $attribute): array
123+
public function findTargetParameters(string $attribute): array
124124
{
125125
return array_map(
126-
fn(array $t) => self::createMethodParameterAttribute($attribute, ...$t),
127-
$this->targetMethodParameters[$attribute] ?? [],
126+
fn(array $t) => self::createParameterAttribute($attribute, ...$t),
127+
$this->targetParameters[$attribute] ?? [],
128128
);
129129
}
130130

@@ -137,9 +137,9 @@ public function findTargetMethodParameters(string $attribute): array
137137
* @param non-empty-string $method
138138
* @param non-empty-string $parameter
139139
*
140-
* @return TargetMethodParameter<T>
140+
* @return TargetParameter<T>
141141
*/
142-
private static function createMethodParameterAttribute(
142+
private static function createParameterAttribute(
143143
string $attribute,
144144
array $arguments,
145145
string $class,
@@ -148,7 +148,7 @@ private static function createMethodParameterAttribute(
148148
): object {
149149
try {
150150
$a = new $attribute(...$arguments);
151-
return new TargetMethodParameter($a, $class, $method, $parameter);
151+
return new TargetParameter($a, $class, $method, $parameter);
152152
} catch (Throwable $e) {
153153
throw new RuntimeException(
154154
"An error occurred while instantiating attribute $attribute on parameter $class::$method($parameter)",
@@ -247,16 +247,16 @@ public function filterTargetMethods(callable $predicate): array
247247
/**
248248
* @param callable(class-string $attribute, class-string $class, non-empty-string $method, non-empty-string $parameter):bool $predicate
249249
*
250-
* @return array<TargetMethodParameter<object>>
250+
* @return array<TargetParameter<object>>
251251
*/
252-
public function filterTargetMethodParameters(callable $predicate): array
252+
public function filterTargetParameters(callable $predicate): array
253253
{
254254
$ar = [];
255255

256-
foreach ($this->targetMethodParameters as $attribute => $references) {
256+
foreach ($this->targetParameters as $attribute => $references) {
257257
foreach ($references as [$arguments, $class, $method, $parameter]) {
258258
if ($predicate($attribute, $class, $method, $parameter)) {
259-
$ar[] = self::createMethodParameterAttribute(
259+
$ar[] = self::createParameterAttribute(
260260
$attribute,
261261
$arguments,
262262
$class,

src/MemoizeAttributeCollector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MemoizeAttributeCollector
2222
* array<TransientTargetClass>,
2323
* array<TransientTargetMethod>,
2424
* array<TransientTargetProperty>,
25-
* array<TransientTargetMethodParameter>,
25+
* array<TransientTargetParameter>,
2626
* }>
2727
* Where _key_ is a class and _value_ is an array where:
2828
* - `0` is a timestamp
@@ -56,7 +56,7 @@ public function collectAttributes(array $classMap): TransientCollection
5656
$classAttributes,
5757
$methodAttributes,
5858
$propertyAttributes,
59-
$methodParameterAttributes,
59+
$parameterAttributes,
6060
] = $this->state[$class] ?? [ 0, [], [], [], [] ];
6161

6262
$mtime = filemtime($filepath);
@@ -74,7 +74,7 @@ public function collectAttributes(array $classMap): TransientCollection
7474
$classAttributes,
7575
$methodAttributes,
7676
$propertyAttributes,
77-
$methodParameterAttributes,
77+
$parameterAttributes,
7878
] = $classAttributeCollector->collectAttributes($class);
7979
} catch (Throwable $e) {
8080
$this->log->error(
@@ -87,7 +87,7 @@ public function collectAttributes(array $classMap): TransientCollection
8787
$classAttributes,
8888
$methodAttributes,
8989
$propertyAttributes,
90-
$methodParameterAttributes,
90+
$parameterAttributes,
9191
];
9292
}
9393

@@ -97,8 +97,8 @@ public function collectAttributes(array $classMap): TransientCollection
9797
if (count($methodAttributes)) {
9898
$collector->addMethodAttributes($class, $methodAttributes);
9999
}
100-
if (count($methodParameterAttributes)) {
101-
$collector->addMethodParameterAttributes($class, $methodParameterAttributes);
100+
if (count($parameterAttributes)) {
101+
$collector->addParameterAttributes($class, $parameterAttributes);
102102
}
103103
if (count($propertyAttributes)) {
104104
$collector->addTargetProperties($class, $propertyAttributes);

src/ParameterAttributeCollector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public function __construct(
1414
}
1515

1616
/**
17-
* @return array<TransientTargetMethodParameter>
17+
* @return array<TransientTargetParameter>
1818
*/
19-
public function collectAttributes(\ReflectionFunctionAbstract $reflectionFunctionAbstract): array // TODO: change to
19+
public function collectAttributes(\ReflectionFunctionAbstract $reflectionFunctionAbstract): array
2020
{
2121
$targets = [];
2222

@@ -36,7 +36,7 @@ public function collectAttributes(\ReflectionFunctionAbstract $reflectionFunctio
3636
foreach ($parameter->getAttributes() as $attribute) {
3737
$this->log->debug("Found attribute {$attribute->getName()} on $paramLabel");
3838

39-
$targets[] = new TransientTargetMethodParameter(
39+
$targets[] = new TransientTargetParameter(
4040
$attribute->getName(),
4141
$attribute->getArguments(),
4242
$functionName,

src/TargetMethodParameter.php renamed to src/TargetParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @template T of object
99
*/
10-
final class TargetMethodParameter
10+
final class TargetParameter
1111
{
1212
/**
1313
* @param T $attribute

src/TransientCollection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ final class TransientCollection
2020
public array $methods = [];
2121

2222
/**
23-
* @var array<class-string, iterable<TransientTargetMethodParameter>>
23+
* @var array<class-string, iterable<TransientTargetParameter>>
2424
*/
25-
public array $methodParameters = [];
25+
public array $parameters = [];
2626

2727
/**
2828
* @var array<class-string, iterable<TransientTargetProperty>>
@@ -52,12 +52,12 @@ public function addMethodAttributes(string $class, iterable $targets): void
5252

5353
/**
5454
* @param class-string $class
55-
* @param iterable<TransientTargetMethodParameter> $targets
55+
* @param iterable<TransientTargetParameter> $targets
5656
* The target class.
5757
*/
58-
public function addMethodParameterAttributes(string $class, iterable $targets): void
58+
public function addParameterAttributes(string $class, iterable $targets): void
5959
{
60-
$this->methodParameters[$class] = $targets;
60+
$this->parameters[$class] = $targets;
6161
}
6262

6363
/**

src/TransientCollectionRenderer.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function render(TransientCollection $collector): string
1616
$targetClassesCode = self::targetsToCode($collector->classes);
1717
$targetMethodsCode = self::targetsToCode($collector->methods);
1818
$targetPropertiesCode = self::targetsToCode($collector->properties);
19-
$targetMethodParametersCode = self::targetsToCode($collector->methodParameters);
19+
$targetParametersCode = self::targetsToCode($collector->parameters);
2020

2121
return <<<PHP
2222
<?php
@@ -27,14 +27,15 @@ public static function render(TransientCollection $collector): string
2727
targetClasses: $targetClassesCode,
2828
targetMethods: $targetMethodsCode,
2929
targetProperties: $targetPropertiesCode,
30-
targetMethodParameters: $targetMethodParametersCode,
30+
targetParameters: $targetParametersCode,
3131
));
3232
PHP;
3333
}
3434

3535
/**
3636
* //phpcs:disable Generic.Files.LineLength.TooLong
37-
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetMethodParameter|TransientTargetProperty>> $targetByClass
37+
*
38+
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetParameter|TransientTargetProperty>> $targetByClass
3839
*
3940
* @return string
4041
*/
@@ -47,9 +48,15 @@ private static function targetsToCode(iterable $targetByClass): string
4748

4849
/**
4950
* //phpcs:disable Generic.Files.LineLength.TooLong
50-
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetMethodParameter|TransientTargetProperty>> $targetByClass
5151
*
52-
* @return array<class-string, array<array{ array<int|string, mixed>, class-string, 2?:non-empty-string, 3?:non-empty-string }>>
52+
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetParameter|TransientTargetProperty>> $targetByClass
53+
*
54+
* @return array<class-string, array<array{
55+
* array<int|string, mixed>,
56+
* class-string,
57+
* 2?:non-empty-string,
58+
* 3?:non-empty-string
59+
* }>>
5360
*/
5461
private static function targetsToArray(iterable $targetByClass): array
5562
{
@@ -59,13 +66,14 @@ private static function targetsToArray(iterable $targetByClass): array
5966
foreach ($targets as $t) {
6067
$a = [ $t->arguments, $class ];
6168

62-
if ($t instanceof TransientTargetMethodParameter) {
69+
if ($t instanceof TransientTargetParameter) {
6370
$a[] = $t->method;
6471
}
6572

66-
if ($t instanceof TransientTargetMethod
73+
if (
74+
$t instanceof TransientTargetMethod
6775
|| $t instanceof TransientTargetProperty
68-
|| $t instanceof TransientTargetMethodParameter
76+
|| $t instanceof TransientTargetParameter
6977
) {
7078
$a[] = $t->name;
7179
}

0 commit comments

Comments
 (0)