Skip to content

Commit

Permalink
[Transform] Add type matching to MethodCallToPropertyFetchRector (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 5, 2022
1 parent 460e0b4 commit 00e857f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.1|^8.0",
"phpstan/phpstan": "^1.4.6"
"phpstan/phpstan": "1.4.7"
},
"autoload": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\Source;

final class RenameToProperty
{
public function getEntityManager()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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')]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -18,9 +19,9 @@
final class MethodCallToPropertyFetchRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<string, string>
* @var MethodCallToPropertyFetch[]
*/
private array $methodCallToPropertyFetchCollection = [];
private array $methodCallsToPropertyFetches = [];

public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -66,12 +67,16 @@ public function getNodeTypes(): array
*/
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;
Expand All @@ -82,10 +87,8 @@ public function refactor(Node $node): ?Node
*/
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;
}
}
34 changes: 34 additions & 0 deletions rules/Transform/ValueObject/MethodCallToPropertyFetch.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 00e857f

Please sign in to comment.