Skip to content

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 17, 2025

The AbstractArrayDeclarationSniff::getActualArrayKey() method collects the contents of a subset of tokens and then runs eval() on this contents to determine what the array key would be.

The code allows for "collecting" type casts tokens as safe for use in the eval(), however, it did not take into account that there have been a number of changes in the available type casts in PHP over time.

Say, the code under scan uses the (real) type cast in an array key. This type cast was deprecated in PHP 7.4 and has been removed since PHP 8.0.

While the code under scan may run on PHP < 8.0 in production, the PHPCS scan might be run on a different PHP version.

  • If the scan would be run on PHP 7.4, the scan would bork out on the deprecation notice triggered by running the code in the eval() and PHPCS catching any such PHP notices and bowing out of scanning the file if/when such notices are encountered.
  • If the scan would be run on PHP >= 8.0, the scan would bork out on a fatal error for the cast no longer being supported.

Now, what with PHP 8.5 deprecating another four type casts variants, this issue is now more relevant than ever before.

The changes made in this commit are intended to prevent such notices about deprecated/removed type casts from negatively impacting a PHPCS scan.

Includes tests.

… notices about deprecated casts

The `AbstractArrayDeclarationSniff::getActualArrayKey()` method collects the contents of a subset of tokens and then runs `eval()` on this contents to determine what the array key would be.

The code allows for "collecting" type casts tokens as safe for use in the `eval()`, however, it did not take into account that there have been a number of changes in the available type casts in PHP over time.

Say, the code under scan uses the `(real)` type cast in an array key.
This type cast was deprecated in PHP 7.4 and has been removed since PHP 8.0.

While the code under scan may run on PHP < 8.0 in production, the PHPCS scan _might_ be run on a different PHP version.
- If the scan would be run on PHP 7.4, the scan would bork out on the deprecation notice triggered by running the code in the `eval()` and PHPCS catching any such PHP notices and bowing out of scanning the file if/when such notices are encountered.
- If the scan would be run on PHP >= 8.0, the scan would bork out on a fatal error for the cast no longer being supported.

Now, what with PHP 8.5 deprecating another four type casts variants, this issue is now more relevant than ever before.

The changes made in this commit are intended to prevent such notices about deprecated/removed type casts from negatively impacting a PHPCS scan.

Includes tests.
@jrfnl
Copy link
Member Author

jrfnl commented Nov 17, 2025

Merging even though Coveralls is not currently reporting back (outage). I am aware that coverage will go down a little with this PR. That is related to the PHP version based conditions and will be "fixed" once code coverage checks will be run on PHP 8.5 (later this week).

@jrfnl jrfnl merged commit 9266ac8 into develop Nov 17, 2025
80 checks passed
@jrfnl jrfnl deleted the feature/abstractarraydeclarationsniff-getactualarraykey-prevent-deprecation-notices branch November 17, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants