Skip to content

Commit 632600b

Browse files
committed
Squiz/NonExecutableCode: fold duplicate code
Follow up on commits 0e10f43 and 01754d9, which both deal with fixing bugs where the sniff would not handle if/elseif/else conditions without curly braces correctly. This commit merges the two near duplicate code blocks, which the above mentioned commits introduced, each containing code doing essentially the same thing. Also note that `T_ELSE` is handled separately now as `else` does not take parentheses and can therefore not be a parenthesis owner. This change is already covered by pre-existing tests.
1 parent fb9447f commit 632600b

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,13 @@ public function process(File $phpcsFile, $stackPtr)
9494
}
9595
}//end if
9696

97-
// Check if this token is actually part of a one-line IF or ELSE statement.
98-
for ($i = ($stackPtr - 1); $i > 0; $i--) {
99-
if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
100-
$i = $tokens[$i]['parenthesis_opener'];
101-
continue;
102-
} else if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
103-
continue;
104-
}
105-
106-
break;
107-
}
108-
109-
if ($tokens[$i]['code'] === T_IF
110-
|| $tokens[$i]['code'] === T_ELSE
111-
|| $tokens[$i]['code'] === T_ELSEIF
97+
// This token may be part of an inline condition.
98+
// If we find a closing parenthesis that belongs to a condition,
99+
// or an "else", we should ignore this token.
100+
if ($tokens[$prev]['code'] === T_ELSE
101+
|| (isset($tokens[$prev]['parenthesis_owner']) === true
102+
&& ($tokens[$tokens[$prev]['parenthesis_owner']]['code'] === T_IF
103+
|| $tokens[$tokens[$prev]['parenthesis_owner']]['code'] === T_ELSEIF))
112104
) {
113105
return;
114106
}
@@ -176,21 +168,6 @@ public function process(File $phpcsFile, $stackPtr)
176168
}//end if
177169
}//end if
178170

179-
// This token may be part of an inline condition.
180-
// If we find a closing parenthesis that belongs to a condition
181-
// we should ignore this token.
182-
if (isset($tokens[$prev]['parenthesis_owner']) === true) {
183-
$owner = $tokens[$prev]['parenthesis_owner'];
184-
$ignore = [
185-
T_IF => true,
186-
T_ELSE => true,
187-
T_ELSEIF => true,
188-
];
189-
if (isset($ignore[$tokens[$owner]['code']]) === true) {
190-
return;
191-
}
192-
}
193-
194171
$ourConditions = array_keys($tokens[$stackPtr]['conditions']);
195172

196173
if (empty($ourConditions) === false) {

0 commit comments

Comments
 (0)