1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reflection\Validator; |
4
|
|
|
|
5
|
|
|
use Reflection\Validator\Exception\BadFunctionDefinitionException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Validates function specification. |
9
|
|
|
*/ |
10
|
|
|
class FunctionValidator |
11
|
|
|
{ |
12
|
|
|
use TypeValidator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A function/method to check the specification of. |
16
|
|
|
* |
17
|
|
|
* @var \ReflectionFunctionAbstract |
18
|
|
|
*/ |
19
|
|
|
protected $target; |
20
|
|
|
/** |
21
|
|
|
* Violations collector. |
22
|
|
|
* |
23
|
|
|
* @var BadFunctionDefinitionException |
24
|
|
|
*/ |
25
|
|
|
protected $exception; |
26
|
|
|
/** |
27
|
|
|
* A list of arguments that function/method must have. |
28
|
|
|
* |
29
|
|
|
* @var ArgumentSpecification[] |
30
|
|
|
*/ |
31
|
|
|
protected $arguments = []; |
32
|
|
|
/** |
33
|
|
|
* A type of returning value by the function. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $returnType = ''; |
38
|
|
|
/** |
39
|
|
|
* A state whether function must return value by reference. |
40
|
|
|
* |
41
|
|
|
* @var null|bool |
42
|
|
|
*/ |
43
|
|
|
protected $returnByReference = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* FunctionValidator constructor. |
47
|
|
|
* |
48
|
|
|
* @param \ReflectionFunctionAbstract $function |
49
|
|
|
* A function/method to check the specification of. |
50
|
|
|
*/ |
51
|
9 |
|
public function __construct(\ReflectionFunctionAbstract $function) |
52
|
|
|
{ |
53
|
9 |
|
$this->target = $function; |
54
|
9 |
|
$this->exception = new BadFunctionDefinitionException($function); |
55
|
9 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Defines an argument that function must have. |
59
|
|
|
* |
60
|
|
|
* @param ArgumentSpecification $argument |
61
|
|
|
* A specification of the argument. |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
6 |
|
public function addArgument(ArgumentSpecification $argument): self |
66
|
|
|
{ |
67
|
6 |
|
$this->arguments[] = $argument; |
68
|
|
|
|
69
|
6 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Require the function to return a value by reference. |
74
|
|
|
* |
75
|
|
|
* @param bool $state |
76
|
|
|
* An indicator of a state. |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
7 |
|
public function setReturnByReference(bool $state): self |
81
|
|
|
{ |
82
|
7 |
|
$this->returnByReference = $state; |
83
|
|
|
|
84
|
7 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Require the function to specify a type of returning value. |
89
|
|
|
* |
90
|
|
|
* @param string $type |
91
|
|
|
* A type of returning value. |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
7 |
|
public function setReturnType(string $type): self |
96
|
|
|
{ |
97
|
7 |
|
$this->returnType = trim($type); |
98
|
|
|
|
99
|
7 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Runs a validation during destruction. |
104
|
|
|
*/ |
105
|
9 |
|
public function __destruct() |
106
|
|
|
{ |
107
|
9 |
|
$this->checkArguments(); |
108
|
9 |
|
$this->checkReturnType(); |
109
|
9 |
|
$this->checkReturnByReference(); |
110
|
|
|
|
111
|
9 |
|
$this->exception->throwIfErrorsExist(); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Validates arguments of a function. |
116
|
|
|
*/ |
117
|
9 |
|
protected function checkArguments() |
118
|
|
|
{ |
119
|
9 |
|
if (empty($this->exception->getErrors())) { |
120
|
8 |
|
$arguments_count = count($this->arguments); |
121
|
8 |
|
$parameters_count = $this->target->getNumberOfParameters(); |
122
|
|
|
|
123
|
8 |
|
if ($parameters_count !== $arguments_count) { |
124
|
3 |
|
$this->exception->addError( |
125
|
3 |
|
'The @function must have @argumentsCount arguments, @parametersCount given.', |
126
|
|
|
[ |
127
|
3 |
|
'@argumentsCount' => $arguments_count, |
128
|
3 |
|
'@parametersCount' => $parameters_count, |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
} else { |
132
|
5 |
|
foreach ($this->target->getParameters() as $i => $parameter) { |
133
|
5 |
|
$this->arguments[$i]->validate($parameter, $this->exception); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
9 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Validates returning type. |
141
|
|
|
*/ |
142
|
9 |
|
protected function checkReturnType() |
143
|
|
|
{ |
144
|
9 |
|
if ('' !== $this->returnType && empty($this->exception->getErrors())) { |
145
|
5 |
|
$return_type = $this->target->getReturnType(); |
146
|
|
|
|
147
|
5 |
|
if (null === $return_type) { |
148
|
1 |
|
$this->exception->addError('The type of returning value for @function is not specified.'); |
149
|
4 |
|
} elseif (!$this->isTypeValid($return_type, $this->returnType)) { |
150
|
1 |
|
$this->exception->addError( |
151
|
1 |
|
'The @function must return a value of "@returnType", but at the moment it is "@currentType".', |
152
|
|
|
[ |
153
|
1 |
|
'@returnType' => $this->returnType, |
154
|
1 |
|
'@currentType' => $return_type, |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
9 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Validates returning by reference. |
163
|
|
|
*/ |
164
|
9 |
|
protected function checkReturnByReference() |
165
|
|
|
{ |
166
|
9 |
|
if (null !== $this->returnByReference && empty($this->exception->getErrors())) { |
167
|
3 |
|
$returns_by_reference = $this->target->returnsReference(); |
168
|
|
|
|
169
|
3 |
|
if ($this->returnByReference) { |
170
|
1 |
|
if (!$returns_by_reference) { |
171
|
1 |
|
$this->exception->addError('The @function must return a value by reference.'); |
172
|
|
|
} |
173
|
|
|
} else { |
174
|
2 |
|
if ($returns_by_reference) { |
175
|
1 |
|
$this->exception->addError('The @function must not return a value by reference.'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
9 |
|
} |
180
|
|
|
} |
181
|
|
|
|