From 3dfb838bdcf98a64ceee9118786619d0aacdbda9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 18 Feb 2020 08:15:31 +0000 Subject: [PATCH 1/2] Hotfix: Formatting\RedundantParentheses false positive with nested ternary --- .../Sniffs/Formatting/RedundantParenthesesSniff.php | 6 +++++- test/Sniffs/Formatting/RedundantParenthesesUnitTest.inc | 7 +++++++ .../Formatting/RedundantParenthesesUnitTest.inc.fixed | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) 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); + } } From aca4089a75aa6c2f76edd264f6e5c94a323ed1af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Thu, 20 Feb 2020 20:41:45 +0000 Subject: [PATCH 2/2] Adds CHANGELOG entry for #90 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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