Skip to content

Commit

Permalink
Catch Slim 3.x deprecated warnings on PHP 8.4
Browse files Browse the repository at this point in the history
Override error_reporting to ignore E_DEPRECATED when initializing Slim
framework to squelch initial deprecation warnings, ignore a few
additional classes in the custom error handler, then restore
error_reporting to its original state before executing the request.

Fixes #35284
  • Loading branch information
dregad committed Feb 9, 2025
1 parent 94f9bf0 commit d5a30a0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/rest/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
# For debugging purposes, uncomment this line to avoid truncated error messages
# $t_config['settings']['addContentLengthHeader'] = false;

# Disable E_DEPRECATED warnings for PHP 8.4
# Necessary to catch errors in method signatures triggered before our custom
# error handler is in place.
if( version_compare( PHP_VERSION, '8.4', '>=' ) ) {
$t_old_error_reporting = error_reporting( error_reporting() & ~E_DEPRECATED );
}

if( version_compare( Slim\App::VERSION, '4.0', '<' )
&& version_compare( PHP_VERSION, '8.1', '>=' )
Expand Down Expand Up @@ -107,6 +113,12 @@ function deprecated_errors_handler(
# Passing null to parameter of type ... is deprecated
case 'Slim/Http/Request.php':
case 'Slim/Http/Uri.php':

# Implicitly marking parameter ... as nullable is deprecated
case 'Slim/DeferredCallable.php':
case 'Slim/Router.php':
case 'Slim/RouteGroup.php':
case 'Slim/Http/Response.php':
return true;
}
}
Expand Down Expand Up @@ -183,6 +195,11 @@ function deprecated_errors_handler(

event_signal( 'EVENT_REST_API_ROUTES', array( array( 'app' => $g_app ) ) );

# Restore error reporting to its original state before executing the request
if( isset( $t_old_error_reporting ) ) {
error_reporting( $t_old_error_reporting );
}

$g_app->run();
}
catch( Throwable $e ) {
Expand Down

0 comments on commit d5a30a0

Please sign in to comment.