|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use WPEmerge; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represent a generic handler - a Closure or a class method to be resolved from the service container |
|
11
|
|
|
*/ |
|
12
|
|
|
class Handler { |
|
13
|
|
|
/** |
|
14
|
|
|
* Parsed handler |
|
15
|
|
|
* |
|
16
|
|
|
* @var array|Closure|null |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $handler = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @throws Exception |
|
24
|
|
|
* @param string|Closure $raw_handler |
|
25
|
|
|
*/ |
|
26
|
4 |
|
public function __construct( $raw_handler ) { |
|
27
|
4 |
|
$handler = $this->parse( $raw_handler ); |
|
28
|
|
|
|
|
29
|
4 |
|
if ( $handler === null ) { |
|
30
|
1 |
|
throw new Exception( 'No or invalid handler provided.' ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
3 |
|
$this->handler = $handler; |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Parse a raw handler to a Closure or a [class, method] array |
|
38
|
|
|
* |
|
39
|
|
|
* @param string|Closure $handler |
|
|
|
|
|
|
40
|
|
|
* @return array|Closure|null |
|
41
|
|
|
*/ |
|
42
|
3 |
|
protected function parse( $raw_handler ) { |
|
43
|
3 |
|
if ( $raw_handler instanceof Closure ) { |
|
44
|
|
|
return $raw_handler; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
if ( is_string( $raw_handler ) ) { |
|
48
|
3 |
|
return $this->parseFromString( $raw_handler ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Parse a raw string handler to a [class, method] array |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $handler |
|
|
|
|
|
|
58
|
|
|
* @return array|null |
|
59
|
|
|
*/ |
|
60
|
3 |
|
protected function parseFromString( $raw_handler ) { |
|
61
|
3 |
|
$handlerPieces = preg_split( '/@|::/', $raw_handler, 2 ); |
|
62
|
|
|
|
|
63
|
3 |
|
if ( count( $handlerPieces ) === 2 ) { |
|
64
|
|
|
return array( |
|
65
|
2 |
|
'class' => $handlerPieces[0], |
|
66
|
2 |
|
'method' => $handlerPieces[1], |
|
67
|
2 |
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the parsed handler |
|
75
|
|
|
* |
|
76
|
|
|
* @return array|Closure|null |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function get() { |
|
79
|
1 |
|
return $this->handler; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Execute the parsed handler with any provided arguments and return the result |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $arguments,... |
|
|
|
|
|
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function execute() { |
|
89
|
3 |
|
$arguments = func_get_args(); |
|
90
|
3 |
|
if ( is_a( $this->handler, Closure::class ) ) { |
|
91
|
2 |
|
return call_user_func_array( $this->handler, $arguments ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$class = $this->handler['class']; |
|
95
|
1 |
|
$method = $this->handler['method']; |
|
96
|
|
|
|
|
97
|
1 |
|
$instance = WPEmerge::instantiate( $class ); |
|
98
|
1 |
|
return call_user_func_array( [$instance, $method], $arguments ); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.