Skip to content
Advertisement

PHP: how to make startup warnings fatal?

I try to make a verification process for my PHP preloading script.

In order to ensure that I don’t have un-managed classes I’m running a script in CLI like:

     php 
        -d error_reporting=2147483647 
        -d opcache.enable_cli=1 
        -d display_errors=1 
        -d display_startup_errors=1 
        -d memory_limit=96M 
        -d opcache.preload=preload.php 
         tools/utils/preload/check-preload.php

I’m getting a warning because one of the class cannot be complied du to missing dependencies. E.g.:

Warning: Can't preload unlinked class UserProfileForceRegenerationDefaultAvatarCommand: Unknown parent SymfonyComponentConsoleCommandCommand in /home/mvacelet/workspace/tuleap/src/common/User/Profile/ForceRegenerationDefaultAvatarCommand.php on line 32

That’s great but the issue I have is that I cannot make this warning fatal. More specifically I would like check-preload.php script to exit with status code different of 0 so my CI can fail.

I cannot find a relevant configuration setting and custom error_handler will not work with startup errors.

Advertisement

Answer

Actually the information I was missing is that the custom error handler must be in the preload file.

As this was mainly for dev/CI purpose to be sure the preload generated by new development will not generate warnings, I went a little bit more subtle:

I now have a verify-preload.php file that does:

    set_error_handler(static fn ($errno, $errstr, $errfile, $errline) => die("$errstr $errfile L$errlinen"), E_ALL);

    require __DIR__ . '/preload.php';

and my verification call is:

     php 
        -d error_reporting=2147483647 
        -d opcache.enable_cli=1 
        -d display_errors=1 
        -d display_startup_errors=1 
        -d memory_limit=96M 
        -d opcache.preload=verify-preload.php 
         tools/utils/preload/check-preload.php

This way:

  • During development or CI, I rely on verify-preload.php so errors are caught.
  • In production, I don’t take the risk of making fatal an harmless warning (remember in this context, warnings are mainly informative that given file will not be preloaded, it doesn’t have any impact on production).
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement