Skip to content

Commit

Permalink
BlankLineBeforeStatementFixer - better comment handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Jun 14, 2020
1 parent 382e00e commit 10a1b75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -263,21 +265,24 @@ public function getPriority()
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isAnyTokenKindsFound(array_values($this->fixTokenMap));
return $tokens->isAnyTokenKindsFound($this->fixTokenMap);
}

/**
* {@inheritdoc}
*/
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;
}

Expand Down Expand Up @@ -331,7 +336,7 @@ private function shouldAddBlankLine(Tokens $tokens, $prevNonWhitespace)
continue;
}

return !$tokens[$j]->equals('{');
return $tokens[$j]->equalsAny([';', '}']);
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,17 @@ function foo()
// comment
return "bar";
}',
],
[
'<?php
function foo()
{
switch ($foo) {
case 2: // comment
return 1;
}
}',
],
];
Expand Down

0 comments on commit 10a1b75

Please sign in to comment.