Skip to content

Commit 9ef71ec

Browse files
committed
Remove unnecessary nesting
1 parent cfc8667 commit 9ef71ec

File tree

5 files changed

+41
-54
lines changed

5 files changed

+41
-54
lines changed

src/ClassAttributeCollector.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828
* array<TransientTargetClass>,
2929
* array<TransientTargetMethod>,
3030
* array<TransientTargetProperty>,
31-
* array<array<TransientTargetMethodParameter>>,
31+
* array<TransientTargetMethodParameter>,
3232
* }
3333
*
3434
* @throws ReflectionException
@@ -57,7 +57,9 @@ public function collectAttributes(string $class): array
5757
);
5858
}
5959

60+
/** @var array<TransientTargetMethod> $methodAttributes */
6061
$methodAttributes = [];
62+
/** @var array<TransientTargetMethodParameter> $methodParameterAttributes */
6163
$methodParameterAttributes = [];
6264

6365
foreach ($classReflection->getMethods() as $methodReflection) {
@@ -123,7 +125,7 @@ private static function isAttributeIgnored(ReflectionAttribute $attribute): bool
123125

124126
/**
125127
* @param array<TransientTargetMethod> $methodAttributes
126-
* @param array<array<TransientTargetMethodParameter>> $methodParameterAttributes
128+
* @param array<TransientTargetMethodParameter> $methodParameterAttributes
127129
*
128130
* @return void
129131
*/
@@ -153,7 +155,7 @@ private function collectMethodAndParameterAttributes(
153155
$parameterAttributes = $this->methodParameterCollector->collectAttributes($methodReflection);
154156

155157
if (count($parameterAttributes)) {
156-
$methodParameterAttributes[] = $parameterAttributes;
158+
$methodParameterAttributes = array_merge($methodParameterAttributes, $parameterAttributes);
157159
}
158160
}
159161
}

src/MemoizeAttributeCollector.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ class MemoizeAttributeCollector
2222
* array<TransientTargetClass>,
2323
* array<TransientTargetMethod>,
2424
* array<TransientTargetProperty>,
25-
* array<array<TransientTargetMethodParameter>>,
25+
* array<TransientTargetMethodParameter>,
2626
* }>
2727
* Where _key_ is a class and _value_ is an array where:
2828
* - `0` is a timestamp
29-
* - `1` is an array of class attributes
30-
* - `2` is an array of method attributes
31-
* - `3` is an array of property attributes
32-
* - `4` is an array of arrays. _key_ is a method name and _value_ parameter attributes
3329
*/
3430
private array $state;
3531

src/TransientCollection.php

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

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

@@ -52,7 +52,7 @@ public function addMethodAttributes(string $class, iterable $targets): void
5252

5353
/**
5454
* @param class-string $class
55-
* @param iterable<iterable<TransientTargetMethodParameter>> $targets
55+
* @param iterable<TransientTargetMethodParameter> $targets
5656
* The target class.
5757
*/
5858
public function addMethodParameterAttributes(string $class, iterable $targets): void

src/TransientCollectionRenderer.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace olvlvl\ComposerAttributeCollector;
44

5-
use function is_iterable;
65
use function var_export;
76

87
/**
@@ -35,7 +34,7 @@ public static function render(TransientCollection $collector): string
3534

3635
/**
3736
* //phpcs:disable Generic.Files.LineLength.TooLong
38-
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetProperty|iterable<TransientTargetMethodParameter>>> $targetByClass
37+
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetMethodParameter|TransientTargetProperty>> $targetByClass
3938
*
4039
* @return string
4140
*/
@@ -48,38 +47,30 @@ private static function targetsToCode(iterable $targetByClass): string
4847

4948
/**
5049
* //phpcs:disable Generic.Files.LineLength.TooLong
51-
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetProperty|iterable<TransientTargetMethodParameter>>> $targetByClass
50+
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetMethodParameter|TransientTargetProperty>> $targetByClass
5251
*
53-
* @return array<class-string, array<array{ array<int|string, mixed>, class-string, 2?:non-empty-string }>>
52+
* @return array<class-string, array<array{ array<int|string, mixed>, class-string, 2?:non-empty-string, 3?:non-empty-string }>>
5453
*/
5554
private static function targetsToArray(iterable $targetByClass): array
5655
{
5756
$by = [];
5857

5958
foreach ($targetByClass as $class => $targets) {
60-
foreach ($targets as $target) {
61-
if (!is_iterable($target)) {
62-
$target = [$target];
63-
}
64-
65-
foreach ($target as $t) {
66-
// args in order how the Target* classes expects them in __construct()
67-
$args = [ $t->arguments, $class ];
68-
69-
if (
70-
$t instanceof TransientTargetMethod
71-
|| $t instanceof TransientTargetProperty
72-
|| $t instanceof TransientTargetMethodParameter
73-
) {
74-
$args[] = $t->name;
75-
}
59+
foreach ($targets as $t) {
60+
$a = [ $t->arguments, $class ];
7661

77-
if ($t instanceof TransientTargetMethodParameter) {
78-
$args[] = $t->method;
79-
}
62+
if ($t instanceof TransientTargetMethod
63+
|| $t instanceof TransientTargetProperty
64+
|| $t instanceof TransientTargetMethodParameter
65+
) {
66+
$a[] = $t->name;
67+
}
8068

81-
$by[$t->attribute][] = $args;
69+
if ($t instanceof TransientTargetMethodParameter) {
70+
$a[] = $t->method;
8271
}
72+
73+
$by[$t->attribute][] = $a;
8374
}
8475
}
8576

tests/ClassAttributeCollectorTest.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,24 @@ public static function provideCollectAttributes(): array
107107
],
108108
[],
109109
[
110-
[
111-
new TransientTargetMethodParameter(
112-
'Acme81\Attribute\ParameterA',
113-
["my parameter label"],
114-
'aMethod',
115-
'myParameter'
116-
),
117-
new TransientTargetMethodParameter(
118-
'Acme81\Attribute\ParameterB',
119-
["my 2nd parameter label", "some more data"],
120-
'aMethod',
121-
'anotherParameter'
122-
),
123-
new TransientTargetMethodParameter(
124-
'Acme81\Attribute\ParameterA',
125-
["my yet another parameter label"],
126-
'aMethod',
127-
'yetAnotherParameter'
128-
),
129-
]
110+
new TransientTargetMethodParameter(
111+
'Acme81\Attribute\ParameterA',
112+
["my parameter label"],
113+
'aMethod',
114+
'myParameter'
115+
),
116+
new TransientTargetMethodParameter(
117+
'Acme81\Attribute\ParameterB',
118+
["my 2nd parameter label", "some more data"],
119+
'aMethod',
120+
'anotherParameter'
121+
),
122+
new TransientTargetMethodParameter(
123+
'Acme81\Attribute\ParameterA',
124+
["my yet another parameter label"],
125+
'aMethod',
126+
'yetAnotherParameter'
127+
),
130128
],
131129
]
132130
],

0 commit comments

Comments
 (0)