Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
Expand Down Expand Up @@ -120,6 +121,7 @@
RemoveExpectAnyFromMockRector::class,
SingleMockPropertyTypeRector::class,
SimplerWithIsInstanceOfRector::class,
DirectInstanceOverMockArgRector::class,

FinalizeTestCaseClassRector::class,
DeclareStrictTypesTestsRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DirectInstanceOverMockArgRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class HandleArrayItem extends TestCase
{
public function testThat()
{
$args = [
$this->createMock(Request::class),
];
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class HandleArrayItem extends TestCase
{
public function testThat()
{
$args = [
new \Symfony\Component\HttpFoundation\Request(),
];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class HandleStaticCall extends TestCase
{
public function testThat()
{
AnyClass::staticRun(
$this->createMock(Request::class),
);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class HandleStaticCall extends TestCase
{
public function testThat()
{
AnyClass::staticRun(
new \Symfony\Component\HttpFoundation\Request(),
);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Source\AnotherObject;
use Symfony\Component\HttpFoundation\Request;

final class SkipAnotherObject extends TestCase
{
public function testThat()
{
AnyClass::staticRun(
$this->createMock(AnotherObject::class),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\Source;

final class AnotherObject
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector;

return RectorConfig::configure()
->withRules([DirectInstanceOverMockArgRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class HandleArrayItem extends TestCase
{
public function testThat()
{
$items = [
$this->createMock(\stdClass::class),
];
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class HandleArrayItem extends TestCase
{
public function testThat()
{
$items = [
$this->createStub(\stdClass::class),
];
}
}

?>
165 changes: 165 additions & 0 deletions rules/CodeQuality/Rector/CallLike/DirectInstanceOverMockArgRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\CallLike;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector\DirectInstanceOverMockArgRectorTest
*/
final class DirectInstanceOverMockArgRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use direct object instance over mock for specific objects in arg of PHPUnit tests',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class SomeTest extends TestCase
{
public function test()
{
$this->someMethod($this->createMock(Request::class));
}

private function someMethod($someClass)
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class SomeTest extends TestCase
{
public function test()
{
$this->someMethod(new Request());
}

private function someMethod($someClass)
{
}
}
CODE_SAMPLE
),

]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class, MethodCall::class, New_::class, ArrayItem::class];
}

/**
* @param MethodCall|StaticCall|New_|ArrayItem $node
*/
public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|null
{
$scope = ScopeFetcher::fetch($node);
if (! $scope->isInClass()) {
return null;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection->is(PHPUnitClassName::TEST_CASE)) {
return null;
}

$hasChanged = false;

if ($node instanceof ArrayItem) {

return $this->refactorArrayItem($node);
}

foreach ($node->getArgs() as $arg) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->isFirstClassCallable() should be checked early before ->getArgs()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$firstArg = $this->matchCreateMockMethodCallArg($arg->value);
if (! $firstArg instanceof Arg) {
continue;
}

$className = $this->valueResolver->getValue($firstArg->value);
if (! in_array($className, [SymfonyClass::REQUEST, SymfonyClass::REQUEST_STACK])) {
continue;
}

$arg->value = new New_(new FullyQualified($className));
$hasChanged = true;
}

if ($hasChanged) {
return $node;
}

return null;
}

private function matchCreateMockMethodCallArg(Expr $expr): ?Arg
{
if (! $expr instanceof MethodCall) {
return null;
}

$methodCall = $expr;
if (! $this->isName($methodCall->name, 'createMock')) {
return null;
}

if ($methodCall->isFirstClassCallable()) {
return null;
}

return $methodCall->getArgs()[0];
}

private function refactorArrayItem(ArrayItem $arrayItem): ?ArrayItem
{
$mockedCallArg = $this->matchCreateMockMethodCallArg($arrayItem->value);
if (! $mockedCallArg instanceof Arg) {
return null;
}

$className = $this->valueResolver->getValue($mockedCallArg->value);

if (! in_array($className, [SymfonyClass::REQUEST, SymfonyClass::REQUEST_STACK])) {
return null;
}

$arrayItem->value = new New_(new FullyQualified($className));

return $arrayItem;
}
}
Loading