9
9
10
10
namespace olvlvl \ComposerAttributeCollector ;
11
11
12
+ use RuntimeException ;
13
+ use Throwable ;
14
+
12
15
use function array_map ;
13
16
14
17
/**
@@ -40,11 +43,32 @@ public function __construct(
40
43
public function findTargetClasses (string $ attribute ): array
41
44
{
42
45
return array_map (
43
- fn (array $ a ) => new TargetClass (new $ attribute( ...$ a[ 0 ] ), $ a [1 ]),
46
+ fn (array $ a ) => new TargetClass (self :: createClassAttribute ( $ attribute, ...$ a ), $ a [1 ]),
44
47
$ this ->targetClasses [$ attribute ] ?? []
45
48
);
46
49
}
47
50
51
+ /**
52
+ * @template T of object
53
+ *
54
+ * @param class-string<T> $attribute
55
+ * @param array<int|string, mixed> $arguments
56
+ * @param class-string $class
57
+ *
58
+ * @return T
59
+ */
60
+ private static function createClassAttribute (string $ attribute , array $ arguments , string $ class ): object
61
+ {
62
+ try {
63
+ return new $ attribute (...$ arguments );
64
+ } catch (Throwable $ e ) {
65
+ throw new RuntimeException (
66
+ "An error occurred while instantiating attribute $ attribute on class $ class " ,
67
+ previous: $ e
68
+ );
69
+ }
70
+ }
71
+
48
72
/**
49
73
* @template T of object
50
74
*
@@ -55,11 +79,36 @@ public function findTargetClasses(string $attribute): array
55
79
public function findTargetMethods (string $ attribute ): array
56
80
{
57
81
return array_map (
58
- fn (array $ a ) => new TargetMethod (new $ attribute( ...$ a[ 0 ] ), $ a [1 ], $ a [2 ]),
82
+ fn (array $ a ) => new TargetMethod (self :: createMethodAttribute ( $ attribute, ...$ a ), $ a [1 ], $ a [2 ]),
59
83
$ this ->targetMethods [$ attribute ] ?? []
60
84
);
61
85
}
62
86
87
+ /**
88
+ * @template T of object
89
+ *
90
+ * @param class-string<T> $attribute
91
+ * @param array<int|string, mixed> $arguments
92
+ * @param class-string $class
93
+ *
94
+ * @return T
95
+ */
96
+ private static function createMethodAttribute (
97
+ string $ attribute ,
98
+ array $ arguments ,
99
+ string $ class ,
100
+ string $ method
101
+ ): object {
102
+ try {
103
+ return new $ attribute (...$ arguments );
104
+ } catch (Throwable $ e ) {
105
+ throw new RuntimeException (
106
+ "An error occurred while instantiating attribute $ attribute on method $ class:: $ method " ,
107
+ previous: $ e
108
+ );
109
+ }
110
+ }
111
+
63
112
/**
64
113
* @param class-string $class
65
114
*
@@ -75,7 +124,7 @@ public function forClass(string $class): ForClass
75
124
continue ;
76
125
}
77
126
78
- $ classAttributes [] = new $ attribute(... $ arguments );
127
+ $ classAttributes [] = self :: createClassAttribute ( $ attribute, $ arguments, $ class );
79
128
}
80
129
}
81
130
@@ -87,7 +136,12 @@ public function forClass(string $class): ForClass
87
136
continue ;
88
137
}
89
138
90
- $ methodAttributes [$ targetMethod ][] = new $ attribute (...$ arguments );
139
+ $ methodAttributes [$ targetMethod ][] = self ::createMethodAttribute (
140
+ $ attribute ,
141
+ $ arguments ,
142
+ $ class ,
143
+ $ targetMethod
144
+ );
91
145
}
92
146
}
93
147
0 commit comments