From 6f2a22d75eed4b5c1ada6926bee05e8d198df172 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 18 Nov 2020 08:10:09 +1100 Subject: [PATCH] Fixed bug #3165 : Squiz.PHP.DisallowComparisonAssignment false positive when comparison inside closure --- package.xml | 1 + .../Squiz/Sniffs/PHP/DisallowComparisonAssignmentSniff.php | 5 +++-- .../Tests/PHP/DisallowComparisonAssignmentUnitTest.inc | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/package.xml b/package.xml index 4336e79855..4d1053fe40 100644 --- a/package.xml +++ b/package.xml @@ -63,6 +63,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- Thanks to Michael S for the patch - Fixed bug #2913 : Generic.WhiteSpace.ScopeIndent false positive when opening and closing tag on same line inside conditional - Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented + - Fixed bug #3165 : Squiz.PHP.DisallowComparisonAssignment false positive when comparison inside closure diff --git a/src/Standards/Squiz/Sniffs/PHP/DisallowComparisonAssignmentSniff.php b/src/Standards/Squiz/Sniffs/PHP/DisallowComparisonAssignmentSniff.php index 7ad2b6f6ac..7c7e2246f5 100644 --- a/src/Standards/Squiz/Sniffs/PHP/DisallowComparisonAssignmentSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/DisallowComparisonAssignmentSniff.php @@ -76,8 +76,9 @@ public function process(File $phpcsFile, $stackPtr) ]; $next = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true); - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS - && $tokens[($next - 1)]['code'] === T_STRING + if ($tokens[$next]['code'] === T_CLOSURE + || ($tokens[$next]['code'] === T_OPEN_PARENTHESIS + && $tokens[($next - 1)]['code'] === T_STRING) ) { // Code will look like: $var = myFunction( // and will be ignored. diff --git a/src/Standards/Squiz/Tests/PHP/DisallowComparisonAssignmentUnitTest.inc b/src/Standards/Squiz/Tests/PHP/DisallowComparisonAssignmentUnitTest.inc index 3a629ff855..022aca739e 100644 --- a/src/Standards/Squiz/Tests/PHP/DisallowComparisonAssignmentUnitTest.inc +++ b/src/Standards/Squiz/Tests/PHP/DisallowComparisonAssignmentUnitTest.inc @@ -65,3 +65,9 @@ $a = [ $var = $foo->something(!$var); $var = $foo?->something(!$var); + +$callback = function ($value) { + if ($value > 10) { + return false; + } +};