From e76e816236f401458dd8e16beecab905861b5867 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 9 Mar 2021 17:32:14 -0500 Subject: [PATCH] Start inline block scope at end of condition rather than at keyword (#230) * Add test for inline conditional assignment and use inside else * Start inline block scope at end of condition rather than at keyword --- .../fixtures/FunctionWithInlineIfConditionFixture.php | 7 +++++++ .../Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 10 ++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php b/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php index aa69874e..d5dfcad1 100644 --- a/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php @@ -136,3 +136,10 @@ function loopAndPushWithUndefinedArray($parts) { $suggestions[] = $part; // undefined array variable return $suggestions; } + +function ifElseConditionWithInlineAssignAndUseInsideElse() { + if($q = getData()) + echo $q; + else + echo $q; +} diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 0ccb736f..fecf0b56 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -1487,19 +1487,21 @@ protected function processVaribleInsideElse(File $phpcsFile, $stackPtr, $varName foreach ($allAssignmentIndices as $index) { foreach ($blockIndices as $blockIndex) { $blockToken = $tokens[$blockIndex]; - Helpers::debug('looking at assignment', $index, 'at block index', $blockIndex, 'which is token', $blockToken); + Helpers::debug('for variable inside else, looking at assignment', $index, 'at block index', $blockIndex, 'which is token', $blockToken); if (isset($blockToken['scope_opener']) && isset($blockToken['scope_closer'])) { $scopeOpener = $blockToken['scope_opener']; $scopeCloser = $blockToken['scope_closer']; } else { - // If the `if` statement has no scope, it is probably inline, which means its scope is up until the next semicolon - $scopeOpener = $blockIndex + 1; + // If the `if` statement has no scope, it is probably inline, which + // means its scope is from the end of the condition up until the next + // semicolon + $scopeOpener = isset($blockToken['parenthesis_closer']) ? $blockToken['parenthesis_closer'] : $blockIndex + 1; $scopeCloser = $phpcsFile->findNext([T_SEMICOLON], $scopeOpener); if (! $scopeCloser) { throw new \Exception("Cannot find scope for if condition block at index {$stackPtr} while examining variable {$varName}"); } } - Helpers::debug('looking at scope', $index, 'between', $scopeOpener, 'and', $scopeCloser); + Helpers::debug('for variable inside else, looking at scope', $index, 'between', $scopeOpener, 'and', $scopeCloser); if (Helpers::isIndexInsideScope($index, $scopeOpener, $scopeCloser)) { $assignmentsInsideAttachedBlocks[] = $index; }