From ed0e0cdffc988e62d285e03fe3a59a1f06a600bb Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 19 Dec 2017 18:28:36 -0500 Subject: [PATCH 1/2] Update tests to allow this inside closures --- VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php b/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php index c01f2850..5bea562d 100644 --- a/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php +++ b/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php @@ -194,8 +194,6 @@ public function testFunctionWithClosureWarnings() { 26, 27, 28, - 36, - 37, 35, ]; $this->assertEquals($expectedWarnings, $lines); From 77951464768fa245c5813b81c87f7c973b937db4 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 19 Dec 2017 18:31:55 -0500 Subject: [PATCH 2/2] Allow `$this` within closures --- .../Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 50705db5..57ca0b08 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -765,10 +765,10 @@ protected function checkForThisWithinClass( } foreach (array_reverse($token['conditions'], true) as $scopePtr => $scopeCode) { - // $this within a closure is invalid + // $this within a closure is valid // Note: have to fetch code from $tokens, T_CLOSURE isn't set for conditions codes. if ($tokens[$scopePtr]['code'] === T_CLOSURE) { - return false; + return true; } if ($scopeCode === T_CLASS) { return true;