Completed
Push — master ( cb0578...28965e )
by Jeroen De
07:11 queued 04:27
created

src/ProcessingError.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 21
	public function __construct( $message, $severity = self::SEVERITY_NORMAL, $element = false, array $tags = [] ) {
55 21
		$this->message = $message;
56 21
		$this->severity = $severity;
57 21
		$this->element = $element;
58 21
		$this->tags = $tags;
59 21
	}
60
61
	/**
62
	 * Adds one or more tags.
63
	 *
64
	 * @since 0.4.1
65
	 *
66
	 * @param string|string[] $criteria
0 ignored issues
show
There is no parameter named $criteria. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
	 */
68
	public function addTags() {
69
		$args = func_get_args();
70
		$this->tags = array_merge( $this->tags, is_array( $args[0] ) ? $args[0] : $args );
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->tags,...0]) ? $args[0] : $args) of type array is incompatible with the declared type array<integer,string> of property $tags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
	}
72
73
	/**
74
	 * Returns the error message describing the error.
75
	 *
76
	 * @since 0.4
77
	 *
78
	 * @return string
79
	 */
80
	public function getMessage() {
81
		return $this->message;
82
	}
83
84
	/**
85
	 * Returns the element this error occurred at, or 'unknown' when i's unknown.
86
	 *
87
	 * @since 0.4
88
	 *
89
	 * @return string
90
	 */
91
	public function getElement() {
92
		return $this->element === false ? 'unknown' : $this->element;
93
	}
94
95
	/**
96
	 * Returns the severity of the error.
97
	 *
98
	 * @since 0.4
99
	 *
100
	 * @return integer Element of the ProcessingError::SEVERITY_ enum
101
	 */
102
	public function getSeverity() {
103
		return $this->severity;
104
	}
105
106
	/**
107
	 * Returns if the severity is equal to or bigger then the provided one.
108
	 *
109
	 * @since 0.4
110
	 *
111
	 * @param integer $severity
112
	 *
113
	 * @return boolean
114
	 */
115 21
	public function hasSeverity( $severity ) {
116 21
		return $this->severity >= $severity;
117
	}
118
119
	/**
120
	 * Returns if the error has a certain tag.
121
	 *
122
	 * @since 0.4.1
123
	 *
124
	 * @param string $tag
125
	 *
126
	 * @return boolean
127
	 */
128
	public function hasTag( $tag ) {
129
		return in_array( $tag, $this->tags );
130
	}
131
132
	/**
133
	 * Returns the tags.
134
	 *
135
	 * @since 0.4.1
136
	 *
137
	 * @return array
138
	 */
139
	public function getTags() {
140
		return $this->tags;
141
	}
142
143
	/**
144
	 * Returns the action associated with the errors severity.
145
	 *
146
	 * @since 0.4
147
	 *
148
	 * @return integer Element of the ProcessingError::ACTION_ enum
149
	 * @throws \Exception
150
	 */
151
	public function getAction() {
152
		// TODO: as option
153
		$errorActions = [
154
			ProcessingError::SEVERITY_MINOR => ProcessingError::ACTION_LOG,
155
			ProcessingError::SEVERITY_LOW => ProcessingError::ACTION_WARN,
156
			ProcessingError::SEVERITY_NORMAL => ProcessingError::ACTION_SHOW,
157
			ProcessingError::SEVERITY_HIGH => ProcessingError::ACTION_DEMAND,
158
		];
159
160
		if ( $this->severity === self::SEVERITY_FATAL ) {
161
			// This action should not be configurable, as lowering it would break in the Validator class.
162
			return self::ACTION_DEMAND;
163
		}
164
		elseif ( array_key_exists( $this->severity, $errorActions ) ) {
165
			return $errorActions[$this->severity];
166
		}
167
		else {
168
			throw new \Exception( "No action associated with error severity '$this->severity'" );
169
		}
170
	}
171
172
	/**
173
	 * Returns if the action associated with the severity is equal to or bigger then the provided one.
174
	 *
175
	 * @since 0.4
176
	 *
177
	 * @param int $action
178
	 *
179
	 * @return boolean
180
	 */
181
	public function hasAction( $action ) {
182
		return $this->getAction() >= $action;
183
	}
184
185
	/**
186
	 * Returns if the error is fatal.
187
	 *
188
	 * @since 0.4
189
	 *
190
	 * @return boolean
191
	 */
192 21
	public function isFatal() {
193 21
		return $this->hasSeverity( self::SEVERITY_FATAL );
194
	}
195
196
	/**
197
	 * Returns if the error should be logged.
198
	 *
199
	 * @since 0.4
200
	 *
201
	 * @return boolean
202
	 */
203
	public function shouldLog() {
204
		return $this->hasAction( self::ACTION_LOG );
205
	}
206
207
	/**
208
	 * Returns if there should be a warning that errors are present.
209
	 *
210
	 * @since 0.4
211
	 *
212
	 * @return boolean
213
	 */
214
	public function shouldWarn() {
215
		return $this->hasAction( self::ACTION_WARN );
216
	}
217
218
	/**
219
	 * Returns if the error message should be shown.
220
	 *
221
	 * @since 0.4
222
	 *
223
	 * @return boolean
224
	 */
225
	public function shouldShow() {
226
		return $this->hasAction( self::ACTION_SHOW );
227
	}
228
229
	/**
230
	 * Returns if the error message should be shown, and the output not be rendered.
231
	 *
232
	 * @since 0.4
233
	 *
234
	 * @return boolean
235
	 */
236
	public function shouldDemand() {
237
		return $this->hasAction( self::ACTION_DEMAND );
238
	}
239
240
}
241