From adb831d0d4eefcbd1aeec2523ee552851d0bdcd1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 29 Sep 2024 09:02:01 +0200 Subject: [PATCH] [CodeQuality] Add SingleWithConsecutiveToWithRector (#370) * register rule to code quality set * [CodeQuality] Add SingleWithConsecutiveToWithRector --- config/sets/phpunit-code-quality.php | 6 ++ .../Fixture/repeated_same.php.inc | 43 ++++++++ .../Fixture/skip_multiple_values.php.inc | 20 ++++ .../SingleWithConsecutiveToWithRectorTest.php | 28 +++++ .../Source/SomeMockedClass.php | 7 ++ .../config/configured_rule.php | 10 ++ .../NarrowIdenticalWithConsecutiveRector.php | 3 + .../SingleWithConsecutiveToWithRector.php | 100 ++++++++++++++++++ 8 files changed, 217 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index 61c924d8..647516fe 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -26,7 +26,9 @@ use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertSameBoolNullToSpecificMethodRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertSameTrueFalseToAssertTrueFalseRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector; +use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector; +use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector; @@ -43,6 +45,10 @@ AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector::class, DataProviderArrayItemsNewLinedRector::class, + // narrow with consecutive + NarrowIdenticalWithConsecutiveRector::class, + SingleWithConsecutiveToWithRector::class, + // specific asserts AssertCompareOnCountableWithMethodToAssertCountRector::class, AssertCompareToSpecificMethodRector::class, diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc new file mode 100644 index 00000000..f256b11b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc @@ -0,0 +1,43 @@ +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [1], + ); + } +} + +?> +----- +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->with( + [1], + ); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc new file mode 100644 index 00000000..81cf84b3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc @@ -0,0 +1,20 @@ +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [1], + [2], + ); + } +} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php new file mode 100644 index 00000000..67face27 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php new file mode 100644 index 00000000..12d95a97 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php @@ -0,0 +1,7 @@ +rule(SingleWithConsecutiveToWithRector::class); +}; diff --git a/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php b/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php index 90ac40e8..7b8a84c5 100644 --- a/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php +++ b/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php @@ -15,6 +15,9 @@ use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; +/** + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\NarrowIdenticalWithConsecutiveRectorTest + */ final class NarrowIdenticalWithConsecutiveRector extends AbstractRector { public function __construct( diff --git a/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php b/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php new file mode 100644 index 00000000..4524f8c3 --- /dev/null +++ b/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php @@ -0,0 +1,100 @@ +personServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [1], + ); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +use PHPUnit\Framework\TestCase; + +final class SomeTest extends TestCase +{ + public function run() + { + $this->personServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->with([1]); + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall|StaticCall $node + */ + public function refactor(Node $node): MethodCall|StaticCall|null + { + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + if (! $this->isName($node->name, 'withConsecutive')) { + return null; + } + + if (count($node->getArgs()) !== 1) { + return null; + } + + $firstArg = $node->getArgs()[0]; + + // use simpler with() instead + $node->name = new Identifier('with'); + $node->args = [new Arg($firstArg->value)]; + + return $node; + } +}