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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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 InlineUsedInCalls extends TestCase
{
private Stub $someStub;

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

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

$this->runThis($this->someStub);
}

private function runThis($object)
{
}
}

?>
-----
<?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 InlineUsedInCalls extends TestCase
{
protected function setUp(): void
{
}

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

$this->runThis($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class));
}

private function runThis($object)
{
}
}

?>
14 changes: 7 additions & 7 deletions rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\PHPUnit\CodeQuality\NodeFinder;

use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -21,19 +21,19 @@ public function __construct(
/**
* @return PropertyFetch[]
*/
public function findInNew(Class_ $class, string $propertyName): array
public function findInCallLikes(Class_ $class, string $propertyName): array
{
/** @var New_[] $news */
$news = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), New_::class);
/** @var CallLike[] $callLikes */
$callLikes = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), CallLike::class);

$propertyFetchesInNewArgs = [];

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

foreach ($new->getArgs() as $arg) {
foreach ($callLike->getArgs() as $arg) {
if (! $arg->value instanceof PropertyFetch) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node
continue;
}

$currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInNew($node, $propertyName);
$currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInCallLikes($node, $propertyName);

// are there more uses than simple passing to a new instance?
$totalPropertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $propertyName);
Expand Down