[Transform] Add type matching to MethodCallToPropertyFetchRector (#1905)

This commit is contained in:
Tomas Votruba 2022-03-05 11:52:44 +01:00 committed by GitHub
parent 460e0b464e
commit 00e857f07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 20 deletions

View File

@ -7,7 +7,7 @@
],
"require": {
"php": "^7.1|^8.0",
"phpstan/phpstan": "^1.4.6"
"phpstan/phpstan": "1.4.7"
},
"autoload": {
"files": [

View File

@ -23,7 +23,7 @@
"nikic/php-parser": "^4.13.2",
"ondram/ci-detector": "^4.1",
"phpstan/phpdoc-parser": "^1.2",
"phpstan/phpstan": "^1.4.6",
"phpstan/phpstan": "1.4.7",
"phpstan/phpstan-phpunit": "^1.0",
"psr/log": "^2.0",
"react/child-process": "^0.6.4",

View File

@ -2,11 +2,13 @@
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Fixture;
class Fixture
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Source\RenameToProperty;
final class Fixture
{
public function run()
public function run(RenameToProperty $renameToProperty)
{
$entityManager = $this->getEntityManager();
$entityManager = $renameToProperty->getEntityManager();
}
}
@ -16,11 +18,13 @@ class Fixture
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Fixture;
class Fixture
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Source\RenameToProperty;
final class Fixture
{
public function run()
public function run(RenameToProperty $renameToProperty)
{
$entityManager = $this->entityManager;
$entityManager = $renameToProperty->entityManager;
}
}

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Source;
final class RenameToProperty
{
public function getEntityManager()
{
}
}

View File

@ -2,13 +2,14 @@
declare(strict_types=1);
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Source\RenameToProperty;
use Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector;
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MethodCallToPropertyFetchRector::class)
->configure([
'getEntityManager' => 'entityManager',
]);
->configure([new MethodCallToPropertyFetch(RenameToProperty::class, 'getEntityManager', 'entityManager')]);
};

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
@ -18,9 +19,9 @@ use Webmozart\Assert\Assert;
final class MethodCallToPropertyFetchRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<string, string>
* @var MethodCallToPropertyFetch[]
*/
private array $methodCallToPropertyFetchCollection = [];
private array $methodCallsToPropertyFetches = [];
public function getRuleDefinition(): RuleDefinition
{
@ -66,12 +67,16 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallToPropertyFetchCollection as $methodName => $propertyName) {
if (! $this->isName($node->name, $methodName)) {
foreach ($this->methodCallsToPropertyFetches as $methodCallToPropertyFetch) {
if (! $this->isName($node->name, $methodCallToPropertyFetch->getOldMethod())) {
continue;
}
return $this->nodeFactory->createPropertyFetch('this', $propertyName);
if (! $this->isObjectType($node->var, $methodCallToPropertyFetch->getOldObjectType())) {
continue;
}
return $this->nodeFactory->createPropertyFetch($node->var, $methodCallToPropertyFetch->getNewProperty());
}
return null;
@ -82,10 +87,8 @@ CODE_SAMPLE
*/
public function configure(array $configuration): void
{
Assert::allString(array_keys($configuration));
Assert::allString($configuration);
Assert::allIsAOf($configuration, MethodCallToPropertyFetch::class);
/** @var array<string, string> $configuration */
$this->methodCallToPropertyFetchCollection = $configuration;
$this->methodCallsToPropertyFetches = $configuration;
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class MethodCallToPropertyFetch
{
public function __construct(
private readonly string $oldType,
private readonly string $oldMethod,
private readonly string $newProperty,
) {
RectorAssert::className($oldType);
}
public function getOldObjectType(): ObjectType
{
return new ObjectType($this->oldType);
}
public function getNewProperty(): string
{
return $this->newProperty;
}
public function getOldMethod(): string
{
return $this->oldMethod;
}
}