I have a library, and I didn't notice that it was giving deprecation warnings until someone created an issue on GitHub. I've enabled:
error_reporting(E_ALL);
And fixed all deprecation warnings. But the question is: can you make the code always fail (I think about unit tests) if there are deprecation warnings? I was thinking maybe about adding ob_ buffering with a regular expression to test if there are no warnings and throw an error. But this would require some code that will not look pretty.
Is there an easy way to just toggle some options to make warnings real errors? So the unit test doesn't pass and the code always fails? I want my code to be future-proof and don't fail when PHP decides to replace deprecation with real error.
Yes, you can make your own PHP script treat all (handle-able) errors, including
E_DEPRECATED/E_USER_DEPRECATEDby turning them into exceptions with your own error handler function.See PHP's
set_error_handler()function. It allows you to define a custom error handler that will be called whenever an error occurs (of levelE_ALLby default, excluding some core errors that do fatal and ignore such a handler, clearly documented on its manual page).Register it after the unit testing framework registers it's own handler or communicate with the testing framework accordingly, e.g. per configuration. See their feature list, this above is pure PHP code.