Skip to content

✨ New PHPCompatibility.Keywords.ForbiddenClassAlias sniff#1952

Merged
wimg merged 1 commit intodevelopfrom
feature/new-forbiddenclassalias-sniff
Oct 27, 2025
Merged

✨ New PHPCompatibility.Keywords.ForbiddenClassAlias sniff#1952
wimg merged 1 commit intodevelopfrom
feature/new-forbiddenclassalias-sniff

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 26, 2025

Inspired by the following change in PHP 8.5:

  • Core:
    . It is no longer possible to use "array" and "callable" as class alias names
    in class_alias().

... I've done a deep-dive into type keywords versus the class_alias() function.

Turns out "normal" reserved keywords are allowed as class aliases: https://3v4l.org/jMsDd
Don't ask me why, as using these aliases would be complicated as "normal" usages like new Name() would be a parse error, but that's not our concern. It's not forbidden by PHP itself.

However, the "other" reserved keywords are forbidden as class alias names since PHP 7.0, with new type keywords which were added in later PHP versions being forbidden as of that PHP version. https://3v4l.org/9Knt7

That is, with the exception of array and callable, which were forgotten in PHP 7.0, with that oversight being fixed now in PHP 8.5.
https://3v4l.org/P0sii#veol
https://3v4l.org/P0sii#vgit.master

This commit introduces a new sniff which detects calls to the class_alias() function with any of these forbidden names as the alias.

Includes tests.
Includes documentation.

Ref:

Related to #1849

Inspired by the following change in PHP 8.5:

> - Core:
>   . It is no longer possible to use "array" and "callable" as class alias names
>     in class_alias().

... I've done a deep-dive into type keywords versus the `class_alias()` function.

Turns out "normal" reserved keywords are allowed as class aliases: https://3v4l.org/jMsDd
Don't ask me why, as using these aliases would be complicated as "normal" usages like `new Name()` would be a parse error, but that's not our concern. It's not forbidden by PHP itself.

However, the "other" reserved keywords _are_ forbidden as class alias names since PHP 7.0, with new type keywords which were added in later PHP versions being forbidden as of that PHP version.
https://3v4l.org/9Knt7

That is, with the exception of `array` and `callable`, which were forgotten in PHP 7.0, with that oversight being fixed now in PHP 8.5.
https://3v4l.org/P0sii#veol
https://3v4l.org/P0sii#vgit.master

This commit introduces a new sniff which detects calls to the `class_alias()` function with any of these forbidden names as the alias.

Includes tests.
Includes documentation.

Ref:
* https://github.com/php/php-src/blob/18687e4f394f7efb1fc84ad5a7a6721f2774a764/UPGRADING#L22-L24
* https://github.com/php/php-src/blob/9b23fb29aba3bcd5ae5aee116fc35b103902a9fa/Zend/zend_compile.c#L202-L223
* Original commit from PHP 7.0: php/php-src@53a4038
* php/php-src 16683
* php/php-src@96d1cd0

Related to 1849
@wimg wimg merged commit 8628971 into develop Oct 27, 2025
76 checks passed
@wimg wimg deleted the feature/new-forbiddenclassalias-sniff branch October 27, 2025 20:43
@jrfnl jrfnl added the PHP: All label Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants