|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Enum BlunderErrorType |
| 5 | + * |
| 6 | + * Defines all supported PHP error types in a structured, strongly-typed way. |
| 7 | + * Each enum case maps to a PHP error constant, including its numeric value, |
| 8 | + * name (e.g. E_WARNING), and a user-friendly title. |
| 9 | + * |
| 10 | + * Provides utility methods for retrieving metadata, matching error codes to enum cases, |
| 11 | + * and listing all known error levels for use in severity filtering and error reporting. |
| 12 | + * |
| 13 | + * @package MaplePHP\Blunder\Enums |
| 14 | + * @author Daniel Ronkainen |
| 15 | + * @license Apache-2.0 license, Copyright © Daniel Ronkainen |
| 16 | + * Don't delete this comment, it's part of the license. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace MaplePHP\Blunder\Enums; |
| 20 | + |
| 21 | +enum BlunderErrorType |
| 22 | +{ |
| 23 | + case FALLBACK; |
| 24 | + case ERROR; |
| 25 | + case WARNING; |
| 26 | + case PARSE; |
| 27 | + case NOTICE; |
| 28 | + case CORE_ERROR; |
| 29 | + case CORE_WARNING; |
| 30 | + case COMPILE_ERROR; |
| 31 | + case COMPILE_WARNING; |
| 32 | + case USER_ERROR; |
| 33 | + case USER_WARNING; |
| 34 | + case USER_NOTICE; |
| 35 | + case RECOVERABLE_ERROR; |
| 36 | + case DEPRECATED; |
| 37 | + case USER_DEPRECATED; |
| 38 | + |
| 39 | + /** |
| 40 | + * A single source of truth for constant value, constant name, and title. |
| 41 | + * |
| 42 | + * @var array<string, array{int, string, string}> |
| 43 | + */ |
| 44 | + private const MAP = [ |
| 45 | + 'FALLBACK' => [0, 'E_USER_ERROR', 'Fallback error'], |
| 46 | + 'ERROR' => [E_ERROR, 'E_ERROR', 'Fatal error'], |
| 47 | + 'WARNING' => [E_WARNING, 'E_WARNING', 'Warning'], |
| 48 | + 'PARSE' => [E_PARSE, 'E_PARSE', 'Parse error'], |
| 49 | + 'NOTICE' => [E_NOTICE, 'E_NOTICE', 'Notice'], |
| 50 | + 'CORE_ERROR' => [E_CORE_ERROR, 'E_CORE_ERROR', 'Core fatal error'], |
| 51 | + 'CORE_WARNING' => [E_CORE_WARNING, 'E_CORE_WARNING', 'Core warning'], |
| 52 | + 'COMPILE_ERROR' => [E_COMPILE_ERROR, 'E_COMPILE_ERROR', 'Compile-time fatal error'], |
| 53 | + 'COMPILE_WARNING' => [E_COMPILE_WARNING, 'E_COMPILE_WARNING', 'Compile-time warning'], |
| 54 | + 'USER_ERROR' => [E_USER_ERROR, 'E_USER_ERROR', 'User fatal error'], |
| 55 | + 'USER_WARNING' => [E_USER_WARNING, 'E_USER_WARNING', 'User warning'], |
| 56 | + 'USER_NOTICE' => [E_USER_NOTICE, 'E_USER_NOTICE', 'User notice'], |
| 57 | + 'RECOVERABLE_ERROR' => [E_RECOVERABLE_ERROR, 'E_RECOVERABLE_ERROR', 'Recoverable fatal error'], |
| 58 | + 'DEPRECATED' => [E_DEPRECATED, 'E_DEPRECATED', 'Deprecated notice'], |
| 59 | + 'USER_DEPRECATED' => [E_USER_DEPRECATED, 'E_USER_DEPRECATED', 'User deprecated notice'], |
| 60 | + 'E_ALL' => [E_ALL, 'E_ALL', 'All'], |
| 61 | + ]; |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Retrieve all PHP constant values that are mapped, either preserving the original keys or as a flat list. |
| 66 | + * |
| 67 | + * @param bool $preserveKeys If true, retains the original keys. Otherwise, returns a flat indexed array. |
| 68 | + * @return array<int> List of PHP constant values. |
| 69 | + */ |
| 70 | + public static function getAllErrorLevels(bool $preserveKeys = false): array |
| 71 | + { |
| 72 | + $items = self::MAP; |
| 73 | + array_shift($items); |
| 74 | + $arr = array_map(fn ($item) => $item[0], $items); |
| 75 | + if ($preserveKeys) { |
| 76 | + return $arr; |
| 77 | + } |
| 78 | + return array_values($arr); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get the PHP constant value associated with this error type. |
| 83 | + * |
| 84 | + * @return int The constant value corresponding to the enum case. |
| 85 | + */ |
| 86 | + public function getErrorLevel(): int |
| 87 | + { |
| 88 | + return self::MAP[$this->name][0]; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the PHP constant key (name) associated with this error type. |
| 94 | + * |
| 95 | + * @return string The constant key corresponding to the enum case. |
| 96 | + */ |
| 97 | + public function getErrorLevelKey(): string |
| 98 | + { |
| 99 | + return self::MAP[$this->name][1]; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + /** |
| 104 | + * Get the user-friendly title for this error type. |
| 105 | + * If the error type is `FALLBACK`, a custom fallback title is returned. |
| 106 | + * |
| 107 | + * @param string $fallback Custom fallback title used if the error type is `FALLBACK`. Defaults to 'Error'. |
| 108 | + * @return string The user-friendly title associated with this error type. |
| 109 | + */ |
| 110 | + public function getErrorLevelTitle(string $fallback = 'Error'): string |
| 111 | + { |
| 112 | + return $this === self::FALLBACK ? $fallback : self::MAP[$this->name][2]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get the enum instance corresponding to the specified error number. |
| 117 | + * |
| 118 | + * If the error number does not match any predefined case, it returns the default fallback case. |
| 119 | + * |
| 120 | + * @param int $errno The error number to find the corresponding enum case for. |
| 121 | + * @return self The matching enum case, or the fallback case if no match is found. |
| 122 | + */ |
| 123 | + public static function fromErrorLevel(int $errno): self |
| 124 | + { |
| 125 | + $cases = self::cases(); |
| 126 | + foreach ($cases as $case) { |
| 127 | + if ($case->getErrorLevel() === $errno) { |
| 128 | + return $case; |
| 129 | + } |
| 130 | + } |
| 131 | + return reset($cases); |
| 132 | + } |
| 133 | +} |
0 commit comments