15
15
use ReflectionException ;
16
16
use ReflectionFunctionAbstract ;
17
17
use ReflectionMethod ;
18
+ use ReflectionNamedType ;
18
19
use RuntimeException ;
19
20
use Toolkit \Stdlib \Helper \PhpHelper ;
20
21
use Traversable ;
@@ -114,7 +115,6 @@ public static function setAttrs($object, array $options): void
114
115
*/
115
116
public static function mappingProps ($ object , array $ data ): void
116
117
{
117
- // TODO
118
118
$ rftObj = PhpHelper::reflectClass ($ object );
119
119
foreach ($ rftObj ->getProperties () as $ rftProp ) {
120
120
// TODO
@@ -213,58 +213,6 @@ public static function hash($object, bool $unique = true): string
213
213
return is_string ($ object ) ? md5 ($ object ) : '' ;
214
214
}
215
215
216
- /**
217
- * Build an array of class method parameters.
218
- *
219
- * @param ReflectionMethod $method Method for which to build the argument array.
220
- * @param array $provideArgs Manual provide params map.
221
- *
222
- * @return array
223
- * @throws RuntimeException
224
- * @throws ReflectionException
225
- */
226
- public static function getMethodArgs (ReflectionMethod $ method , array $ provideArgs = []): array
227
- {
228
- $ methodArgs = [];
229
-
230
- foreach ($ method ->getParameters () as $ idx => $ param ) {
231
- // if user have been provide arg
232
- if (isset ($ provideArgs [$ idx ])) {
233
- $ methodArgs [] = $ provideArgs [$ idx ];
234
- continue ;
235
- }
236
-
237
- // $depRftClass = $param->getClass();
238
- $ depRftClass = $ param ->getType ();
239
-
240
- // If we have a dependency, that means it has been type-hinted.
241
- if ($ depRftClass && ($ depClass = $ depRftClass ->getName ()) !== Closure::class) {
242
- $ depObject = self ::create ($ depClass );
243
-
244
- if ($ depObject instanceof $ depClass ) {
245
- $ methodArgs [] = $ depObject ;
246
- continue ;
247
- }
248
- }
249
-
250
- // Finally, if there is a default parameter, use it.
251
- if ($ param ->isOptional ()) {
252
- $ methodArgs [] = $ param ->getDefaultValue ();
253
- continue ;
254
- }
255
-
256
- // $dependencyVarName = $param->getName();
257
- // Couldn't resolve dependency, and no default was provided.
258
- throw new RuntimeException (sprintf (
259
- 'Could not resolve dependency: %s for the %dth parameter ' ,
260
- $ param ->getPosition (),
261
- $ param ->getName ()
262
- ));
263
- }
264
-
265
- return $ methodArgs ;
266
- }
267
-
268
216
/**
269
217
* Build an array of class method parameters.
270
218
*
@@ -280,15 +228,29 @@ public static function buildReflectCallArgs(ReflectionFunctionAbstract $rftFunc,
280
228
{
281
229
$ funcArgs = [];
282
230
foreach ($ rftFunc ->getParameters () as $ param ) {
231
+ $ name = $ param ->getName ();
232
+ $ pType = $ param ->getType ();
233
+ if (!$ pType instanceof ReflectionNamedType) {
234
+ if ($ param ->isOptional ()) {
235
+ $ funcArgs [] = $ param ->getDefaultValue ();
236
+ continue ;
237
+ }
238
+
239
+ throw new RuntimeException (sprintf (
240
+ 'Could not resolve the %dth parameter(%s) ' ,
241
+ $ param ->getPosition (),
242
+ $ name
243
+ ));
244
+ }
245
+
283
246
// filling by param type. eg: an class name
284
- $ typeName = ( string ) $ param -> getType ();
247
+ $ typeName = $ pType -> getName ();
285
248
if ($ typeName !== Closure::class && isset ($ provideArgs [$ typeName ])) {
286
249
$ funcArgs [] = $ provideArgs [$ typeName ];
287
250
continue ;
288
251
}
289
252
290
253
// filling by param name and type is same.
291
- $ name = $ param ->getName ();
292
254
if (isset ($ provideArgs [$ name ]) && $ typeName === gettype ($ provideArgs [$ name ])) {
293
255
$ funcArgs [] = $ provideArgs [$ name ];
294
256
continue ;
@@ -301,7 +263,7 @@ public static function buildReflectCallArgs(ReflectionFunctionAbstract $rftFunc,
301
263
}
302
264
303
265
throw new RuntimeException (sprintf (
304
- 'Could not resolve dependency: %s for the %dth parameter ' ,
266
+ 'Could not resolve the %dth parameter(%s) ' ,
305
267
$ param ->getPosition (),
306
268
$ name
307
269
));
@@ -366,6 +328,57 @@ public static function createByArray($config)
366
328
return null ;
367
329
}
368
330
331
+ /**
332
+ * Build an array of class method parameters.
333
+ *
334
+ * @param ReflectionMethod $method Method for which to build the argument array.
335
+ * @param array $provideArgs Manual provide params map.
336
+ *
337
+ * @return array
338
+ * @throws RuntimeException
339
+ * @throws ReflectionException
340
+ */
341
+ public static function getMethodArgs (ReflectionMethod $ method , array $ provideArgs = []): array
342
+ {
343
+ $ methodArgs = [];
344
+
345
+ foreach ($ method ->getParameters () as $ idx => $ param ) {
346
+ // if user have been provide arg
347
+ if (isset ($ provideArgs [$ idx ])) {
348
+ $ methodArgs [] = $ provideArgs [$ idx ];
349
+ continue ;
350
+ }
351
+
352
+ // $depRftClass = $param->getClass();
353
+ $ depRftClass = $ param ->getType ();
354
+
355
+ // If we have a dependency, that means it has been type-hinted.
356
+ if ($ depRftClass && ($ depClass = $ depRftClass ->getName ()) !== Closure::class) {
357
+ $ depObject = self ::create ($ depClass );
358
+
359
+ if ($ depObject instanceof $ depClass ) {
360
+ $ methodArgs [] = $ depObject ;
361
+ continue ;
362
+ }
363
+ }
364
+
365
+ // Finally, if there is a default parameter, use it.
366
+ if ($ param ->isOptional ()) {
367
+ $ methodArgs [] = $ param ->getDefaultValue ();
368
+ continue ;
369
+ }
370
+
371
+ // $dependencyVarName = $param->getName();
372
+ // Couldn't resolve dependency, and no default was provided.
373
+ throw new RuntimeException (sprintf (
374
+ 'Could not resolve dependency: %s for the %dth parameter ' ,
375
+ $ param ->getName (),
376
+ $ param ->getPosition ()
377
+ ));
378
+ }
379
+
380
+ return $ methodArgs ;
381
+ }
369
382
370
383
/**
371
384
* Get class namespace
0 commit comments