Skip to content

[BUGFIX] Avoid double autoloading of class aliases#1552

Merged
JakeQZ merged 4 commits into
mainfrom
bugfix/class-aliases
Mar 3, 2026
Merged

[BUGFIX] Avoid double autoloading of class aliases#1552
JakeQZ merged 4 commits into
mainfrom
bugfix/class-aliases

Conversation

@oliverklee

@oliverklee oliverklee commented Feb 27, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1546
Fixes #1557

@JakeQZ

JakeQZ commented Feb 28, 2026

Copy link
Copy Markdown
Collaborator

If this doesn't work, an alternative would be to wrap the class_alias() calls in a class_exists() check, e.g.

if (!\class_exists(Rule::class, false)) {
    /**
     * @deprecated in v9.2, will be removed in v10.0.  Use `Property\Declaration` instead, which is a direct replacement.
     */
    class_alias(Declaration::class, Rule::class);
}

I think that should work, and I'd actually prefer it:

  • It avoids introducing an additional root-level directory along with the associated configuration/command changes;
  • We can keep the aliasing files in the location of the old classes -
    • If PHPStan is updated to support recognizing class aliases without the files being specifically preloaded, we can then simply remove the autoload.files section from composer.json (the only reason the files are being preloaded is to avoid PHPStan warnings in consumer projects - [BUGFIX] Make class aliases available for consumers #1534).

@oliverklee

Copy link
Copy Markdown
Collaborator Author

I've added the wrappers now.

@coveralls

coveralls commented Feb 28, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 72.886% (-0.1%) from 73.02%
when pulling 3964955 on bugfix/class-aliases
into dfdf183 on main.

@JakeQZ

JakeQZ commented Feb 28, 2026

Copy link
Copy Markdown
Collaborator

I've added the wrappers now.

With the wrappers added, would it be possible to keep the class_alias files in their original location, so that we don't have to introduce an additional root-level directory and change the commands in composer.json?

@JakeQZ JakeQZ left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the files to a class-aliases directory does not help, as reported in #1546 (comment):

Moving the references from 'src' to 'class-aliases' is just changing where the problem happens.

I think we should keep the files in their original location, to avoid introducing a new root-level directory and potential confusion. I'm hopeful that the class_exists() test will solve the problem.

Comment thread class-aliases/Rule/Rule.php Outdated

use function Safe\class_alias;

if (!\class_exists(Rule::class) && !\interface_exists(Rule::class)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pass false as the second argument to class_exists() and interface_exists() to avoid any unwanted autoloading.

Comment thread class-aliases/RuleSet/RuleContainer.php Outdated

use function Safe\class_alias;

if (!\class_exists(RuleContainer::class) && !\interface_exists(RuleContainer::class)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pass false as the second argument to class_exists() and interface_exists() to avoid any unwanted autoloading.

@oliverklee oliverklee marked this pull request as ready for review March 3, 2026 08:47
@oliverklee oliverklee requested a review from JakeQZ March 3, 2026 08:49
@JakeQZ JakeQZ merged commit 1b395cb into main Mar 3, 2026
24 checks passed
@JakeQZ JakeQZ deleted the bugfix/class-aliases branch March 3, 2026 17:14
@JakeQZ

JakeQZ commented Mar 3, 2026

Copy link
Copy Markdown
Collaborator

We should probably make a 9.2.1 release with this fix, since there have been several reports of the issue since 9.2.0 which was only released a few days ago.

@oliverklee

Copy link
Copy Markdown
Collaborator Author

As we've added a new feature, this would then be a 9.3.0 release.

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.

Rule class is undefined, which does not let my symfony applicatino start PHP-CSS-Parser Incompatible with OPCache / Composer Autoload

4 participants