Skip to content

Commit d34dc96

Browse files
committed
fix: Function ReflectionType::__toString() is deprecated error
1 parent 611062c commit d34dc96

File tree

1 file changed

+69
-56
lines changed

1 file changed

+69
-56
lines changed

src/Obj/ObjectHelper.php

+69-56
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ReflectionException;
1616
use ReflectionFunctionAbstract;
1717
use ReflectionMethod;
18+
use ReflectionNamedType;
1819
use RuntimeException;
1920
use Toolkit\Stdlib\Helper\PhpHelper;
2021
use Traversable;
@@ -114,7 +115,6 @@ public static function setAttrs($object, array $options): void
114115
*/
115116
public static function mappingProps($object, array $data): void
116117
{
117-
// TODO
118118
$rftObj = PhpHelper::reflectClass($object);
119119
foreach ($rftObj->getProperties() as $rftProp) {
120120
// TODO
@@ -213,58 +213,6 @@ public static function hash($object, bool $unique = true): string
213213
return is_string($object) ? md5($object) : '';
214214
}
215215

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-
268216
/**
269217
* Build an array of class method parameters.
270218
*
@@ -280,15 +228,29 @@ public static function buildReflectCallArgs(ReflectionFunctionAbstract $rftFunc,
280228
{
281229
$funcArgs = [];
282230
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+
283246
// filling by param type. eg: an class name
284-
$typeName = (string)$param->getType();
247+
$typeName = $pType->getName();
285248
if ($typeName !== Closure::class && isset($provideArgs[$typeName])) {
286249
$funcArgs[] = $provideArgs[$typeName];
287250
continue;
288251
}
289252

290253
// filling by param name and type is same.
291-
$name = $param->getName();
292254
if (isset($provideArgs[$name]) && $typeName === gettype($provideArgs[$name])) {
293255
$funcArgs[] = $provideArgs[$name];
294256
continue;
@@ -301,7 +263,7 @@ public static function buildReflectCallArgs(ReflectionFunctionAbstract $rftFunc,
301263
}
302264

303265
throw new RuntimeException(sprintf(
304-
'Could not resolve dependency: %s for the %dth parameter',
266+
'Could not resolve the %dth parameter(%s)',
305267
$param->getPosition(),
306268
$name
307269
));
@@ -366,6 +328,57 @@ public static function createByArray($config)
366328
return null;
367329
}
368330

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+
}
369382

370383
/**
371384
* Get class namespace

0 commit comments

Comments
 (0)