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 User\Profile\ForceRegenerationDefaultAvatarCommand: Unknown parent Symfony\Component\Console\Command\Command 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.
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$errline\n"), 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:
verify-preload.php
so errors are caught.