From 10a1b757ce2636d4c6935d5ba572573eaf60e75b Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Mon, 8 Jun 2020 12:16:36 +0000 Subject: [PATCH] BlankLineBeforeStatementFixer - better comment handling --- .../Whitespace/BlankLineBeforeStatementFixer.php | 13 +++++++++---- .../BlankLineBeforeStatementFixerTest.php | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php b/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php index d732f8b9a9a..6783a1e6d75 100644 --- a/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php +++ b/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php @@ -76,6 +76,8 @@ public function configure(array $configuration = null) foreach ($this->configuration['statements'] as $key) { $this->fixTokenMap[$key] = self::$tokenMap[$key]; } + + $this->fixTokenMap = array_values($this->fixTokenMap); } /** @@ -263,7 +265,7 @@ public function getPriority() */ public function isCandidate(Tokens $tokens) { - return $tokens->isAnyTokenKindsFound(array_values($this->fixTokenMap)); + return $tokens->isAnyTokenKindsFound($this->fixTokenMap); } /** @@ -271,13 +273,16 @@ public function isCandidate(Tokens $tokens) */ protected function applyFix(\SplFileInfo $file, Tokens $tokens) { - $tokenKinds = array_values($this->fixTokenMap); $analyzer = new TokensAnalyzer($tokens); for ($index = $tokens->count() - 1; $index > 0; --$index) { $token = $tokens[$index]; - if (!$token->isGivenKind($tokenKinds) || ($token->isGivenKind(T_WHILE) && $analyzer->isWhilePartOfDoWhile($index))) { + if (!$token->isGivenKind($this->fixTokenMap)) { + continue; + } + + if ($token->isGivenKind(T_WHILE) && $analyzer->isWhilePartOfDoWhile($index)) { continue; } @@ -331,7 +336,7 @@ private function shouldAddBlankLine(Tokens $tokens, $prevNonWhitespace) continue; } - return !$tokens[$j]->equals('{'); + return $tokens[$j]->equalsAny([';', '}']); } } diff --git a/tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php b/tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php index c089cdcdf39..09f483743ae 100644 --- a/tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php +++ b/tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php @@ -1019,6 +1019,17 @@ function foo() // comment return "bar"; +}', + ], + [ + '