Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store
.sass-cache
composer.lock
vendor
vendor
.idea/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Instead of letting PHP handle the excluded severities, you can redirect them to
```php
$run = new Run(new HtmlHandler());
$run->severity()
->excludeSeverityLevels([E_WARNING, E_USER_WARNING])
->excludeSeverityLevels([E_DEPRECATED, E_USER_DEPRECATED])
->redirectTo(function ($errNo, $errStr, $errFile, $errLine) {
error_log("Custom log: $errStr in $errFile on line $errLine");
return true; // Suppresses output for excluded severities
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "maplephp/blunder",
"type": "library",
"version": "v1.2.1",
"description": "Blunder is a well-designed PHP error handling framework.",
"keywords": [
"php",
Expand All @@ -27,12 +26,12 @@
}
],
"require": {
"php": ">=8.0",
"maplephp/http": "^1.0",
"maplephp/prompts": "^1.0"
"php": ">=8.2",
"maplephp/http": "^2.0",
"maplephp/prompts": "^1.2"
},
"require-dev": {
"maplephp/unitary": "^1.0"
"maplephp/unitary": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
133 changes: 133 additions & 0 deletions src/Enums/BlunderErrorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/**
* Enum BlunderErrorType
*
* Defines all supported PHP error types in a structured, strongly-typed way.
* Each enum case maps to a PHP error constant, including its numeric value,
* name (e.g. E_WARNING), and a user-friendly title.
*
* Provides utility methods for retrieving metadata, matching error codes to enum cases,
* and listing all known error levels for use in severity filtering and error reporting.
*
* @package MaplePHP\Blunder\Enums
* @author Daniel Ronkainen
* @license Apache-2.0 license, Copyright © Daniel Ronkainen
* Don't delete this comment, it's part of the license.
*/

namespace MaplePHP\Blunder\Enums;

enum BlunderErrorType
{
case FALLBACK;
case ERROR;
case WARNING;
case PARSE;
case NOTICE;
case CORE_ERROR;
case CORE_WARNING;
case COMPILE_ERROR;
case COMPILE_WARNING;
case USER_ERROR;
case USER_WARNING;
case USER_NOTICE;
case RECOVERABLE_ERROR;
case DEPRECATED;
case USER_DEPRECATED;

/**
* A single source of truth for constant value, constant name, and title.
*
* @var array<string, array{int, string, string}>
*/
private const MAP = [
'FALLBACK' => [0, 'E_USER_ERROR', 'Fallback error'],
'ERROR' => [E_ERROR, 'E_ERROR', 'Fatal error'],
'WARNING' => [E_WARNING, 'E_WARNING', 'Warning'],
'PARSE' => [E_PARSE, 'E_PARSE', 'Parse error'],
'NOTICE' => [E_NOTICE, 'E_NOTICE', 'Notice'],
'CORE_ERROR' => [E_CORE_ERROR, 'E_CORE_ERROR', 'Core fatal error'],
'CORE_WARNING' => [E_CORE_WARNING, 'E_CORE_WARNING', 'Core warning'],
'COMPILE_ERROR' => [E_COMPILE_ERROR, 'E_COMPILE_ERROR', 'Compile-time fatal error'],
'COMPILE_WARNING' => [E_COMPILE_WARNING, 'E_COMPILE_WARNING', 'Compile-time warning'],
'USER_ERROR' => [E_USER_ERROR, 'E_USER_ERROR', 'User fatal error'],
'USER_WARNING' => [E_USER_WARNING, 'E_USER_WARNING', 'User warning'],
'USER_NOTICE' => [E_USER_NOTICE, 'E_USER_NOTICE', 'User notice'],
'RECOVERABLE_ERROR' => [E_RECOVERABLE_ERROR, 'E_RECOVERABLE_ERROR', 'Recoverable fatal error'],
'DEPRECATED' => [E_DEPRECATED, 'E_DEPRECATED', 'Deprecated notice'],
'USER_DEPRECATED' => [E_USER_DEPRECATED, 'E_USER_DEPRECATED', 'User deprecated notice'],
'E_ALL' => [E_ALL, 'E_ALL', 'All'],
];


/**
* Retrieve all PHP constant values that are mapped, either preserving the original keys or as a flat list.
*
* @param bool $preserveKeys If true, retains the original keys. Otherwise, returns a flat indexed array.
* @return array<int> List of PHP constant values.
*/
public static function getAllErrorLevels(bool $preserveKeys = false): array
{
$items = self::MAP;
array_shift($items);
$arr = array_map(fn ($item) => $item[0], $items);
if ($preserveKeys) {
return $arr;
}
return array_values($arr);
}

/**
* Get the PHP constant value associated with this error type.
*
* @return int The constant value corresponding to the enum case.
*/
public function getErrorLevel(): int
{
return self::MAP[$this->name][0];
}


/**
* Get the PHP constant key (name) associated with this error type.
*
* @return string The constant key corresponding to the enum case.
*/
public function getErrorLevelKey(): string
{
return self::MAP[$this->name][1];
}


/**
* Get the user-friendly title for this error type.
* If the error type is `FALLBACK`, a custom fallback title is returned.
*
* @param string $fallback Custom fallback title used if the error type is `FALLBACK`. Defaults to 'Error'.
* @return string The user-friendly title associated with this error type.
*/
public function getErrorLevelTitle(string $fallback = 'Error'): string
{
return $this === self::FALLBACK ? $fallback : self::MAP[$this->name][2];
}

/**
* Get the enum instance corresponding to the specified error number.
*
* If the error number does not match any predefined case, it returns the default fallback case.
*
* @param int $errno The error number to find the corresponding enum case for.
* @return self The matching enum case, or the fallback case if no match is found.
*/
public static function fromErrorLevel(int $errno): self
{
$cases = self::cases();
foreach ($cases as $case) {
if ($case->getErrorLevel() === $errno) {
return $case;
}
}
return reset($cases);
}
}
Loading
Loading