1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ParamProcessor; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @since 1.0 |
7
|
|
|
* |
8
|
|
|
* @licence GNU GPL v2+ |
9
|
|
|
* @author Jeroen De Dauw < [email protected] > |
10
|
|
|
*/ |
11
|
|
|
class ProcessingError { |
12
|
|
|
|
13
|
|
|
const SEVERITY_MINOR = 0; // Minor error. ie a deprecation notice |
14
|
|
|
const SEVERITY_LOW = 1; // Lower-then-normal severity. ie an unknown parameter |
15
|
|
|
const SEVERITY_NORMAL = 2; // Normal severity. ie an invalid value provided |
16
|
|
|
const SEVERITY_HIGH = 3; // Higher-then-normal severity. ie an invalid value for a significant parameter |
17
|
|
|
const SEVERITY_FATAL = 4; // Fatal error. Either a missing or an invalid required parameter |
18
|
|
|
|
19
|
|
|
const ACTION_IGNORE = 0; // Ignore the error |
20
|
|
|
const ACTION_LOG = 1; // Log the error |
21
|
|
|
const ACTION_WARN = 2; // Warn that there is an error |
22
|
|
|
const ACTION_SHOW = 3; // Show the error |
23
|
|
|
const ACTION_DEMAND = 4; // Show the error and don't render output |
24
|
|
|
|
25
|
|
|
public $message; |
26
|
|
|
public $severity; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* List of 'tags' for the error. This is mainly meant for indicating an error |
30
|
|
|
* type, such as 'missing parameter' or 'invalid value', but allows for multiple |
31
|
|
|
* such indications. |
32
|
|
|
* |
33
|
|
|
* @since 0.4 |
34
|
|
|
* |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $tags; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Where the error occurred. |
41
|
|
|
* |
42
|
|
|
* @since 0.4 |
43
|
|
|
* |
44
|
|
|
* @var string|bool |
45
|
|
|
*/ |
46
|
|
|
public $element; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $message |
50
|
|
|
* @param integer $severity |
51
|
|
|
* @param string|bool $element |
52
|
|
|
* @param string[] $tags |
53
|
|
|
*/ |
54
|
35 |
|
public function __construct( string $message, int $severity = self::SEVERITY_NORMAL, $element = false, array $tags = [] ) { |
55
|
35 |
|
$this->message = $message; |
56
|
35 |
|
$this->severity = $severity; |
57
|
35 |
|
$this->element = $element; |
58
|
35 |
|
$this->tags = $tags; |
59
|
35 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds one or more tags. |
63
|
|
|
* |
64
|
|
|
* @since 0.4.1 |
65
|
|
|
* |
66
|
|
|
* @param string|string[] $criteria |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function addTags() { |
69
|
|
|
$args = func_get_args(); |
70
|
|
|
$this->tags = array_merge( $this->tags, is_array( $args[0] ) ? $args[0] : $args ); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getMessage(): string { |
74
|
|
|
return $this->message; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the element this error occurred at, or 'unknown' when i's unknown. |
79
|
|
|
*/ |
80
|
17 |
|
public function getElement(): string { |
81
|
17 |
|
return ( $this->element === false || $this->element === '' ) ? 'unknown' : $this->element; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the severity of the error. |
86
|
|
|
* @return integer Element of the self::SEVERITY_ enum |
87
|
|
|
*/ |
88
|
|
|
public function getSeverity(): int { |
89
|
|
|
return $this->severity; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns if the severity is equal to or bigger then the provided one. |
94
|
|
|
*/ |
95
|
35 |
|
public function hasSeverity( int $severity ): bool { |
96
|
35 |
|
return $this->severity >= $severity; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns if the error has a certain tag. |
101
|
|
|
*/ |
102
|
|
|
public function hasTag( string $tag ): bool { |
103
|
|
|
return in_array( $tag, $this->tags ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string[] |
108
|
|
|
*/ |
109
|
|
|
public function getTags(): array { |
110
|
|
|
return $this->tags; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the action associated with the errors severity. |
115
|
|
|
* |
116
|
|
|
* @return integer Element of the self::ACTION_ enum |
117
|
|
|
* @throws \Exception |
118
|
|
|
*/ |
119
|
|
|
public function getAction(): int { |
120
|
|
|
// TODO: as option |
121
|
|
|
$errorActions = [ |
122
|
|
|
self::SEVERITY_MINOR => self::ACTION_LOG, |
123
|
|
|
self::SEVERITY_LOW => self::ACTION_WARN, |
124
|
|
|
self::SEVERITY_NORMAL => self::ACTION_SHOW, |
125
|
|
|
self::SEVERITY_HIGH => self::ACTION_DEMAND, |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
if ( $this->severity === self::SEVERITY_FATAL ) { |
129
|
|
|
// This action should not be configurable, as lowering it would break in the Validator class. |
130
|
|
|
return self::ACTION_DEMAND; |
131
|
|
|
} |
132
|
|
|
elseif ( array_key_exists( $this->severity, $errorActions ) ) { |
133
|
|
|
return $errorActions[$this->severity]; |
134
|
|
|
} |
135
|
|
|
else { |
136
|
|
|
throw new \Exception( "No action associated with error severity '$this->severity'" ); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns if the action associated with the severity is equal to or bigger then the provided one. |
142
|
|
|
*/ |
143
|
|
|
public function hasAction( int $action ): bool { |
144
|
|
|
return $this->getAction() >= $action; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns if the error is fatal. |
149
|
|
|
*/ |
150
|
35 |
|
public function isFatal(): bool { |
151
|
35 |
|
return $this->hasSeverity( self::SEVERITY_FATAL ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns if the error should be logged. |
156
|
|
|
*/ |
157
|
|
|
public function shouldLog(): bool { |
158
|
|
|
return $this->hasAction( self::ACTION_LOG ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns if there should be a warning that errors are present. |
163
|
|
|
*/ |
164
|
|
|
public function shouldWarn(): bool { |
165
|
|
|
return $this->hasAction( self::ACTION_WARN ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns if the error message should be shown. |
170
|
|
|
*/ |
171
|
|
|
public function shouldShow(): bool { |
172
|
|
|
return $this->hasAction( self::ACTION_SHOW ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns if the error message should be shown, and the output not be rendered. |
177
|
|
|
*/ |
178
|
|
|
public function shouldDemand(): bool { |
179
|
|
|
return $this->hasAction( self::ACTION_DEMAND ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.