Skip to content

Commit 09485e3

Browse files
authored
Merge pull request #7 from MaplePHP/develop
v1.3.0 Maintainability
2 parents 0666c5a + 74eb5a3 commit 09485e3

30 files changed

+1191
-738
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.DS_Store
33
.sass-cache
44
composer.lock
5-
vendor
5+
vendor
6+
.idea/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Instead of letting PHP handle the excluded severities, you can redirect them to
7474
```php
7575
$run = new Run(new HtmlHandler());
7676
$run->severity()
77-
->excludeSeverityLevels([E_WARNING, E_USER_WARNING])
77+
->excludeSeverityLevels([E_DEPRECATED, E_USER_DEPRECATED])
7878
->redirectTo(function ($errNo, $errStr, $errFile, $errLine) {
7979
error_log("Custom log: $errStr in $errFile on line $errLine");
8080
return true; // Suppresses output for excluded severities

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "maplephp/blunder",
33
"type": "library",
4-
"version": "v1.2.1",
54
"description": "Blunder is a well-designed PHP error handling framework.",
65
"keywords": [
76
"php",
@@ -27,12 +26,12 @@
2726
}
2827
],
2928
"require": {
30-
"php": ">=8.0",
31-
"maplephp/http": "^1.0",
32-
"maplephp/prompts": "^1.0"
29+
"php": ">=8.2",
30+
"maplephp/http": "^2.0",
31+
"maplephp/prompts": "^1.2"
3332
},
3433
"require-dev": {
35-
"maplephp/unitary": "^1.0"
34+
"maplephp/unitary": "^2.0"
3635
},
3736
"autoload": {
3837
"psr-4": {

src/BlunderErrorException.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Enums/BlunderErrorType.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)