Skip to content

Commit

Permalink
Do not ignore closures
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Jan 6, 2020
1 parent 1504f91 commit 0a6c51e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
$end = $tokens[$stackPtr]['scope_closer'];

do {
$next = $phpcsFile->findNext([T_VARIABLE, T_CLOSURE, T_FN, T_ANON_CLASS], ($next + 1), $end);
$next = $phpcsFile->findNext([T_VARIABLE, T_ANON_CLASS], ($next + 1), $end);
if ($next === false) {
continue;
} else if ($tokens[$next]['code'] === T_CLOSURE
|| $tokens[$next]['code'] === T_FN
|| $tokens[$next]['code'] === T_ANON_CLASS
) {
} else if ($tokens[$next]['code'] === T_ANON_CLASS) {
$next = $tokens[$next]['scope_closer'];
continue;
} else if (strtolower($tokens[$next]['content']) !== '$this') {
Expand Down
49 changes: 39 additions & 10 deletions src/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,55 @@ class MyClass

public function getAnonymousClass() {
return new class() {
public static function something() {
$this->doSomething();
public static function something() {
$this->doSomething();
}
};
}
}

trait MyTrait {
public static function myFunc() {
$this->doSomething();
}
public static function myFunc() {
$this->doSomething();
}
}

$b = new class()
{
public static function myFunc() {
$this->doSomething();
}
public static function myFunc() {
$this->doSomething();
}

public static function other() {
return fn () => $this->name;
}

public static function anonClassUseThis() {
return new class($this) {
public function __construct($class) {
}
};
}

public static function anonClassAnotherThis() {
return new class() {
public function __construct() {
$this->id = 1;
}
};
}

public static function other() {
return fn () => $this->name;
public static function anonClassNestedUseThis() {
return new class(new class($this) {}) {
};
}

public static function anonClassNestedAnotherThis() {
return new class(new class() {
public function __construct() {
$this->id = 1;
}
}) {
};
}
}
4 changes: 4 additions & 0 deletions src/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public function getErrorList()
9 => 1,
14 => 1,
20 => 1,
41 => 1,
61 => 1,
69 => 1,
76 => 1,
80 => 1,
84 => 1,
99 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 0a6c51e

Please sign in to comment.