diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a6e49f6..c4bedd12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#90](https://github.com/webimpress/coding-standard/pull/90) fixes false-positive in `Formatting\RedundantParentheses` sniff when using nested ternary with `instanceof` condition. ## 1.1.3 - 2020-02-09 diff --git a/src/WebimpressCodingStandard/Sniffs/Formatting/RedundantParenthesesSniff.php b/src/WebimpressCodingStandard/Sniffs/Formatting/RedundantParenthesesSniff.php index 626aa65b..08fc6ffd 100644 --- a/src/WebimpressCodingStandard/Sniffs/Formatting/RedundantParenthesesSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Formatting/RedundantParenthesesSniff.php @@ -152,7 +152,11 @@ public function process(File $phpcsFile, $stackPtr) if (! in_array($tokens[$prev]['code'], Tokens::$castTokens, true)) { $instanceOf = $phpcsFile->findNext(T_INSTANCEOF, $stackPtr + 1, $closePtr); if ($instanceOf !== false) { - $op = $phpcsFile->findNext(Tokens::$booleanOperators, $stackPtr + 1, $closePtr); + $op = $phpcsFile->findNext( + Tokens::$booleanOperators + [T_INLINE_ELSE => T_INLINE_ELSE], + $stackPtr + 1, + $closePtr + ); if ($op === false) { $this->error($phpcsFile, $stackPtr, $closePtr, 'SingleInstanceOf'); return; diff --git a/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc b/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc index 671ec928..72e338dd 100644 --- a/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc +++ b/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc @@ -192,4 +192,11 @@ class RedundantParentheses if (! (new Invoke())()) {} if (! (new Invoke())->method()) {} } + + public function nestedTernary() + { + return $a->foo() + ? $a->bar() + : ($a->baz() instanceof \DateTime ? 1 : 2); + } } diff --git a/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc.fixed b/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc.fixed index be7db75b..c3c978fa 100644 --- a/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc.fixed +++ b/test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc.fixed @@ -192,4 +192,11 @@ class RedundantParentheses if (! (new Invoke())()) {} if (! (new Invoke())->method()) {} } + + public function nestedTernary() + { + return $a->foo() + ? $a->bar() + : ($a->baz() instanceof \DateTime ? 1 : 2); + } }