1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator; |
6
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
8
|
|
|
use Laminas\Code\Reflection\MethodReflection; |
9
|
|
|
use ProxyManager\Generator\MethodGenerator; |
10
|
|
|
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use function count; |
13
|
|
|
use function strtr; |
14
|
|
|
use function var_export; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Method decorator for remote objects |
18
|
|
|
*/ |
19
|
|
|
class RemoteObjectMethod extends MethodGenerator |
20
|
3 |
|
{ |
21
|
|
|
private const TEMPLATE |
22
|
|
|
= <<<'PHP' |
23
|
|
|
$defaultValues = #DEFAULT_VALUES#; |
24
|
|
|
$declaredParameterCount = #PARAMETER_COUNT#; |
25
|
|
|
|
26
|
3 |
|
$args = \func_get_args() + $defaultValues; |
27
|
|
|
|
28
|
3 |
|
#PROXIED_RETURN# |
29
|
3 |
|
PHP; |
30
|
3 |
|
|
31
|
3 |
|
/** @return self|static */ |
32
|
3 |
|
public static function generateMethod( |
33
|
|
|
MethodReflection $originalMethod, |
34
|
|
|
PropertyGenerator $adapterProperty, |
35
|
3 |
|
ReflectionClass $originalClass |
36
|
|
|
) : self { |
37
|
|
|
/** @var self $method */ |
38
|
|
|
$method = static::fromReflectionWithoutBodyAndDocBlock($originalMethod); |
39
|
|
|
$proxiedReturn = '$return = $this->' . $adapterProperty->getName() |
40
|
|
|
. '->call(' . var_export($originalClass->getName(), true) |
41
|
|
|
. ', ' . var_export($originalMethod->getName(), true) . ', $args);' . "\n\n" |
42
|
|
|
. ProxiedMethodReturnExpression::generate('$return', $originalMethod); |
43
|
|
|
|
44
|
|
|
$defaultValues = self::getDefaultValuesForMethod($originalMethod); |
45
|
|
|
$declaredParameterCount = count($originalMethod->getParameters()); |
46
|
|
|
|
47
|
|
|
$method->setBody( |
48
|
|
|
strtr( |
49
|
|
|
self::TEMPLATE, |
50
|
|
|
[ |
51
|
|
|
'#PROXIED_RETURN#' => $proxiedReturn, |
52
|
|
|
'#DEFAULT_VALUES#' => var_export($defaultValues, true), |
53
|
|
|
'#PARAMETER_COUNT#' => var_export($declaredParameterCount, true), |
54
|
|
|
] |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return $method; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
private static function getDefaultValuesForMethod(MethodReflection $originalMethod) : array |
65
|
|
|
{ |
66
|
|
|
$defaultValues = []; |
67
|
|
|
foreach ($originalMethod->getParameters() as $parameter) { |
68
|
|
|
if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) { |
69
|
|
|
/** @psalm-var int|float|bool|array|string|null */ |
70
|
|
|
$defaultValues[] = $parameter->getDefaultValue(); |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($parameter->isVariadic()) { |
|
|
|
|
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$defaultValues[] = null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $defaultValues; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.