From fc4d3ead059392287c53add7e5832b33b7b3be33 Mon Sep 17 00:00:00 2001 From: Pierre PLAZANET Date: Mon, 26 Apr 2021 11:53:13 +0200 Subject: [PATCH] fix: phpspec void remove linebreak (#140) --- src/PedroTroller/CS/Fixer/PhpspecFixer.php | 13 +++- tests/Runner.php | 6 +- tests/UseCase.php | 19 ++---- tests/UseCase/Behat.php | 10 +-- tests/UseCase/CommentLineToPhpdocBlock.php | 22 ++---- .../DoctrineMigrations/UselessComments.php | 22 ++---- .../UselessGetDescription.php | 22 ++---- tests/UseCase/ExceptionsPunctuation.php | 22 ++---- tests/UseCase/ForbiddenFunctions.php | 22 ++---- tests/UseCase/FuncSpec.php | 22 ++---- tests/UseCase/LineBreakBetweenMethods.php | 22 ++---- .../Regression/Case1.php | 22 ++---- .../Regression/Case2.php | 22 ++---- .../Regression/Case3.php | 22 ++---- .../Regression/Case4.php | 22 ++---- .../Regression/Case5.php | 22 ++---- ...eBreakBetweenMethodsWithNoReformatting.php | 22 ++---- ...etweenMethodsWithNoSplitOnNumberOfArgs.php | 22 ++---- tests/UseCase/LineBreakBetweenStatements.php | 22 ++---- tests/UseCase/OrderedSpecElements.php | 22 ++---- .../OrderedSpecElements/Regression/Case1.php | 22 ++---- .../OrderedWithGetterAndSetterFirst.php | 22 ++---- .../Regression/Case1.php | 22 ++---- .../Regression/Case2.php | 22 ++---- tests/UseCase/Phpspec.php | 67 ++++++++++++------- tests/UseCase/Phpspec/Regression/Case1.php | 22 ++---- tests/UseCase/Phpspec/Regression/Case2.php | 22 ++---- tests/UseCase/PhpspecScenarioScope.php | 22 ++---- tests/UseCase/SingleLineComment/Collapsed.php | 23 ++----- tests/UseCase/SingleLineComment/Expanded.php | 23 ++----- tests/UseCase/UselessCodeAfterReturn.php | 22 ++---- .../Regression/Case1.php | 31 +++++++++ .../{Sample1 => Case1}/file.php.txt | 0 .../Regression/Sample1.php | 43 ------------ tests/UseCase/UselessComment.php | 22 ++---- tests/UseCase/UselessComment/Php71.php | 22 ++---- .../UselessComment/Regression/DocBlockEnd.php | 22 ++---- .../Regression/DoubleLineBreak.php | 22 ++---- .../Regression/WithInterface.php | 22 ++---- 39 files changed, 257 insertions(+), 616 deletions(-) create mode 100644 tests/UseCase/UselessCodeAfterReturn/Regression/Case1.php rename tests/UseCase/UselessCodeAfterReturn/Regression/{Sample1 => Case1}/file.php.txt (100%) delete mode 100644 tests/UseCase/UselessCodeAfterReturn/Regression/Sample1.php diff --git a/src/PedroTroller/CS/Fixer/PhpspecFixer.php b/src/PedroTroller/CS/Fixer/PhpspecFixer.php index eee078c..b6f3bfc 100644 --- a/src/PedroTroller/CS/Fixer/PhpspecFixer.php +++ b/src/PedroTroller/CS/Fixer/PhpspecFixer.php @@ -6,6 +6,7 @@ use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer; use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; +use PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer; use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; use PhpCsFixer\Tokenizer\Token; @@ -99,7 +100,10 @@ public function getDocumentation() */ public function getPriority() { - return Priority::after(VisibilityRequiredFixer::class); + return Priority::after( + VisibilityRequiredFixer::class, + VoidReturnFixer::class + ); } /** @@ -223,7 +227,12 @@ private function removeReturn(SplFileInfo $file, Tokens $tokens): void } $tokens->clearRange($closeBraceIndex + 1, $openCurlyBracket - 1); - $tokens->ensureWhitespaceAtIndex($openCurlyBracket, 0, "\n".$this->analyze($tokens)->getLineIndentation($openBraceIndex)); + + if ($tokens[$closeBraceIndex - 1]->isWhitespace() && false !== strpos($tokens[$closeBraceIndex - 1]->getContent(), "\n")) { + $tokens->ensureWhitespaceAtIndex($openCurlyBracket, 0, ' '); + } else { + $tokens->ensureWhitespaceAtIndex($openCurlyBracket, 0, "\n".$this->analyze($tokens)->getLineIndentation($openBraceIndex)); + } } } diff --git a/tests/Runner.php b/tests/Runner.php index 5160176..c035c5c 100644 --- a/tests/Runner.php +++ b/tests/Runner.php @@ -80,7 +80,7 @@ private static function runUseCases(): void continue; } - $fixer = $usecase->getFixer(); + $fixers = $usecase->getFixers(); $tokens = Tokens::fromCode($usecase->getRawScript()); $differ = new Differ(); @@ -90,7 +90,9 @@ private static function runUseCases(): void echo "#######################################################################################\n"; echo "\n"; - $fixer->fix(new SplFileInfo(__FILE__), $tokens); + foreach ($fixers as $fixer) { + $fixer->fix(new SplFileInfo(__FILE__), $tokens); + } if ($usecase->getExpectation() !== $tokens->generateCode()) { throw new Exception($differ->diff($usecase->getExpectation(), $tokens->generateCode())); diff --git a/tests/UseCase.php b/tests/UseCase.php index d961301..d9081d8 100644 --- a/tests/UseCase.php +++ b/tests/UseCase.php @@ -9,22 +9,13 @@ interface UseCase { /** - * @return FixerInterface + * @return iterable */ - public function getFixer(); + public function getFixers(): iterable; - /** - * @return string - */ - public function getRawScript(); + public function getRawScript(): string; - /** - * @return string - */ - public function getExpectation(); + public function getExpectation(): string; - /** - * @return int - */ - public function getMinSupportedPhpVersion(); + public function getMinSupportedPhpVersion(): int; } diff --git a/tests/UseCase/Behat.php b/tests/UseCase/Behat.php index 99266c2..dded23a 100644 --- a/tests/UseCase/Behat.php +++ b/tests/UseCase/Behat.php @@ -9,12 +9,12 @@ final class Behat implements UseCase { - public function getFixer() + public function getFixers(): iterable { - return new OrderBehatStepsFixer(); + yield new OrderBehatStepsFixer(); } - public function getRawScript() + public function getRawScript(): string { return <<<'CONTEXT' ['var_dump', 'dump'], ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' 80, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return file_get_contents(sprintf('%s/Case1/CamelizeNamingStrategy.php.text', __DIR__)); } - /** - * {@inheritdoc} - */ - public function getExpectation() + public function getExpectation(): string { return file_get_contents(sprintf('%s/Case1/CamelizeNamingStrategy.php.text', __DIR__)); } - /** - * {@inheritdoc} - */ - public function getMinSupportedPhpVersion() + public function getMinSupportedPhpVersion(): int { return 70100; } diff --git a/tests/UseCase/LineBreakBetweenMethods/Regression/Case2.php b/tests/UseCase/LineBreakBetweenMethods/Regression/Case2.php index 445c597..e84aafd 100644 --- a/tests/UseCase/LineBreakBetweenMethods/Regression/Case2.php +++ b/tests/UseCase/LineBreakBetweenMethods/Regression/Case2.php @@ -9,10 +9,7 @@ final class Case2 implements UseCase { - /** - * {@inheritdoc} - */ - public function getFixer() + public function getFixers(): iterable { $fixer = new LineBreakBetweenMethodArgumentsFixer(); @@ -21,13 +18,10 @@ public function getFixer() 'max-length' => 80, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' 80, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' 100, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' 100, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' false, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' 90, ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' getRawScript(); } - /** - * {@inheritdoc} - */ - public function getMinSupportedPhpVersion() + public function getMinSupportedPhpVersion(): int { return 0; } diff --git a/tests/UseCase/Phpspec.php b/tests/UseCase/Phpspec.php index cd102b1..0efa0c4 100644 --- a/tests/UseCase/Phpspec.php +++ b/tests/UseCase/Phpspec.php @@ -5,22 +5,20 @@ namespace tests\UseCase; use PedroTroller\CS\Fixer\PhpspecFixer; +use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer; +use PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer; use tests\UseCase; final class Phpspec implements UseCase { - /** - * {@inheritdoc} - */ - public function getFixer() + public function getFixers(): iterable { - return new PhpspecFixer(); + yield new VisibilityRequiredFixer(); + yield new VoidReturnFixer(); + yield new PhpspecFixer(); } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'SPEC' ['Funk\Spec'], ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return file_get_contents(sprintf('%s/Case1/file.php.txt', __DIR__)); } - /** - * {@inheritdoc} - */ - public function getExpectation() + public function getExpectation(): string { return file_get_contents(sprintf('%s/Case1/file.php.txt', __DIR__)); } - /** - * {@inheritdoc} - */ - public function getMinSupportedPhpVersion() + public function getMinSupportedPhpVersion(): int { return 0; } diff --git a/tests/UseCase/Phpspec/Regression/Case2.php b/tests/UseCase/Phpspec/Regression/Case2.php index c91a54f..782c697 100644 --- a/tests/UseCase/Phpspec/Regression/Case2.php +++ b/tests/UseCase/Phpspec/Regression/Case2.php @@ -9,34 +9,22 @@ final class Case2 implements UseCase { - /** - * {@inheritdoc} - */ - public function getFixer() + public function getFixers(): iterable { - return new PhpspecFixer(); + yield new PhpspecFixer(); } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return file_get_contents(sprintf('%s/Case2/file.php.txt', __DIR__)); } - /** - * {@inheritdoc} - */ - public function getExpectation() + public function getExpectation(): string { return file_get_contents(sprintf('%s/Case2/file.php.txt', __DIR__)); } - /** - * {@inheritdoc} - */ - public function getMinSupportedPhpVersion() + public function getMinSupportedPhpVersion(): int { return 0; } diff --git a/tests/UseCase/PhpspecScenarioScope.php b/tests/UseCase/PhpspecScenarioScope.php index 220332a..6c3a4ee 100644 --- a/tests/UseCase/PhpspecScenarioScope.php +++ b/tests/UseCase/PhpspecScenarioScope.php @@ -9,18 +9,12 @@ final class PhpspecScenarioScope implements UseCase { - /** - * {@inheritdoc} - */ - public function getFixer() + public function getFixers(): iterable { - return new PhpspecScenarioScopeFixer(); + yield new PhpspecScenarioScopeFixer(); } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'SPEC' configure([ 'action' => 'collapsed', 'types' => ['@var'], ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP' configure([ 'action' => 'expanded', 'types' => ['@var'], ]); - return $fixer; + yield $fixer; } - /** - * {@inheritdoc} - */ - public function getRawScript() + public function getRawScript(): string { return <<<'PHP'