|
11 | 11 | use SlevomatCodingStandard\Helpers\SniffSettingsHelper; |
12 | 12 | use SlevomatCodingStandard\Helpers\TokenHelper; |
13 | 13 | use SlevomatCodingStandard\Helpers\VariableHelper; |
14 | | -use function array_keys; |
15 | 14 | use function array_reverse; |
16 | 15 | use function count; |
17 | 16 | use function in_array; |
18 | 17 | use const T_CATCH; |
19 | 18 | use const T_DOUBLE_QUOTED_STRING; |
| 19 | +use const T_FINALLY; |
20 | 20 | use const T_HEREDOC; |
21 | 21 | use const T_VARIABLE; |
22 | 22 | use const T_WHITESPACE; |
@@ -76,11 +76,26 @@ public function process(File $phpcsFile, $catchPointer): void |
76 | 76 |
|
77 | 77 | $tryEndPointer = CatchHelper::getTryEndPointer($phpcsFile, $catchPointer); |
78 | 78 |
|
79 | | - if ($tokens[$tryEndPointer]['conditions'] !== []) { |
80 | | - $lastConditionPointer = array_reverse(array_keys($tokens[$tryEndPointer]['conditions']))[0]; |
81 | | - $nextScopeEnd = $tokens[$lastConditionPointer]['scope_closer']; |
82 | | - } else { |
83 | | - $nextScopeEnd = count($tokens) - 1; |
| 79 | + $possibleFinallyPointer = $tokens[$tryEndPointer]['scope_condition']; |
| 80 | + if ( |
| 81 | + $tokens[$possibleFinallyPointer]['code'] === T_FINALLY |
| 82 | + && $this->isVariableUsedInCodePart( |
| 83 | + $phpcsFile, |
| 84 | + $tokens[$possibleFinallyPointer]['scope_opener'], |
| 85 | + $tokens[$possibleFinallyPointer]['scope_closer'], |
| 86 | + $variableName |
| 87 | + ) |
| 88 | + ) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $nextScopeEnd = count($tokens) - 1; |
| 93 | + |
| 94 | + foreach (array_reverse($tokens[$tryEndPointer]['conditions'], true) as $conditionPointer => $conditionCode) { |
| 95 | + if (in_array($conditionCode, TokenHelper::$functionTokenCodes, true)) { |
| 96 | + $nextScopeEnd = $tokens[$conditionPointer]['scope_closer']; |
| 97 | + break; |
| 98 | + } |
84 | 99 | } |
85 | 100 |
|
86 | 101 | if ($this->isVariableUsedInCodePart($phpcsFile, $tryEndPointer, $nextScopeEnd, $variableName)) { |
|
0 commit comments