2222use Kaspi \DiContainer \Traits \ParameterTypeByReflectionTrait ;
2323use ReflectionFunctionAbstract ;
2424use ReflectionParameter ;
25+ use Throwable ;
2526
2627use function array_column ;
2728use function array_filter ;
@@ -136,12 +137,11 @@ private function basedOnPhpAttributes(): array
136137 continue ;
137138 }
138139 } catch (AutowireAttributeException |AutowireParameterTypeException $ e ) {
139- throw ( new ArgumentBuilderException (
140+ throw $ this -> exceptionWithContext (
140141 message: sprintf ('Cannot build argument via php attribute for %s in %s. ' , $ param , $ param ->getDeclaringFunction ()),
141- previous: $ e
142- ))
143- ->setContext (context_param: $ param )
144- ;
142+ previous: $ e ,
143+ context_param: $ param
144+ );
145145 }
146146
147147 if (false !== $ this ->pushFromBindArguments ($ args , $ param )) {
@@ -177,12 +177,11 @@ private function basedOnBindArgumentsAsPriorityAndPhpAttributes(): array
177177 continue ;
178178 }
179179 } catch (AutowireAttributeException |AutowireParameterTypeException $ e ) {
180- throw ( new ArgumentBuilderException (
180+ throw $ this -> exceptionWithContext (
181181 message: sprintf ('Cannot build argument via php attribute for %s in %s. ' , $ param , $ param ->getDeclaringFunction ()),
182- previous: $ e
183- ))
184- ->setContext (context_param: $ param )
185- ;
182+ previous: $ e ,
183+ context_param: $ param
184+ );
186185 }
187186
188187 $ this ->pushFromParameterType ($ args , $ param );
@@ -208,12 +207,11 @@ private function pushFromParameterType(array &$args, ReflectionParameter $param)
208207 }
209208 } catch (AutowireParameterTypeException $ e ) {
210209 if (!$ param ->isDefaultValueAvailable ()) {
211- throw ( new ArgumentBuilderException (
210+ throw $ this -> exceptionWithContext (
212211 message: sprintf ('Cannot build argument via type hint for %s in %s. ' , $ param , functionName ($ param ->getDeclaringFunction ())),
213- previous: $ e
214- ))
215- ->setContext (context_parameter: $ param )
216- ;
212+ previous: $ e ,
213+ context_param: $ param
214+ );
217215 }
218216 }
219217 }
@@ -278,14 +276,10 @@ private function getTailArguments(): array
278276
279277 foreach ($ tailArgs as $ key => $ value ) {
280278 if (is_string ($ key )) {
281- throw (new ArgumentBuilderException (
282- sprintf ('Cannot build arguments for %s. Does not accept unknown named parameter $%s. ' , functionName ($ this ->functionOrMethod ), $ key )
283- ))
284- ->setContext (
285- bind_arguments: $ this ->bindArguments ,
286- tail_args: $ tailArgs ,
287- )
288- ;
279+ throw $ this ->exceptionWithContext (
280+ message: sprintf ('Cannot build arguments for %s. Does not accept unknown named parameter $%s. ' , functionName ($ this ->functionOrMethod ), $ key ),
281+ context_tail_args: $ tailArgs
282+ );
289283 }
290284 }
291285
@@ -349,4 +343,15 @@ private function getDefinitionByAttributes(ReflectionParameter $param): Generato
349343 yield $ definition ;
350344 }
351345 }
346+
347+ private function exceptionWithContext (string $ message , ?Throwable $ previous = null , mixed ...$ context ): ArgumentBuilderException
348+ {
349+ return (new ArgumentBuilderException (message: $ message , previous: $ previous ))
350+ ->setContext (
351+ ...$ context ,
352+ context_reflection_function: $ this ->functionOrMethod ,
353+ context_bind_arguments: $ this ->bindArguments ,
354+ )
355+ ;
356+ }
352357}
0 commit comments