-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionClassHelper.php
190 lines (159 loc) · 5.25 KB
/
ReflectionClassHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Oro\Component\PhpUtils;
class ReflectionClassHelper
{
/** @var string */
protected $className;
/** @var \ReflectionClass */
protected $refClass;
/** @var \ReflectionMethod[] */
protected $refMethods = [];
/** @var string */
protected $lastError;
/**
* @param string $className
*/
public function __construct($className)
{
$this->className = $className;
}
/**
* Returns the last occurred error
*
* @return string
*/
public function getLastError()
{
$error = $this->lastError;
$this->lastError = null;
return $error;
}
/**
* Checks whether the given method exist in class declaration
*
* @param string $method The name of a method
*
* @return bool
*/
public function hasMethod($method)
{
if (empty($method)) {
return false;
}
return $this->getRefClass()->hasMethod($method);
}
/**
* Validates arguments list. $this->getLastError() will return error message if this method return FALSE
*
* @param string $method The name of a method
* @param array $arguments The list of expected arguments
*
* @return bool
*/
public function isValidArguments($method, array $arguments)
{
$refMethod = $this->getMethod($method);
$params = $refMethod->getParameters();
$paramNames = $this->mapParameterListToNames($params);
$argumentsCount = count($arguments);
$argumentsKeys = array_keys($arguments);
$isAssoc = ArrayUtil::isAssoc($arguments);
$diff = array_diff($argumentsKeys, $paramNames);
if ($isAssoc && $diff) {
$this->lastError = sprintf('Unknown argument(s) for "%s" method given: ', $method);
$this->lastError .= implode(', ', $diff);
return false;
} elseif (!$isAssoc && $refMethod->getNumberOfParameters() < $argumentsCount) {
$this->lastError = sprintf('Number of arguments given greater than declared in "%s" method', $method);
return false;
}
if ($isAssoc || $argumentsCount === 0) {
$requiredParams = array_filter(
$params,
function (\ReflectionParameter $param) {
return !$param->isOptional();
}
);
$requiredParamNames = $this->mapParameterListToNames($requiredParams);
$missingArguments = array_diff($requiredParamNames, $argumentsKeys);
if (!empty($missingArguments)) {
$this->lastError = sprintf('Missing required argument(s) for "%s" method: ', $method);
$this->lastError .= implode(', ', $missingArguments);
return false;
}
} elseif ($argumentsCount < $refMethod->getNumberOfRequiredParameters()) {
$this->lastError = sprintf(
'"%s" method requires at least %d argument(s) to be passed, %d given',
$method,
$refMethod->getNumberOfRequiredParameters(),
$argumentsCount
);
return false;
}
return true;
}
/**
* Completes arguments array by default values that were not passed, but set at declaration
*
* @param string $method The name of a method
* @param array $arguments The list of arguments
*/
public function completeArguments($method, array &$arguments)
{
if (ArrayUtil::isAssoc($arguments)) {
$result = [];
$refMethod = $this->getMethod($method);
$params = $refMethod->getParameters();
foreach ($params as $param) {
$hasValue = isset($arguments[$param->name]) || array_key_exists($param->name, $arguments);
if (!$hasValue && $param->isOptional() && !empty($arguments)) {
$result[$param->name] = $param->getDefaultValue();
} elseif ($hasValue) {
$result[$param->name] = $arguments[$param->name];
}
unset($arguments[$param->name]);
}
$arguments = $result;
}
}
/**
* @param \ReflectionParameter[] $parameters
*
* @return string[]
*/
protected function mapParameterListToNames(array $parameters)
{
return array_map(
function (\ReflectionParameter $parameter) {
return $parameter->name;
},
$parameters
);
}
/**
* Lazy initialization of reflection class instance
*
* @return \ReflectionClass
*/
protected function getRefClass()
{
if (null === $this->refClass) {
$this->refClass = new \ReflectionClass($this->className);
}
return $this->refClass;
}
/**
* Lazy initialization of reflection method instance
*
* @param string $method
*
* @return \ReflectionMethod
*/
protected function getMethod($method)
{
if (!isset($this->refMethods[$method])) {
$this->refMethods[$method] = $this->getRefClass()->getMethod($method);
}
return $this->refMethods[$method];
}
}