Skip to content

Commit

Permalink
feat: remove strict lambda in phpspec files
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Mar 8, 2023
1 parent b25f454 commit 1ae8b58
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 36 additions & 1 deletion src/PedroTroller/CS/Fixer/PhpspecFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,15 +87,17 @@ 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.',
]
);
}

public function getPriority(): int
{
return Priority::after(
StaticLambdaFixer::class,
VisibilityRequiredFixer::class,
VoidReturnFixer::class
VoidReturnFixer::class,
);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/TokensAnalyzerIntegration/MethodArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
101 changes: 101 additions & 0 deletions tests/UseCase/Phpspec/Regression/Case3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace tests\UseCase\Phpspec\Regression;

use PedroTroller\CS\Fixer\PhpspecFixer;
use tests\UseCase;

final class Case3 implements UseCase
{
public function getFixers(): iterable
{
yield new PhpspecFixer();
}

public function getRawScript(): string
{
return <<<'PHP'
<?php
declare(strict_types=1);
namespace spec\App;
use PhpSpec\ObjectBehavior;
class MyClass extends ObjectBehavior
{
function it_is_a_scenario(FileManager $fileManager)
{
$fileManager
->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'
<?php
declare(strict_types=1);
namespace spec\App;
use PhpSpec\ObjectBehavior;
class MyClass extends ObjectBehavior
{
function it_is_a_scenario(FileManager $fileManager)
{
$fileManager
->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;
}
}

0 comments on commit 1ae8b58

Please sign in to comment.