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 HandleArrayItem extends TestCase
{
private Stub $someStub;

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

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

public function testArrayItems()
{
$anotherObject = $this->getMockBuilder(NotRelevantClass::class)
->setConstructorArgs([$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 HandleArrayItem extends TestCase
{
protected function setUp(): void
{
}

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

public function testArrayItems()
{
$anotherObject = $this->getMockBuilder(NotRelevantClass::class)
->setConstructorArgs([$this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class)]);
}
}

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

namespace Rector\PHPUnit\CodeQuality\NodeFinder;

use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -48,4 +49,32 @@ public function findInCallLikes(Class_ $class, string $propertyName): array

return $propertyFetchesInNewArgs;
}

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

$propertyFetchesInArrays = [];

foreach ($arrays as $array) {
foreach ($array->items as $arrayItem) {
if (! $arrayItem->value instanceof PropertyFetch) {
continue;
}

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

$propertyFetchesInArrays[] = $propertyFetch;
}
}

return $propertyFetchesInArrays;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ public function refactor(Node $node): ?Node
}

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

// are there more uses than simple passing to a new instance?
$totalPropertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $propertyName);
if ((count($totalPropertyFetches) - 1) !== count($currentPropertyFetchesInNewArgs)) {
if ((count($totalPropertyFetches) - 1) !== (count($currentPropertyFetchesInNewArgs) + count(
$currentPropertyFetchesInArrays
))) {
continue;
}

Expand Down