Skip to content

Commit d3bbba3

Browse files
loilosirbrillig
authored andcommitted
Support anonymous classes (#53)
* Tests: add test for $this and properties in anonymous classes * findVariableScope, checkForThisWithinClass: consider anonymous classes
1 parent b3553be commit d3bbba3

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static function findVariableScope(File $phpcsFile, $stackPtr) {
174174
if (($scopeCode === T_FUNCTION) || ($scopeCode === T_CLOSURE)) {
175175
return $scopePtr;
176176
}
177-
if (in_array($scopeCode, [T_CLASS, T_INTERFACE, T_TRAIT])) {
177+
if (in_array($scopeCode, [T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT])) {
178178
$in_class = true;
179179
}
180180
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected function checkForThisWithinClass(File $phpcsFile, $stackPtr, $varName,
320320
if ($tokens[$scopePtr]['code'] === T_CLOSURE) {
321321
return true;
322322
}
323-
if ($scopeCode === T_CLASS || $scopeCode === T_TRAIT) {
323+
if ($scopeCode === T_CLASS || $scopeCode === T_ANON_CLASS || $scopeCode === T_TRAIT) {
324324
return true;
325325
}
326326
}

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,18 @@ public function testTraitAllowsThis() {
390390
$this->assertEquals($expectedErrors, $lines);
391391
}
392392

393+
public function testAnonymousClassAllowsThis() {
394+
$fixtureFile = $this->getFixture('AnonymousClassFixture.php');
395+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
396+
$phpcsFile->process();
397+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
398+
$expectedWarnings = [];
399+
$this->assertEquals($expectedWarnings, $lines);
400+
$lines = $this->getErrorLineNumbersFromFile($phpcsFile);
401+
$expectedErrors = [];
402+
$this->assertEquals($expectedErrors, $lines);
403+
}
404+
393405
public function testVariableFunctionCallsCountAsUsage() {
394406
$fixtureFile = $this->getFixture('FunctionWithVariableCallFixture.php');
395407
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
@@ -414,6 +426,18 @@ public function testTraitsAllowPropertyDefinitions() {
414426
$this->assertEquals($expectedErrors, $lines);
415427
}
416428

429+
public function testAnonymousClassAllowPropertyDefinitions() {
430+
$fixtureFile = $this->getFixture('AnonymousClassWithPropertiesFixture.php');
431+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
432+
$phpcsFile->process();
433+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
434+
$expectedWarnings = [];
435+
$this->assertEquals($expectedWarnings, $lines);
436+
$lines = $this->getErrorLineNumbersFromFile($phpcsFile);
437+
$expectedErrors = [];
438+
$this->assertEquals($expectedErrors, $lines);
439+
}
440+
417441
public function testUnusedParamsAreReported() {
418442
$fixtureFile = $this->getFixture('FunctionWithUnusedParamsFixture.php');
419443
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
new class {
4+
public function sayHelloWorld() {
5+
echo 'Hello'.$this->getWorld();
6+
}
7+
8+
protected function getWorld() {
9+
return ' World!';
10+
}
11+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
new class {
4+
protected $storedHello;
5+
public $helloOptions = [];
6+
public function sayHelloWorld() {
7+
echo "hello world";
8+
}
9+
};
10+

0 commit comments

Comments
 (0)