1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reflection\Validator\Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* An exception for marking a definition of function/method as unacceptable. |
7
|
|
|
*/ |
8
|
|
|
class BadFunctionDefinitionException extends \ReflectionException implements \ArrayAccess |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Global tokens to replace in error messages. |
12
|
|
|
* |
13
|
|
|
* @var string[] |
14
|
|
|
*/ |
15
|
|
|
private $tokens = []; |
16
|
|
|
/** |
17
|
|
|
* A list of error messages. |
18
|
|
|
* |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
private $errors = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* BadFunctionDefinitionException constructor. |
25
|
|
|
* |
26
|
|
|
* @param \ReflectionFunctionAbstract $function |
27
|
|
|
* A function/method to collect violations for. |
28
|
|
|
* |
29
|
|
|
* @throws self |
30
|
|
|
* When argument is not of "ReflectionMethod" or "ReflectionFunction" type. |
31
|
|
|
*/ |
32
|
20 |
|
public function __construct(\ReflectionFunctionAbstract $function) |
33
|
|
|
{ |
34
|
20 |
|
parent::__construct(); |
35
|
|
|
|
36
|
20 |
|
if ($function instanceof \ReflectionMethod) { |
37
|
18 |
|
$this->tokens['@function'] = sprintf('"%s::%s()" method', $function->class, $function->name); |
38
|
2 |
|
} elseif ($function instanceof \ReflectionFunction) { |
39
|
1 |
|
$this->tokens['@function'] = sprintf('"%s()" function', $function->name); |
40
|
|
|
} else { |
41
|
1 |
|
$this->message = sprintf( |
42
|
1 |
|
'The argument of the "%s()" must be of "%s" or "%s" type, "%s" given.', |
43
|
1 |
|
__METHOD__, |
44
|
1 |
|
\ReflectionMethod::class, |
45
|
1 |
|
\ReflectionFunction::class, |
46
|
1 |
|
get_class($function) |
47
|
|
|
); |
48
|
|
|
|
49
|
1 |
|
throw $this; |
50
|
|
|
} |
51
|
19 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
2 |
|
public function offsetExists($token) |
57
|
|
|
{ |
58
|
2 |
|
return isset($this->tokens[$token]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
2 |
|
public function offsetGet($token) |
65
|
|
|
{ |
66
|
2 |
|
return $this->tokens[$token]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
15 |
|
public function offsetSet($token, $value) |
73
|
|
|
{ |
74
|
15 |
|
$this->tokens[$token] = $value; |
75
|
15 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
2 |
|
public function offsetUnset($token) |
81
|
|
|
{ |
82
|
2 |
|
unset($this->tokens[$token]); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds an error message to the list. |
87
|
|
|
* |
88
|
|
|
* @param string $message |
89
|
|
|
* A message to add. |
90
|
|
|
* @param string[] $tokens |
91
|
|
|
* An array of additional tokens to replace in a message. |
92
|
|
|
*/ |
93
|
17 |
|
public function addError(string $message, array $tokens = []) |
94
|
|
|
{ |
95
|
17 |
|
$this->errors[] = strtr($message, $this->tokens + $tokens); |
96
|
17 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns a list of error messages. |
100
|
|
|
* |
101
|
|
|
* @return string[] |
102
|
|
|
* A list of error messages. |
103
|
|
|
*/ |
104
|
19 |
|
public function getErrors(): array |
105
|
|
|
{ |
106
|
19 |
|
return $this->errors; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Throws an instance of self in a case when errors list is not empty. |
111
|
|
|
* |
112
|
|
|
* @throws self |
113
|
|
|
*/ |
114
|
19 |
|
public function throwIfErrorsExist() |
115
|
|
|
{ |
116
|
19 |
|
if (!empty($this->errors)) { |
117
|
17 |
|
$this->tokens = []; |
118
|
17 |
|
$this->message = implode(PHP_EOL, $this->errors); |
119
|
|
|
|
120
|
17 |
|
throw $this; |
121
|
|
|
} |
122
|
4 |
|
} |
123
|
|
|
} |
124
|
|
|
|