|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Atreyu; |
|
4
|
|
|
|
|
5
|
|
|
class Executable |
|
6
|
|
|
{ |
|
7
|
|
|
private $callableReflection; |
|
8
|
|
|
private $invocationObject; |
|
9
|
|
|
private $isInstanceMethod; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(\ReflectionFunctionAbstract $reflFunc, $invocationObject = null) |
|
12
|
|
|
{ |
|
13
|
|
|
if ($reflFunc instanceof \ReflectionMethod) { |
|
14
|
|
|
$this->isInstanceMethod = true; |
|
15
|
|
|
$this->setMethodCallable($reflFunc, $invocationObject); |
|
16
|
|
|
} else { |
|
17
|
|
|
$this->isInstanceMethod = false; |
|
18
|
|
|
$this->callableReflection = $reflFunc; |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function setMethodCallable(\ReflectionMethod $reflection, $invocationObject) |
|
23
|
|
|
{ |
|
24
|
|
|
if (is_object($invocationObject)) { |
|
25
|
|
|
$this->callableReflection = $reflection; |
|
26
|
|
|
$this->invocationObject = $invocationObject; |
|
27
|
|
|
} elseif ($reflection->isStatic()) { |
|
28
|
|
|
$this->callableReflection = $reflection; |
|
29
|
|
|
} else { |
|
30
|
|
|
throw new \InvalidArgumentException( |
|
31
|
|
|
'ReflectionMethod callables must specify an invocation object' |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function __invoke() |
|
37
|
|
|
{ |
|
38
|
|
|
$args = func_get_args(); |
|
39
|
|
|
$reflection = $this->callableReflection; |
|
40
|
|
|
|
|
41
|
|
|
if ($this->isInstanceMethod) { |
|
42
|
|
|
return $reflection->invokeArgs($this->invocationObject, $args); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->callableReflection->isClosure() |
|
46
|
|
|
? $this->invokeClosureCompat($reflection, $args) |
|
47
|
|
|
: $reflection->invokeArgs($args); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @TODO Remove this extra indirection when 5.3 support is dropped |
|
52
|
|
|
* @param $reflection |
|
53
|
|
|
* @param $args |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
private function invokeClosureCompat($reflection, $args) |
|
58
|
|
|
{ |
|
59
|
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) { |
|
60
|
|
|
$scope = $reflection->getClosureScopeClass(); |
|
61
|
|
|
$closure = \Closure::bind( |
|
62
|
|
|
$reflection->getClosure(), |
|
63
|
|
|
$reflection->getClosureThis(), |
|
64
|
|
|
$scope ? $scope->name : null |
|
65
|
|
|
); |
|
66
|
|
|
return call_user_func_array($closure, $args); |
|
67
|
|
|
} else { |
|
68
|
|
|
return $reflection->invokeArgs($args); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getCallableReflection() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->callableReflection; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getInvocationObject() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->invocationObject; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function isInstanceMethod() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->isInstanceMethod; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: