style: auto-fix 1340 phpcs PSR-12 violations via phpcbf; exclude MissingNamespace and SideEffects
This commit is contained in:
+27
-13
@@ -1,11 +1,13 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Centralized Error Handler
|
||||
*
|
||||
* Provides consistent error handling, logging, and response formatting
|
||||
* across the application.
|
||||
*/
|
||||
class ErrorHandler {
|
||||
class ErrorHandler
|
||||
{
|
||||
private static ?string $logFile = null;
|
||||
private static bool $initialized = false;
|
||||
|
||||
@@ -14,7 +16,8 @@ class ErrorHandler {
|
||||
*
|
||||
* @param bool $displayErrors Whether to display errors (false in production)
|
||||
*/
|
||||
public static function init(bool $displayErrors = false): void {
|
||||
public static function init(bool $displayErrors = false): void
|
||||
{
|
||||
if (self::$initialized) {
|
||||
return;
|
||||
}
|
||||
@@ -45,7 +48,8 @@ class ErrorHandler {
|
||||
* @param int $errline Line number
|
||||
* @return bool
|
||||
*/
|
||||
public static function handleError(int $errno, string $errstr, string $errfile, int $errline): bool {
|
||||
public static function handleError(int $errno, string $errstr, string $errfile, int $errline): bool
|
||||
{
|
||||
// Don't handle suppressed errors
|
||||
if (!(error_reporting() & $errno)) {
|
||||
return false;
|
||||
@@ -69,7 +73,8 @@ class ErrorHandler {
|
||||
*
|
||||
* @param Throwable $exception
|
||||
*/
|
||||
public static function handleException(Throwable $exception): void {
|
||||
public static function handleException(Throwable $exception): void
|
||||
{
|
||||
$message = sprintf(
|
||||
"Uncaught %s: %s in %s on line %d\nStack trace:\n%s",
|
||||
get_class($exception),
|
||||
@@ -94,7 +99,8 @@ class ErrorHandler {
|
||||
/**
|
||||
* Handle fatal errors on shutdown
|
||||
*/
|
||||
public static function handleShutdown(): void {
|
||||
public static function handleShutdown(): void
|
||||
{
|
||||
$error = error_get_last();
|
||||
|
||||
if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
|
||||
@@ -120,7 +126,8 @@ class ErrorHandler {
|
||||
* @param int $level Error level
|
||||
* @param array $context Additional context
|
||||
*/
|
||||
public static function log(string $message, int $level = E_USER_NOTICE, array $context = []): void {
|
||||
public static function log(string $message, int $level = E_USER_NOTICE, array $context = []): void
|
||||
{
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
$levelName = self::getErrorTypeName($level);
|
||||
|
||||
@@ -140,7 +147,8 @@ class ErrorHandler {
|
||||
* @param int $httpCode HTTP status code
|
||||
* @param Throwable|null $exception Original exception (for debug info)
|
||||
*/
|
||||
public static function sendErrorResponse(string $message, int $httpCode = 500, ?Throwable $exception = null): void {
|
||||
public static function sendErrorResponse(string $message, int $httpCode = 500, ?Throwable $exception = null): void
|
||||
{
|
||||
http_response_code($httpCode);
|
||||
|
||||
if (!headers_sent()) {
|
||||
@@ -172,7 +180,8 @@ class ErrorHandler {
|
||||
* @param array $errors Array of validation errors
|
||||
* @param string $message Overall error message
|
||||
*/
|
||||
public static function sendValidationError(array $errors, string $message = 'Validation failed'): void {
|
||||
public static function sendValidationError(array $errors, string $message = 'Validation failed'): void
|
||||
{
|
||||
http_response_code(422);
|
||||
|
||||
if (!headers_sent()) {
|
||||
@@ -192,7 +201,8 @@ class ErrorHandler {
|
||||
*
|
||||
* @param string $message Error message
|
||||
*/
|
||||
public static function sendNotFoundError(string $message = 'Resource not found'): void {
|
||||
public static function sendNotFoundError(string $message = 'Resource not found'): void
|
||||
{
|
||||
self::sendErrorResponse($message, 404);
|
||||
}
|
||||
|
||||
@@ -201,7 +211,8 @@ class ErrorHandler {
|
||||
*
|
||||
* @param string $message Error message
|
||||
*/
|
||||
public static function sendUnauthorizedError(string $message = 'Authentication required'): void {
|
||||
public static function sendUnauthorizedError(string $message = 'Authentication required'): void
|
||||
{
|
||||
self::sendErrorResponse($message, 401);
|
||||
}
|
||||
|
||||
@@ -210,7 +221,8 @@ class ErrorHandler {
|
||||
*
|
||||
* @param string $message Error message
|
||||
*/
|
||||
public static function sendForbiddenError(string $message = 'Access denied'): void {
|
||||
public static function sendForbiddenError(string $message = 'Access denied'): void
|
||||
{
|
||||
self::sendErrorResponse($message, 403);
|
||||
}
|
||||
|
||||
@@ -220,7 +232,8 @@ class ErrorHandler {
|
||||
* @param int $errno Error number
|
||||
* @return string Error type name
|
||||
*/
|
||||
private static function getErrorTypeName(int $errno): string {
|
||||
private static function getErrorTypeName(int $errno): string
|
||||
{
|
||||
$types = [
|
||||
E_ERROR => 'ERROR',
|
||||
E_WARNING => 'WARNING',
|
||||
@@ -248,7 +261,8 @@ class ErrorHandler {
|
||||
* @param int $lines Number of lines to return
|
||||
* @return array Log entries
|
||||
*/
|
||||
public static function getRecentErrors(int $lines = 50): array {
|
||||
public static function getRecentErrors(int $lines = 50): array
|
||||
{
|
||||
if (self::$logFile === null || !file_exists(self::$logFile)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user