Skip to content

Commit 17eed38

Browse files
committed
Simplify attribute creation functions
1 parent 32f0033 commit 17eed38

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/Collection.php

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,29 @@ public function __construct(
4040
public function findTargetClasses(string $attribute): array
4141
{
4242
return array_map(
43-
fn(array $a) => new TargetClass(self::createClassAttribute($attribute, ...$a), $a[1]),
44-
$this->targetClasses[$attribute] ?? []
43+
fn(array $t) => self::createClassAttribute($attribute, ...$t),
44+
$this->targetClasses[$attribute] ?? [],
4545
);
4646
}
4747

4848
/**
4949
* @template T of object
5050
*
5151
* @param class-string<T> $attribute
52-
* @param array<int|string, mixed> $arguments
52+
* @param array<mixed> $arguments
5353
* @param class-string $class
5454
*
55-
* @return T
55+
* @return TargetClass<T>
5656
*/
5757
private static function createClassAttribute(string $attribute, array $arguments, string $class): object
5858
{
5959
try {
60-
return new $attribute(...$arguments);
60+
$a = new $attribute(...$arguments);
61+
return new TargetClass($a, $class);
6162
} catch (Throwable $e) {
6263
throw new RuntimeException(
6364
"An error occurred while instantiating attribute $attribute on class $class",
64-
previous: $e
65+
previous: $e,
6566
);
6667
}
6768
}
@@ -76,32 +77,34 @@ private static function createClassAttribute(string $attribute, array $arguments
7677
public function findTargetMethods(string $attribute): array
7778
{
7879
return array_map(
79-
fn(array $a) => new TargetMethod(self::createMethodAttribute($attribute, ...$a), $a[1], $a[2]),
80-
$this->targetMethods[$attribute] ?? []
80+
fn(array $t) => self::createMethodAttribute($attribute, ...$t),
81+
$this->targetMethods[$attribute] ?? [],
8182
);
8283
}
8384

8485
/**
8586
* @template T of object
8687
*
8788
* @param class-string<T> $attribute
88-
* @param array<int|string, mixed> $arguments
89+
* @param array<mixed> $arguments
8990
* @param class-string $class
91+
* @param non-empty-string $method
9092
*
91-
* @return T
93+
* @return TargetMethod<T>
9294
*/
9395
private static function createMethodAttribute(
9496
string $attribute,
9597
array $arguments,
9698
string $class,
97-
string $method
99+
string $method,
98100
): object {
99101
try {
100-
return new $attribute(...$arguments);
102+
$a = new $attribute(...$arguments);
103+
return new TargetMethod($a, $class, $method);
101104
} catch (Throwable $e) {
102105
throw new RuntimeException(
103106
"An error occurred while instantiating attribute $attribute on method $class::$method",
104-
previous: $e
107+
previous: $e,
105108
);
106109
}
107110
}
@@ -116,32 +119,34 @@ private static function createMethodAttribute(
116119
public function findTargetProperties(string $attribute): array
117120
{
118121
return array_map(
119-
fn(array $a) => new TargetProperty(self::createPropertyAttribute($attribute, ...$a), $a[1], $a[2]),
120-
$this->targetProperties[$attribute] ?? []
122+
fn(array $t) => self::createPropertyAttribute($attribute, ...$t),
123+
$this->targetProperties[$attribute] ?? [],
121124
);
122125
}
123126

124127
/**
125128
* @template T of object
126129
*
127130
* @param class-string<T> $attribute
128-
* @param array<int|string, mixed> $arguments
131+
* @param array<mixed> $arguments
129132
* @param class-string $class
133+
* @param non-empty-string $property
130134
*
131-
* @return T
135+
* @return TargetProperty<T>
132136
*/
133137
private static function createPropertyAttribute(
134138
string $attribute,
135139
array $arguments,
136140
string $class,
137-
string $property
141+
string $property,
138142
): object {
139143
try {
140-
return new $attribute(...$arguments);
144+
$a = new $attribute(...$arguments);
145+
return new TargetProperty($a, $class, $property);
141146
} catch (Throwable $e) {
142147
throw new RuntimeException(
143148
"An error occurred while instantiating attribute $attribute on property $class::$property",
144-
previous: $e
149+
previous: $e,
145150
);
146151
}
147152
}
@@ -156,9 +161,9 @@ public function filterTargetClasses(callable $predicate): array
156161
$ar = [];
157162

158163
foreach ($this->targetClasses as $attribute => $references) {
159-
foreach ($references as [ $arguments, $class ]) {
164+
foreach ($references as [$arguments, $class]) {
160165
if ($predicate($attribute, $class)) {
161-
$ar[] = new TargetClass(self::createClassAttribute($attribute, $arguments, $class), $class);
166+
$ar[] = self::createClassAttribute($attribute, $arguments, $class);
162167
}
163168
}
164169
}
@@ -176,14 +181,14 @@ public function filterTargetMethods(callable $predicate): array
176181
$ar = [];
177182

178183
foreach ($this->targetMethods as $attribute => $references) {
179-
foreach ($references as [ $arguments, $class, $method ]) {
184+
foreach ($references as [$arguments, $class, $method]) {
180185
if ($predicate($attribute, $class, $method)) {
181-
$ar[] = new TargetMethod(self::createMethodAttribute(
186+
$ar[] = self::createMethodAttribute(
182187
$attribute,
183188
$arguments,
184189
$class,
185-
$method
186-
), $class, $method);
190+
$method,
191+
);
187192
}
188193
}
189194
}
@@ -201,14 +206,14 @@ public function filterTargetProperties(callable $predicate): array
201206
$ar = [];
202207

203208
foreach ($this->targetProperties as $attribute => $references) {
204-
foreach ($references as [ $arguments, $class, $property ]) {
209+
foreach ($references as [$arguments, $class, $property]) {
205210
if ($predicate($attribute, $class, $property)) {
206-
$ar[] = new TargetProperty(self::createPropertyAttribute(
211+
$ar[] = self::createPropertyAttribute(
207212
$attribute,
208213
$arguments,
209214
$class,
210-
$property
211-
), $class, $property);
215+
$property,
216+
);
212217
}
213218
}
214219
}

0 commit comments

Comments
 (0)