Skip to content

v1.3.0 Maintainability #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.2",
"maplephp/http": "^1.0",
"maplephp/prompts": "^1.0"
},
Expand Down
35 changes: 32 additions & 3 deletions src/BlunderErrorException.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
<?php

/**
* Class BlunderErrorException
*
* Extends PHP's ErrorException with support for a custom "pretty" message,
* and allows preserving original exception file and line information when
* rethrowing exceptions with modified types.
*
* Useful for enhanced exception handling, presentation, and debugging
* within the Blunder framework.
*
* @package MaplePHP\Blunder
* @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;

use Exception;
use ErrorException;
use ReflectionClass;
use Throwable;

class BlunderErrorException extends ErrorException
final class BlunderErrorException extends ErrorException
{
protected ?string $prettyMessage = null;

/*
public function __construct(
string $message = "",
int $code = 0,
int $severity = E_ERROR,
string $file = __FILE__,
int $line = __LINE__,
?Throwable $previous = null
) {
parent::__construct($message, $code, $severity, $file, $line, $previous);
}
*/

/**
* Will return the default ErrorException message
*
* @return string|null
*/
public function getPrettyMessage(): ?string
{
return (!is_null($this->prettyMessage)) ? $this->prettyMessage : $this->message;
return ($this->prettyMessage !== null) ? $this->prettyMessage : $this->message;
}

/**
* Set pretty message that can be used in execption handlers
* Set pretty message that can be used in exception handlers
*
* @param string $message
* @return void
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