diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index 7454a661..74feb3be 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -15,12 +15,14 @@ use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector; +use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\EntityDocumentCreateMockToDirectNewRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector; +use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector; use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector; use Rector\PHPUnit\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector; use Rector\PHPUnit\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector; @@ -111,7 +113,7 @@ AddInstanceofAssertForNullableInstanceRector::class, // enable next after testing - // \Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector::class, + AddInstanceofAssertForNullableArgumentRector::class, AssertArrayCastedObjectToAssertSameRector::class, @@ -149,7 +151,10 @@ EntityDocumentCreateMockToDirectNewRector::class, ReplaceAtMethodWithDesiredMatcherRector::class, BareCreateMockAssignToDirectUseRector::class, + + // dead code RemoveNeverUsedMockPropertyRector::class, + RemoveStandaloneCreateMockRector::class, // readability NoSetupWithParentCallOverrideRector::class, diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..854170b9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/fixture.php.inc @@ -0,0 +1,30 @@ +createMock('someClass'); + } +} + +?> +----- + diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/handle_multiples.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/handle_multiples.php.inc new file mode 100644 index 00000000..8ebaacf9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/handle_multiples.php.inc @@ -0,0 +1,33 @@ +createMock('someClass'); + $this->createMock('someClass'); + $this->createMock('someClass'); + $this->createMock('someClass'); + } +} + +?> +----- + diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/handle_nested_call.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/handle_nested_call.php.inc new file mode 100644 index 00000000..eabceae7 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/handle_nested_call.php.inc @@ -0,0 +1,32 @@ +createMock('someClass') + ->method('some') + ->willReturn(100); + } +} + +?> +----- + diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/skip_assigned_call.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/skip_assigned_call.php.inc new file mode 100644 index 00000000..d34bccfe --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/Fixture/skip_assigned_call.php.inc @@ -0,0 +1,13 @@ +createMock('someClass'); + } +} diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/RemoveStandaloneCreateMockRectorTest.php b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/RemoveStandaloneCreateMockRectorTest.php new file mode 100644 index 00000000..96bafc76 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/RemoveStandaloneCreateMockRectorTest.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/ClassMethod/RemoveStandaloneCreateMockRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/config/configured_rule.php new file mode 100644 index 00000000..72f7bcc9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(RemoveStandaloneCreateMockRector::class); +}; diff --git a/rules/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector.php b/rules/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector.php new file mode 100644 index 00000000..1d9f2739 --- /dev/null +++ b/rules/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector.php @@ -0,0 +1,112 @@ +createMock('SomeClass'); + } +} +CODE_SAMPLE + + , + <<<'CODE_SAMPLE' +use PHPUnit\Framework\TestCase; + +final class SomeTest extends TestCase +{ + public function test() + { + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class]; + } + + /** + * @param ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + $hasChanged = false; + foreach ((array) $node->stmts as $key => $stmt) { + if (! $stmt instanceof Expression) { + continue; + } + + if (! $stmt->expr instanceof MethodCall) { + continue; + } + + $topmostCall = $this->resolveTopmostCall($stmt->expr); + if (! $this->isName($topmostCall->name, 'createMock')) { + continue; + } + + unset($node->stmts[$key]); + $hasChanged = true; + } + + if ($hasChanged) { + return $node; + } + + return null; + } + + private function resolveTopmostCall(MethodCall $methodCall): MethodCall + { + $currentMethodCall = $methodCall; + while ($currentMethodCall->var instanceof MethodCall) { + $currentMethodCall = $currentMethodCall->var; + } + + return $currentMethodCall; + } +} diff --git a/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php b/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php index eae0abfb..bbc41338 100644 --- a/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php +++ b/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php @@ -4,11 +4,11 @@ namespace Rector\PHPUnit\PHPUnit120\Rector\MethodCall; -use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Type\ObjectType; use Rector\PHPUnit\Enum\PHPUnitClassName; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;