Bug Description
The sniff ThisFoundOutsideClass fires a false-positive on $this when nested inside an anonymous class and a closure.
Given the following reproduction scenario
The issue happens when running this command:
./vendor/bin/phpcs -ps example.php --standard=PHPCompatibility --runtime-set testVersion 7.4-8.1
... over a file containing this code:
<?php
return function () {
$obj = new class() {
protected $x = 0;
public function increment(): int {
return ++$this->x;
}
};
};
with the built-in ruleset.
I'd expect the following behaviour
Pass
Instead this happened
FILE: /tmp/PHPCompatibility/example.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
9 | WARNING | Closures / anonymous functions only have access to $this if used within a class or when bound to an object using bindTo(). Please verify.
| | (PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Environment
| Environment |
Answer |
| PHP version |
8.1.6; 7.4.29 |
| PHP_CodeSniffer version |
3.7.2 |
| PHPCompatibility version |
develop (circa 4576a17) |
| Install type |
composer |
Additional Context (optional)
This issue was pointed out to me as totten/civix#294. I can see it happening, so I'm forwarding it along with a tighter description.
Tested Against develop branch?
Bug Description
The sniff
ThisFoundOutsideClassfires a false-positive on$thiswhen nested inside an anonymous class and a closure.Given the following reproduction scenario
The issue happens when running this command:
... over a file containing this code:
with the built-in ruleset.
I'd expect the following behaviour
Pass
Instead this happened
Environment
develop(circa 4576a17)Additional Context (optional)
This issue was pointed out to me as totten/civix#294. I can see it happening, so I'm forwarding it along with a tighter description.
Tested Against
developbranch?developbranch of PHPCompatibility.