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,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass;

final class HopAroundInCall extends TestCase
{
public function test()
{
$someMock = $this->createMock(AnotherClass::class);

$anotherClass = new AnotherClass(1, 2, 3, $someMock);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass;

final class HopAroundInCall extends TestCase
{
public function test()
{
$anotherClass = new AnotherClass(1, 2, 3, $this->createMock(AnotherClass::class));
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultipleUsesAsNoConnection extends TestCase
{
public function test()
{
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);

$this->useMock($someMock);
$this->useMockAgain($someMock);
}

private function useMock($someMock)
{
}

private function useMockAgain($someMock)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

final class AnotherClass
{
public function __construct(...$various)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,20 @@ public function refactor(Node $node): ?Node
$assign = $stmt->expr;

$instanceArg = $this->assignedMocksCollector->matchCreateMockArgAssignedToVariable($assign);
if (! $instanceArg instanceof Arg) {
if ($instanceArg instanceof Arg && $assign->var instanceof Variable && $this->isName(
$assign->var,
$variableName
)) {
// 1. remove assign
unset($node->stmts[$key]);
$hasChanged = true;
$variablesToMethodCalls[$variableName] = $assign->expr;
continue;
}
}

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

if (! $this->isName($assign->var, $variableName)) {
continue;
}

// 1. remove assign
unset($node->stmts[$key]);
$hasChanged = true;

$variablesToMethodCalls[$variableName] = $assign->expr;
// nothing to processy yet
if ($variablesToMethodCalls === []) {
continue;
}

Expand Down