Skip to content

Skip require of files with top-level side effects in RemovePsr4AutoloadedIncludeRector - #8175

Merged
TomasVotruba merged 1 commit into
rectorphp:tv-remove-dead-include-requirefrom
lll-lll-lll-lll:side-effect-guard-for-psr4-include-removal
Jul 29, 2026
Merged

Skip require of files with top-level side effects in RemovePsr4AutoloadedIncludeRector#8175
TomasVotruba merged 1 commit into
rectorphp:tv-remove-dead-include-requirefrom
lll-lll-lll-lll:side-effect-guard-for-psr4-include-removal

Conversation

@lll-lll-lll-lll

Copy link
Copy Markdown

Adds the side-effect guard discussed in #8149 (comment) (thanks @samsonasik for the catch).

resolveSingleDeclaredClassName() now bails out unless the target file's top level contains only type declarations: class-likes, use statements, statement-form declare, and comments (namespace is checked recursively). Files with top-level function/const definitions or executed statements — e.g. Cli::init(); after the class — keep their require load-bearing, since autoloading replays only the type declarations.

The guard is one extra condition in front of the existing checks, with existing code untouched, so it can only make the rule remove less, never more.

Fixtures:

  • skip_class_with_side_effect — class followed by ClassWithInit::init(); (the CodeIgniter-style case)
  • skip_class_with_function — class plus a top-level function

Both fail (the require gets removed) without the guard.

@TomasVotruba
TomasVotruba merged commit e0fbd47 into rectorphp:tv-remove-dead-include-require Jul 29, 2026
39 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

Thank you 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants