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 @@ -7,6 +7,7 @@
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector;
Expand Down Expand Up @@ -132,6 +133,7 @@
CreateStubOverCreateMockArgRector::class,
ExpressionCreateMockToCreateStubRector::class,
PropertyCreateMockToCreateStubRector::class,
InlineStubPropertyToCreateStubMethodCallRector::class,

// @test first, enable later
// \Rector\PHPUnit\CodeQuality\Rector\Expression\ConfiguredMockEntityToSetterObjectRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;

final class InlineStubOnlyProperty extends TestCase
{
private Stub $someStub;

protected function setUp(): void
{
$this->someStub = $this->createStub(NotRelevantClass::class);
}

public function testAnother()
{
$anotherObject = new NotRelevantClass($this->someStub);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;

final class InlineStubOnlyProperty extends TestCase
{
protected function setUp(): void
{
}

public function testAnother()
{
$anotherObject = new NotRelevantClass($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class));
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;

final class SkipIfUsed extends TestCase
{
private Stub $someStub;

protected function setUp(): void
{
$this->someStub = $this->createStub(NotRelevantClass::class);
}

public function testAnother()
{
$someCall = $this->someStub->method('some_method');

$anotherObject = new NotRelevantClass($this->someStub);
}
}
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\Class_\InlineStubPropertyToCreateStubMethodCallRector;

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

final class InlineStubPropertyToCreateStubMethodCallRectorTest 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,12 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source;

final class NotRelevantClass
{
public function __construct($object)
{
}
}
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\Class_\InlineStubPropertyToCreateStubMethodCallRector;

return RectorConfig::configure()
->withRules([InlineStubPropertyToCreateStubMethodCallRector::class]);
69 changes: 69 additions & 0 deletions rules/CodeQuality/NodeAnalyser/StubPropertyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class StubPropertyResolver
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ValueResolver $valueResolver,
) {
}

/**
* @return array<string, string>
*/
public function resolveFromClassMethod(ClassMethod $classMethod): array
{
$propertyNamesToStubClasses = [];

foreach ((array) $classMethod->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

$assign = $stmt->expr;

if (! $assign->var instanceof PropertyFetch) {
continue;
}

if (! $assign->expr instanceof MethodCall) {
continue;
}

$methodCall = $assign->expr;
if (! $this->nodeNameResolver->isName($methodCall->name, 'createStub')) {
continue;
}

$propertyFetch = $assign->var;
$propertyName = $this->nodeNameResolver->getName($propertyFetch->name);

if (! is_string($propertyName)) {
continue;
}

$firstArg = $methodCall->getArgs()[0];
$stubbedClassName = $this->valueResolver->getValue($firstArg->value);

$propertyNamesToStubClasses[$propertyName] = $stubbedClassName;
}

return $propertyNamesToStubClasses;
}
}
51 changes: 51 additions & 0 deletions rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeFinder;

use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\BetterNodeFinder;

final readonly class PropertyFetchUsageFinder
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
) {
}

/**
* @return PropertyFetch[]
*/
public function findInNew(Class_ $class, string $propertyName): array
{
/** @var New_[] $news */
$news = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), New_::class);

$propertyFetchesInNewArgs = [];

foreach ($news as $new) {
if ($new->isFirstClassCallable()) {
continue;
}

foreach ($new->getArgs() as $arg) {
if (! $arg->value instanceof PropertyFetch) {
continue;
}

if (! $this->nodeNameResolver->isName($arg->value->name, $propertyName)) {
continue;
}

$propertyFetchesInNewArgs[] = $arg->value;
}
}

return $propertyFetchesInNewArgs;
}
}
Loading