|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reflection\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Reflection\Validator\Exception\BadFunctionDefinitionException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Specification of the method/function argument. |
|
9
|
|
|
*/ |
|
10
|
|
|
class ArgumentSpecification |
|
11
|
|
|
{ |
|
12
|
|
|
use TypeValidator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A name of the argument. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $name = ''; |
|
20
|
|
|
/** |
|
21
|
|
|
* A type of the argument. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $type = ''; |
|
26
|
|
|
/** |
|
27
|
|
|
* Indicates whether an argument must be optional. |
|
28
|
|
|
* |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $optional = false; |
|
32
|
|
|
/** |
|
33
|
|
|
* Indicates whether an argument must be passed by reference. |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $passedByReference = false; |
|
38
|
|
|
/** |
|
39
|
|
|
* A parameter to compare specification with. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \ReflectionParameter |
|
42
|
|
|
*/ |
|
43
|
|
|
private $parameter; |
|
44
|
|
|
/** |
|
45
|
|
|
* A bag for errors. |
|
46
|
|
|
* |
|
47
|
|
|
* @var BadFunctionDefinitionException |
|
48
|
|
|
*/ |
|
49
|
|
|
private $exception; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* ArgumentSpecification constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* A name of the argument. |
|
56
|
|
|
*/ |
|
57
|
15 |
|
public function __construct(string $name) |
|
58
|
|
|
{ |
|
59
|
15 |
|
$this->name = $name; |
|
60
|
15 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Defines a type of the argument. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $type |
|
66
|
|
|
* A type of the argument. |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
14 |
|
public function setType(string $type): self |
|
71
|
|
|
{ |
|
72
|
14 |
|
$this->type = trim($type); |
|
73
|
|
|
|
|
74
|
14 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Defines whether an argument is optional. |
|
79
|
|
|
* |
|
80
|
|
|
* @param bool $state |
|
81
|
|
|
* An indicator of a state. |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
15 |
|
public function setOptional(bool $state): self |
|
86
|
|
|
{ |
|
87
|
15 |
|
$this->optional = $state; |
|
88
|
|
|
|
|
89
|
15 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Defines whether an argument is passed by reference. |
|
94
|
|
|
* |
|
95
|
|
|
* @param bool $state |
|
96
|
|
|
* An indicator of a state. |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
15 |
|
public function setPassedByReference(bool $state): self |
|
101
|
|
|
{ |
|
102
|
15 |
|
$this->passedByReference = $state; |
|
103
|
|
|
|
|
104
|
15 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Validates whether argument specification according to parameter definition. |
|
109
|
|
|
* |
|
110
|
|
|
* @param \ReflectionParameter $parameter |
|
111
|
|
|
* A parameter to compare specification with. |
|
112
|
|
|
* @param BadFunctionDefinitionException|null $exception |
|
113
|
|
|
* An exception to collect violations. |
|
114
|
|
|
*/ |
|
115
|
13 |
|
public function validate(\ReflectionParameter $parameter, BadFunctionDefinitionException $exception = null) |
|
116
|
|
|
{ |
|
117
|
13 |
|
$stop_here = false; |
|
118
|
|
|
|
|
119
|
13 |
|
if (null === $exception) { |
|
120
|
8 |
|
$exception = new BadFunctionDefinitionException($parameter->getDeclaringFunction()); |
|
121
|
8 |
|
$stop_here = true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
13 |
|
$exception['@argument'] = sprintf('argument %d', $parameter->getPosition() + 1); |
|
125
|
|
|
|
|
126
|
13 |
|
$this->parameter = $parameter; |
|
127
|
13 |
|
$this->exception = $exception; |
|
128
|
|
|
|
|
129
|
13 |
|
$this->checkName(); |
|
130
|
13 |
|
$this->checkOptional(); |
|
131
|
13 |
|
$this->checkReference(); |
|
132
|
13 |
|
$this->checkType(); |
|
133
|
|
|
|
|
134
|
|
|
// An exception was not passed from the outside which means that we |
|
135
|
|
|
// don't have an execution context and can fail here. |
|
136
|
13 |
|
if ($stop_here) { |
|
137
|
8 |
|
$exception->throwIfErrorsExist(); |
|
138
|
|
|
} |
|
139
|
5 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Checks that parameter is named correctly. |
|
143
|
|
|
*/ |
|
144
|
13 |
|
private function checkName() |
|
145
|
|
|
{ |
|
146
|
13 |
|
if (empty($this->exception->getErrors()) && $this->name !== $this->parameter->name) { |
|
147
|
1 |
|
$this->exception->addError( |
|
148
|
1 |
|
'The @argument of the @function has the "@givenName" name, but must be "@requiredName".', |
|
149
|
|
|
[ |
|
150
|
1 |
|
'@givenName' => $this->parameter->name, |
|
151
|
1 |
|
'@requiredName' => $this->name, |
|
152
|
|
|
] |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
13 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Checks optionality of the parameter. |
|
159
|
|
|
*/ |
|
160
|
13 |
|
private function checkOptional() |
|
161
|
|
|
{ |
|
162
|
13 |
|
if (empty($this->exception->getErrors())) { |
|
163
|
12 |
|
$has_default_value = $this->parameter->isDefaultValueAvailable(); |
|
164
|
|
|
|
|
165
|
12 |
|
if ($this->optional) { |
|
166
|
1 |
|
if (!$has_default_value) { |
|
167
|
1 |
|
$this->exception->addError('The @argument of the @function must be optional.'); |
|
168
|
|
|
} |
|
169
|
|
|
} else { |
|
170
|
11 |
|
if ($has_default_value) { |
|
171
|
1 |
|
$this->exception->addError('The @argument of the @function must not be optional.'); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
13 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Checks referenceability of the parameter. |
|
179
|
|
|
*/ |
|
180
|
13 |
|
private function checkReference() |
|
181
|
|
|
{ |
|
182
|
13 |
|
if (empty($this->exception->getErrors())) { |
|
183
|
10 |
|
$has_passed_by_reference = $this->parameter->isPassedByReference(); |
|
184
|
|
|
|
|
185
|
10 |
|
if ($this->passedByReference) { |
|
186
|
1 |
|
if (!$has_passed_by_reference) { |
|
187
|
1 |
|
$this->exception->addError('The @argument of the @function must be passed by reference.'); |
|
188
|
|
|
} |
|
189
|
|
|
} else { |
|
190
|
9 |
|
if ($has_passed_by_reference) { |
|
191
|
1 |
|
$this->exception->addError('The @argument of the @function must not be passed by reference.'); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
13 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Checks a type of the parameter. |
|
199
|
|
|
*/ |
|
200
|
13 |
|
private function checkType() |
|
201
|
|
|
{ |
|
202
|
13 |
|
if (empty($this->exception->getErrors())) { |
|
203
|
8 |
|
$parameter_type = $this->parameter->getType(); |
|
204
|
|
|
|
|
205
|
8 |
|
if ('' === $this->type) { |
|
206
|
1 |
|
$this->exception->addError('The type of the @argument of the @function is not specified.'); |
|
207
|
7 |
|
} elseif (null === $parameter_type) { |
|
208
|
1 |
|
$this->exception->addError( |
|
209
|
1 |
|
'The type of the @argument of the @function must be of "@argumentType" type, none given.', |
|
210
|
|
|
[ |
|
211
|
1 |
|
'@argumentType' => $this->type, |
|
212
|
|
|
] |
|
213
|
|
|
); |
|
214
|
6 |
|
} elseif (!$this->isTypeValid($parameter_type, $this->type)) { |
|
215
|
1 |
|
$this->exception->addError( |
|
216
|
1 |
|
'The @argument of the @function must be of "@argumentType" type, "@parameterType" given.', |
|
217
|
|
|
[ |
|
218
|
1 |
|
'@argumentType' => $this->type, |
|
219
|
1 |
|
'@parameterType' => $parameter_type, |
|
220
|
|
|
] |
|
221
|
|
|
); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
13 |
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|