From 1ae8b58f11afabe8c299c087cee77557aee880fa Mon Sep 17 00:00:00 2001 From: PedroTroller Date: Wed, 8 Mar 2023 13:29:03 +0100 Subject: [PATCH] feat: remove strict lambda in phpspec files --- README.md | 2 + bin/doc | 6 +- composer.json | 2 +- src/PedroTroller/CS/Fixer/PhpspecFixer.php | 37 ++++++- .../MethodArguments.php | 2 +- tests/UseCase/Phpspec/Regression/Case3.php | 101 ++++++++++++++++++ 6 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 tests/UseCase/Phpspec/Regression/Case3.php diff --git a/README.md b/README.md index 3fb7c9b..6d36aee 100644 --- a/README.md +++ b/README.md @@ -1080,6 +1080,8 @@ Phpspec scenario functions MUST NOT have a scope. The methods of the phpspec specification classes MUST BE sorted (let, letGo, its_*, it_*, getMatchers and the rest of the methods) +Lambda functions MUST NOT have a static scope. + ### Available options diff --git a/bin/doc b/bin/doc index e35ef59..2a9eb34 100755 --- a/bin/doc +++ b/bin/doc @@ -18,8 +18,8 @@ $fixers = array_map(function (AbstractFixer $fixer) { $samples = $fixer->getDefinition()->getCodeSamples(); return [ - 'name' => $fixer->getName(), - 'doc' => [ + 'name' => $fixer->getName(), + 'doc' => [ 'summary' => $fixer->getDefinition()->getSummary(), ], 'deprecated' => $fixer->isDeprecated(), @@ -39,7 +39,7 @@ $fixers = array_map(function (AbstractFixer $fixer) { ? $fixer->getConfigurationDefinition()->getOptions() : [] ), - 'samples' => array_map(function (CodeSample $sample) use ($fixer) { + 'samples' => array_map(function (CodeSample $sample) use ($fixer) { if ($fixer instanceof ConfigurableFixerInterface) { $fixer->configure($sample->getConfiguration()); } diff --git a/composer.json b/composer.json index df843f2..fe13ecd 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "MIT", "require": { "php": "^8.0", - "friendsofphp/php-cs-fixer": "^3.13.1" + "friendsofphp/php-cs-fixer": "^3.14.0" }, "require-dev": { "phpspec/phpspec": "^7.0", diff --git a/src/PedroTroller/CS/Fixer/PhpspecFixer.php b/src/PedroTroller/CS/Fixer/PhpspecFixer.php index d7f55d3..e92f0e6 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\ConfigurableFixerInterface; +use PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer; use PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer; use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface; @@ -86,6 +87,7 @@ public function getDocumentation(): string 'Phpspec scenario functions MUST NOT have a return type declaration.', 'Phpspec scenario functions MUST NOT have a scope.', 'The methods of the phpspec specification classes MUST BE sorted (let, letGo, its_*, it_*, getMatchers and the rest of the methods)', + 'Lambda functions MUST NOT have a static scope.', ] ); } @@ -93,8 +95,9 @@ public function getDocumentation(): string public function getPriority(): int { return Priority::after( + StaticLambdaFixer::class, VisibilityRequiredFixer::class, - VoidReturnFixer::class + VoidReturnFixer::class, ); } @@ -141,6 +144,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void { $this->removeScope($file, $tokens); $this->removeReturn($file, $tokens); + $this->removeStaticLambda($file, $tokens); parent::applyFix($file, $tokens); } @@ -222,6 +226,37 @@ private function removeReturn(SplFileInfo $file, Tokens $tokens): void } } + private function removeStaticLambda(SplFileInfo $file, Tokens $tokens): void + { + $sequences = [ + [ + [T_STATIC, 'static'], + [T_FN, 'fn'], + '(', + ], + [ + [T_STATIC, 'static'], + [T_FUNCTION, 'function'], + '(', + ], + ]; + + foreach ($sequences as $sequence) { + $found = $tokens->findSequence($sequence); + + while (false === \in_array($found, [null, []], true)) { + foreach ($found as $index => $token) { + if ($token->isGivenKind(T_STATIC)) { + $tokens->clearAt($index); + $tokens->removeTrailingWhitespace($index); + } + } + + $found = $tokens->findSequence($sequence, $index); + } + } + } + private function filterElementsByMethodName(string $regex, array $elements): array { $filter = []; diff --git a/tests/TokensAnalyzerIntegration/MethodArguments.php b/tests/TokensAnalyzerIntegration/MethodArguments.php index a8ea0b7..d379637 100644 --- a/tests/TokensAnalyzerIntegration/MethodArguments.php +++ b/tests/TokensAnalyzerIntegration/MethodArguments.php @@ -66,7 +66,7 @@ public function assertions(TokensAnalyzer $analyzer, Tokens $tokens): void Assert::eq( $arguments, [ - ($theFunction + 5) => [ + ($theFunction + 5) => [ 'type' => 'Domain\\Model\\User', 'name' => '$user', 'nullable' => false, diff --git a/tests/UseCase/Phpspec/Regression/Case3.php b/tests/UseCase/Phpspec/Regression/Case3.php new file mode 100644 index 0000000..e72f63a --- /dev/null +++ b/tests/UseCase/Phpspec/Regression/Case3.php @@ -0,0 +1,101 @@ +createTemporaryFile(Argument::any(), Argument::any()) + ->will( + static fn ($args) => tempnam($args[1] ?? sys_get_temp_dir(), $args[0] ?? uniqid()) + ) + ; + + $fileManager + ->createTemporaryFile(Argument::any(), Argument::any()) + ->will( + static function ($args) { + return tempnam($args[1] ?? sys_get_temp_dir(), $args[0] ?? uniqid()); + } + ) + ; + } + + private static function staticFunc(): string + { + return 'Hello'; + } + } + PHP; + } + + public function getExpectation(): string + { + return <<<'PHP' + createTemporaryFile(Argument::any(), Argument::any()) + ->will( + fn ($args) => tempnam($args[1] ?? sys_get_temp_dir(), $args[0] ?? uniqid()) + ) + ; + + $fileManager + ->createTemporaryFile(Argument::any(), Argument::any()) + ->will( + function ($args) { + return tempnam($args[1] ?? sys_get_temp_dir(), $args[0] ?? uniqid()); + } + ) + ; + } + + private static function staticFunc(): string + { + return 'Hello'; + } + } + PHP; + } + + public function getMinSupportedPhpVersion(): int + { + return 0; + } +}