From 348efa8909668f65d16fe01ed31990f004eb606e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Fri, 9 Jul 2021 20:59:00 +0200 Subject: [PATCH] RequireSingleLineCallSniffTest: Fixed fixer --- .../Sniffs/Functions/AbstractLineCall.php | 45 +++++++++++++++---- .../ReferenceUsedNamesOnlySniff.php | 4 +- .../RequireSingleLineCallSniffTest.php | 8 +++- ...thComplexParametersEnabledErrors.fixed.php | 2 + ...CallWithComplexParametersEnabledErrors.php | 6 +++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php b/SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php index 940a2dcff..72c869974 100644 --- a/SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php +++ b/SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php @@ -7,15 +7,16 @@ use SlevomatCodingStandard\Helpers\IndentationHelper; use SlevomatCodingStandard\Helpers\TokenHelper; use function array_merge; -use function preg_replace; use function rtrim; -use function sprintf; use function trim; +use const T_CLOSE_PARENTHESIS; +use const T_COMMA; use const T_FUNCTION; use const T_OPEN_PARENTHESIS; use const T_PARENT; use const T_SELF; use const T_STATIC; +use const T_WHITESPACE; abstract class AbstractLineCall implements Sniff { @@ -52,13 +53,41 @@ protected function getLineStart(File $phpcsFile, int $pointer): string protected function getCall(File $phpcsFile, int $parenthesisOpenerPointer, int $parenthesisCloserPointer): string { - $call = TokenHelper::getContent($phpcsFile, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer - 1); + $tokens = $phpcsFile->getTokens(); + + $pointerBeforeParenthesisCloser = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1); + + $endPointer = $tokens[$pointerBeforeParenthesisCloser]['code'] === T_COMMA + ? $pointerBeforeParenthesisCloser + : $parenthesisCloserPointer; + + $call = ''; + + for ($i = $parenthesisOpenerPointer + 1; $i < $endPointer; $i++) { + if ($tokens[$i]['code'] === T_COMMA) { + $nextPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1); + if ($tokens[$nextPointer]['code'] === T_CLOSE_PARENTHESIS) { + $i = $nextPointer - 1; + continue; + } + } - $call = preg_replace(sprintf('~%s[ \t]*~', $phpcsFile->eolChar), ' ', $call); - $call = preg_replace('~([({[])\s+~', '$1', $call); - $call = preg_replace('~\s+([)}\]])~', '$1', $call); - $call = preg_replace('~,\s*\)~', ')', $call); - $call = preg_replace('~,\s*$~', '', $call); + if ($tokens[$i]['code'] === T_WHITESPACE) { + if ($tokens[$i]['content'] === $phpcsFile->eolChar) { + if ($tokens[$i - 1]['code'] === T_COMMA) { + $call .= ' '; + } + + continue; + + } if ($tokens[$i]['column'] === 1) { + // Nothing + continue; + } + } + + $call .= $tokens[$i]['content']; + } return trim($call); } diff --git a/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php b/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php index 2622df370..a9a354f3c 100644 --- a/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php +++ b/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php @@ -262,9 +262,7 @@ public function process(File $phpcsFile, $openTagPointer): void && $namespacePointers === [] ) { $label = sprintf( - $reference->isConstant - ? 'Constant %s' - : ($reference->isFunction ? 'Function %s()' : 'Class %s'), + $reference->isConstant ? 'Constant %s' : ($reference->isFunction ? 'Function %s()' : 'Class %s'), $name ); diff --git a/tests/Sniffs/Functions/RequireSingleLineCallSniffTest.php b/tests/Sniffs/Functions/RequireSingleLineCallSniffTest.php index 4ab77df51..52fb8bfa1 100644 --- a/tests/Sniffs/Functions/RequireSingleLineCallSniffTest.php +++ b/tests/Sniffs/Functions/RequireSingleLineCallSniffTest.php @@ -66,7 +66,7 @@ public function testWithComplexParametersEnabledErrors(): void 'ignoreWithComplexParameter' => false, ]); - self::assertSame(4, $report->getErrorCount()); + self::assertSame(5, $report->getErrorCount()); self::assertSniffError( $report, @@ -92,6 +92,12 @@ public function testWithComplexParametersEnabledErrors(): void RequireSingleLineCallSniff::CODE_REQUIRED_SINGLE_LINE_CALL, 'Call of method doWhatever() should be placed on a single line.' ); + self::assertSniffError( + $report, + 29, + RequireSingleLineCallSniff::CODE_REQUIRED_SINGLE_LINE_CALL, + 'Call of method doNothing() should be placed on a single line.' + ); self::assertAllFixedInFile($report); } diff --git a/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.fixed.php b/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.fixed.php index f0844419c..16c612b5a 100644 --- a/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.fixed.php +++ b/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.fixed.php @@ -11,5 +11,7 @@ public function foo() return new self([true, false]); self::doWhatever(self::doAnything(true, false)); + + $this->doNothing('[test]', '{test}', '(test)'); } } diff --git a/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.php b/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.php index 8d57578a3..73937d61f 100644 --- a/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.php +++ b/tests/Sniffs/Functions/data/requireSingleLineCallWithComplexParametersEnabledErrors.php @@ -25,5 +25,11 @@ function () { self::doWhatever( self::doAnything(true, false) ); + + $this->doNothing( + '[test]', + '{test}', + '(test)', + ); } }