Skip to content

FunctionDeclarations/NewClosure: bug fix - allow for anon classes within closures#1928

Merged
wimg merged 1 commit intodevelopfrom
feature/1481-newclosure-bug-fix
Oct 20, 2025
Merged

FunctionDeclarations/NewClosure: bug fix - allow for anon classes within closures#1928
wimg merged 1 commit intodevelopfrom
feature/1481-newclosure-bug-fix

Conversation

@jrfnl
Copy link
Copy Markdown
Member

@jrfnl jrfnl commented Oct 19, 2025

A PHP 7.0+ anonymous class can be declared within a closure and can use $this and OO hierarchy keywords, like self. In that case, those are not being used by the closure, but by the anonymous class.

Along the same lines, all other "closed" structures, like named functions, named classes etc, can be declared within a closure and for most of these, using $this or self is perfectly valid.

This commit changes the sniff to take those situations into account by skipping over any closed structures nested within a closure to prevent false positives.

Includes unit tests.

Fixes #1481

…hin closures

A PHP 7.0+ anonymous class can be declared _within_ a closure and can use `$this` and OO hierarchy keywords, like `self`.
In that case, those are not being used by the closure, but by the anonymous class.

Along the same lines, all other "closed" structures, like named functions, named classes etc, can be declared within a closure and for most of these, using `$this` or `self` is perfectly valid.

This commit changes the sniff to take those situations into account by skipping over any closed structures nested within a closure to prevent false positives.

Includes unit tests.

Fixes 1481
@jrfnl jrfnl added this to the 10.0.0-alpha1 milestone Oct 19, 2025
@jrfnl jrfnl requested a review from wimg October 19, 2025 03:27
@jrfnl jrfnl added Type: bug PR: quick merge PR only contains relatively simple changes PR: ready for review labels Oct 19, 2025
@wimg wimg merged commit bbbef69 into develop Oct 20, 2025
74 checks passed
@wimg wimg deleted the feature/1481-newclosure-bug-fix branch October 20, 2025 19:28
@github-actions github-actions bot removed PR: quick merge PR only contains relatively simple changes PR: ready for review labels Oct 20, 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.

ThisFoundOutsideClass fires false-positive on anonymous class

2 participants