Skip to content

Commit

Permalink
fix: improve handling of comments in line_break_between_method_argume…
Browse files Browse the repository at this point in the history
…nts (#215)
  • Loading branch information
ostrolucky authored Nov 16, 2024
1 parent 68613e1 commit 4fc4686
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,20 @@ private function mergeArgs(Tokens $tokens, $index): void
$closeBraceIndex = $this->analyze($tokens)->getClosingParenthesis($openBraceIndex);

foreach ($tokens->findGivenKind(T_WHITESPACE, $openBraceIndex, $closeBraceIndex) as $spaceIndex => $spaceToken) {
if ($tokens[$tokens->getPrevNonWhitespace($spaceIndex)]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
continue;
}

if ($tokens[$tokens->getNextNonWhitespace($spaceIndex)]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
continue;
}

$tokens[$spaceIndex] = new Token([T_WHITESPACE, ' ']);
}

$tokens->removeTrailingWhitespace($openBraceIndex);
if (!$tokens[$tokens->getNextNonWhitespace($openBraceIndex)]->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
$tokens->removeTrailingWhitespace($openBraceIndex);
}
$tokens->removeLeadingWhitespace($closeBraceIndex);

$end = $tokens->getNextTokenOfKind($closeBraceIndex, [';', '{']);
Expand Down
52 changes: 52 additions & 0 deletions tests/UseCase/LineBreakBetweenMethods/Regression/Case10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace tests\UseCase\LineBreakBetweenMethods\Regression;

use PedroTroller\CS\Fixer\CodingStyle\LineBreakBetweenMethodArgumentsFixer;
use tests\UseCase;

/**
* https://github.com/PedroTroller/PhpCSFixer-Custom-Fixers/issues/208
* https://github.com/PedroTroller/PhpCSFixer-Custom-Fixers/issues/214.
*/
final class Case10 implements UseCase
{
public function getFixers(): iterable
{
yield new LineBreakBetweenMethodArgumentsFixer();
}

public function getRawScript(): string
{
return <<<'PHP'
<?php
class Foo
{
public function __construct(
// simple comment
public ?string $simpleCommentAbove = null,
// public string|null $commentedOutLine = null,
public ?string $detailAndEdit = null,
/**
* @var list<string>
*/
public array $groups = [])
{
}
}
PHP;
}

public function getExpectation(): string
{
return $this->getRawScript();
}

public function getMinSupportedPhpVersion(): int
{
return 80000;
}
}

0 comments on commit 4fc4686

Please sign in to comment.