Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecation of the E_STRICT constant in PHP 8.4 #5956

Closed
derrabus opened this issue Sep 19, 2024 · 6 comments
Closed

Deprecation of the E_STRICT constant in PHP 8.4 #5956

derrabus opened this issue Sep 19, 2024 · 6 comments
Assignees
Labels
type/change-in-php-requires-adaptation A change in PHP requires a change so that existing PHPUnit functionality continues to work version/8 Something affects PHPUnit 8 version/9 Something affects PHPUnit 9 version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11

Comments

@derrabus
Copy link
Contributor

Q A
PHPUnit version 9.6.20, but the 8.5 branch seems to be affected too.
PHP version 8.4-dev
Installation Method Composer

Summary

PHP 8.4 triggers a deprecation if we access the E_STRICT constant.

https://wiki.php.net/rfc/deprecations_php_8_4#remove_e_strict_error_level_and_deprecate_e_strict_constant

That error code is obsolete since PHP 7.0, but some core extensions did still use it in rare cases until PHP 7.4. The deprecation prepares a possible future removal of said constant.

The constant is used inside a switch block that triggers different behavior depending on the error level. This means that even if no E_STRICT errors are triggered, we get a deprecation notice because PHPUnit accesses a deprecated constant for comparison.

I'd volunteer to work on a fix, but I'm not sure about the way forward. Not handling the E_STRICT level at all feels wrong because as long as the level exists, some custom PHP extension could still trigger it. We could just use the plain value of the constant for comparison, assuming that the constant's value will never be changed or repurposed in future PHP releases.

Current behavior

PHP Deprecated:  Constant E_STRICT is deprecated in /path/to/phpunit/src/Util/ErrorHandler.php on line 99

How to reproduce

Run a test that triggers PHPUnit's error handler.

Expected behavior

No deprecation is triggered.

@derrabus derrabus added the type/change-in-php-requires-adaptation A change in PHP requires a change so that existing PHPUnit functionality continues to work label Sep 19, 2024
@sebastianbergmann sebastianbergmann added version/9 Something affects PHPUnit 9 version/10 Something affects PHPUnit 10 version/8 Something affects PHPUnit 8 version/11 Something affects PHPUnit 11 labels Sep 19, 2024
@sebastianbergmann
Copy link
Owner

PHPUnit 10 and PHPUnit 11 also use this constant.

@derrabus
Copy link
Contributor Author

For reference: php/php-src#13053

@sebastianbergmann sebastianbergmann changed the title Deprecation of the E_STRICT constant Deprecation of the E_STRICT constant in PHP 8.4 Sep 19, 2024
@sebastianbergmann
Copy link
Owner

Could it be this simple?

diff --git a/src/Runner/ErrorHandler.php b/src/Runner/ErrorHandler.php
index 9e51f98ba..34ff5f0a2 100644
--- a/src/Runner/ErrorHandler.php
+++ b/src/Runner/ErrorHandler.php
@@ -19,7 +19,6 @@
 use const E_NOTICE;
 use const E_PARSE;
 use const E_RECOVERABLE_ERROR;
-use const E_STRICT;
 use const E_USER_DEPRECATED;
 use const E_USER_ERROR;
 use const E_USER_NOTICE;
@@ -27,7 +26,9 @@
 use const E_WARNING;
 use function array_keys;
 use function array_values;
+use function constant;
 use function debug_backtrace;
+use function defined;
 use function error_reporting;
 use function restore_error_handler;
 use function set_error_handler;
@@ -90,9 +91,12 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
         $ignoredByBaseline = $this->ignoredByBaseline($errorFile, $errorLine, $errorString);
         $ignoredByTest     = $test->metadata()->isIgnoreDeprecations()->isNotEmpty();

+        if (defined('E_STRICT') && $errorNumber === constant('E_STRICT')) {
+            $errorNumber = E_NOTICE;
+        }
+
         switch ($errorNumber) {
             case E_NOTICE:
-            case E_STRICT:
                 Event\Facade::emitter()->testTriggeredPhpNotice(
                     $test,
                     $errorString,

As I am currently travelling, I do not have access to a build of PHP 8.4-dev with php/php-src#13053. Therefore I cannot check whether defined('E_STRICT') and/or constant('E_STRICT') also trigger the deprecation. But as these are function calls, we should be able to suppress any E_DEPRECATED raised by these using the @ operator.

@sebastianbergmann
Copy link
Owner

@mvorisek Do you have any insight? Thanks!

@mvorisek
Copy link
Contributor

I support the patch above but @ is needed before constant('E_STRICT') - https://3v4l.org/qIUg9/rfc#vgit.master

you can also simplify @constant('E_STRICT') into @E_STRICT - https://3v4l.org/B60NO/rfc#vgit.master

@derrabus
Copy link
Contributor Author

That was fast, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/change-in-php-requires-adaptation A change in PHP requires a change so that existing PHPUnit functionality continues to work version/8 Something affects PHPUnit 8 version/9 Something affects PHPUnit 9 version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11
Projects
None yet
Development

No branches or pull requests

3 participants