From 4fc4686c7f342d3413352a0f65c0195b4021ecd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Sat, 16 Nov 2024 11:02:50 +0100 Subject: [PATCH] fix: improve handling of comments in line_break_between_method_arguments (#215) --- .../LineBreakBetweenMethodArgumentsFixer.php | 12 ++++- .../Regression/Case10.php | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/UseCase/LineBreakBetweenMethods/Regression/Case10.php diff --git a/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php b/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php index 988f750..2345ea7 100644 --- a/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php +++ b/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php @@ -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, [';', '{']); diff --git a/tests/UseCase/LineBreakBetweenMethods/Regression/Case10.php b/tests/UseCase/LineBreakBetweenMethods/Regression/Case10.php new file mode 100644 index 0000000..19d11f6 --- /dev/null +++ b/tests/UseCase/LineBreakBetweenMethods/Regression/Case10.php @@ -0,0 +1,52 @@ + + */ + public array $groups = []) + { + } + } + PHP; + } + + public function getExpectation(): string + { + return $this->getRawScript(); + } + + public function getMinSupportedPhpVersion(): int + { + return 80000; + } +}