|
5 | 5 | * @author Brad Kent <bkfake-github@yahoo.com> |
6 | 6 | * @license http://opensource.org/licenses/MIT MIT |
7 | 7 | * @copyright 2014-2022 Brad Kent |
8 | | - * @version v3.4 |
| 8 | + * @version v3.2 |
9 | 9 | */ |
10 | 10 |
|
11 | 11 | namespace bdk\ErrorHandler; |
@@ -102,18 +102,9 @@ class Error extends Event |
102 | 102 | */ |
103 | 103 | public function __construct(ErrorHandler $errHandler, array $values) |
104 | 104 | { |
105 | | - $this->assertValues($values); |
106 | 105 | $this->subject = $errHandler; |
107 | | - $this->values = \array_merge( |
108 | | - $this->values, |
109 | | - $this->defaultValues($values), |
110 | | - $values, |
111 | | - array( |
112 | | - 'message' => $this->isHtml() |
113 | | - ? \str_replace('<a ', '<a target="phpRef" ', $this->values['message']) |
114 | | - : $this->values['message'], |
115 | | - ) |
116 | | - ); |
| 106 | + $this->assertValues($values); |
| 107 | + $this->setValues($values); |
117 | 108 | unset($this->values['vars']['GLOBALS']); |
118 | 109 | $errorCaller = $errHandler->get('errorCaller'); |
119 | 110 | if ($errorCaller) { |
@@ -265,6 +256,38 @@ public function &offsetGet($key) |
265 | 256 | return parent::offsetGet($key); |
266 | 257 | } |
267 | 258 |
|
| 259 | + /** |
| 260 | + * {@inheritDoc} |
| 261 | + */ |
| 262 | + public function setValues(array $values = array()) |
| 263 | + { |
| 264 | + $this->setValuesInit($values); |
| 265 | + $errType = $values['type']; |
| 266 | + $hash = $this->hash($values); |
| 267 | + $prevOccurance = $this->subject->get('error', $hash); |
| 268 | + $isSuppressed = $this->isSuppressed($prevOccurance); |
| 269 | + $this->values = \array_merge( |
| 270 | + $this->values, |
| 271 | + array( |
| 272 | + 'continueToNormal' => $this->setContinueToNormal($isSuppressed, $prevOccurance === null), |
| 273 | + 'continueToPrevHandler' => $this->subject->getCfg('continueToPrevHandler'), |
| 274 | + 'throw' => $this->isFatal() === false && ($errType & $this->subject->getCfg('errorThrow')) === $errType, |
| 275 | + ), |
| 276 | + $values, |
| 277 | + array( |
| 278 | + 'category' => $this->values['category'], |
| 279 | + 'hash' => $hash, |
| 280 | + 'isFirstOccur' => !$prevOccurance, |
| 281 | + 'isHtml' => $this->isHtml(), |
| 282 | + 'isSuppressed' => $isSuppressed, |
| 283 | + 'typeStr' => self::$errTypes[$errType], |
| 284 | + 'message' => $this->isHtml() |
| 285 | + ? \str_replace('<a ', '<a target="phpRef" ', $this->values['message']) |
| 286 | + : $this->values['message'], |
| 287 | + ) |
| 288 | + ); |
| 289 | + } |
| 290 | + |
268 | 291 | /** |
269 | 292 | * Get human-friendly error type |
270 | 293 | * |
@@ -293,47 +316,17 @@ private function assertValues($values) |
293 | 316 | $keysMustHave = array('type', 'message', 'file', 'line'); |
294 | 317 | $keys = \array_keys($values); |
295 | 318 | if (\array_intersect($keysMustHave, $keys) !== $keysMustHave) { |
296 | | - throw new InvalidArgumentException('Error values must include: ' . \implode(', ', $keysMustHave)); |
| 319 | + throw new InvalidArgumentException('Error values must include: type, message, file, & line'); |
297 | 320 | } |
298 | | - if (\array_key_exists($values['type'], self::$errTypes) === false) { |
| 321 | + $validTypes = \array_diff_key(self::$errTypes, \array_flip(array(E_ALL))); |
| 322 | + if (\array_key_exists($values['type'], $validTypes) === false) { |
299 | 323 | throw new InvalidArgumentException('invalid error type specified'); |
300 | 324 | } |
301 | 325 | if (\array_key_exists('vars', $values) && \is_array($values['vars']) === false) { |
302 | 326 | throw new InvalidArgumentException('Error vars must be an array'); |
303 | 327 | } |
304 | 328 | } |
305 | 329 |
|
306 | | - /** |
307 | | - * Get default values |
308 | | - * |
309 | | - * @param array $values Initial / primary values |
310 | | - * |
311 | | - * @return array |
312 | | - */ |
313 | | - private function defaultValues($values) |
314 | | - { |
315 | | - $this->values = \array_merge( |
316 | | - $this->values, |
317 | | - $values |
318 | | - ); |
319 | | - $errType = $this->values['type']; |
320 | | - $hash = $this->hash(); |
321 | | - $prevOccurance = $this->subject->get('error', $hash); |
322 | | - $isSuppressed = $this->isSuppressed($errType, $prevOccurance); |
323 | | - return array( |
324 | | - 'category' => $this->getCategory($errType), |
325 | | - 'continueToNormal' => $this->setContinueToNormal($isSuppressed, $prevOccurance === null), |
326 | | - 'continueToPrevHandler' => $this->subject->getCfg('continueToPrevHandler'), |
327 | | - 'exception' => $this->subject->get('uncaughtException'), // non-null if error is uncaught-exception |
328 | | - 'hash' => $hash, |
329 | | - 'isFirstOccur' => !$prevOccurance, |
330 | | - 'isHtml' => $this->isHtml(), |
331 | | - 'isSuppressed' => $isSuppressed, |
332 | | - 'throw' => $this->isFatal() === false && ($errType & $this->subject->getCfg('errorThrow')) === $errType, |
333 | | - 'typeStr' => self::$errTypes[$errType], |
334 | | - ); |
335 | | - } |
336 | | - |
337 | 330 | /** |
338 | 331 | * Generate hash used to uniquely identify this error |
339 | 332 | * |
@@ -389,17 +382,17 @@ private function isHtml() |
389 | 382 | /** |
390 | 383 | * Get initial `isSuppressed` value |
391 | 384 | * |
392 | | - * @param int $errType The level of the error |
393 | 385 | * @param self|null $prevOccurance previous occurrence of current error |
394 | 386 | * |
395 | 387 | * @return bool |
396 | 388 | */ |
397 | | - private function isSuppressed($errType, self $prevOccurance = null) |
| 389 | + private function isSuppressed($prevOccurance = null) |
398 | 390 | { |
399 | 391 | if ($prevOccurance && !$prevOccurance['isSuppressed']) { |
400 | 392 | // if any instance of this error was not supprssed, reflect that |
401 | 393 | return false; |
402 | 394 | } |
| 395 | + $errType = $this->values['type']; |
403 | 396 | if (($this->subject->getCfg('suppressNever') & $errType) === $errType) { |
404 | 397 | // never suppress tyis type |
405 | 398 | return false; |
@@ -432,4 +425,25 @@ private function setContinueToNormal($isSuppressed, $isFirstOccurance) |
432 | 425 | } |
433 | 426 | return $continueToNormal; |
434 | 427 | } |
| 428 | + |
| 429 | + /** |
| 430 | + * Set the core values (type, message, file, line) |
| 431 | + * |
| 432 | + * @param array $values values being set |
| 433 | + * |
| 434 | + * @return void |
| 435 | + */ |
| 436 | + private function setValuesInit($values) |
| 437 | + { |
| 438 | + $errType = $values['type']; |
| 439 | + $category = $this->getCategory($errType); |
| 440 | + $this->values = \array_merge( |
| 441 | + $this->values, |
| 442 | + array( |
| 443 | + 'category' => $category, |
| 444 | + 'exception' => $this->subject->get('uncaughtException'), // non-null if error is uncaught-exception |
| 445 | + ), |
| 446 | + $values |
| 447 | + ); |
| 448 | + } |
435 | 449 | } |
0 commit comments