Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve handling of comments in line_break_between_method_arguments #215

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}