rector/rules/PhpSpecToPHPUnit/Rector/Variable/MockVariableToPropertyFetchRector.php
Tomas Votruba 96112cb1f0 Updated Rector to commit 2da49992cc
2da49992cc [Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
2021-07-05 22:50:18 +00:00

51 lines
1.5 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector\Variable;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use Rector\PhpSpecToPHPUnit\PhpSpecMockCollector;
use Rector\PhpSpecToPHPUnit\Rector\AbstractPhpSpecToPHPUnitRector;
/**
* $mock->call()
* ↓
* $this->mock->call()
*
* @see \Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\PhpSpecToPHPUnitRectorTest
*/
final class MockVariableToPropertyFetchRector extends \Rector\PhpSpecToPHPUnit\Rector\AbstractPhpSpecToPHPUnitRector
{
/**
* @var \Rector\PhpSpecToPHPUnit\PhpSpecMockCollector
*/
private $phpSpecMockCollector;
public function __construct(\Rector\PhpSpecToPHPUnit\PhpSpecMockCollector $phpSpecMockCollector)
{
$this->phpSpecMockCollector = $phpSpecMockCollector;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\Variable::class];
}
/**
* @param Variable $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if (!$this->isInPhpSpecBehavior($node)) {
return null;
}
if (!$this->phpSpecMockCollector->isVariableMockInProperty($node)) {
return null;
}
/** @var string $variableName */
$variableName = $this->getName($node);
return new \PhpParser\Node\Expr\PropertyFetch(new \PhpParser\Node\Expr\Variable('this'), $variableName);
}
}