Skip to content

Commit

Permalink
Updated Rector to commit dfbd366
Browse files Browse the repository at this point in the history
rectorphp/rector-src@dfbd366 [Transform] Add MethodCallToMethodCallRector (#505)
  • Loading branch information
TomasVotruba committed Jul 25, 2021
1 parent 8f07228 commit 3f2ea1e
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 72 deletions.
51 changes: 49 additions & 2 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 491 Rules Overview
# 492 Rules Overview

<br>

Expand Down Expand Up @@ -94,7 +94,7 @@

- [Restoration](#restoration) (6)

- [Transform](#transform) (36)
- [Transform](#transform) (37)

- [TypeDeclaration](#typedeclaration) (18)

Expand Down Expand Up @@ -10792,6 +10792,53 @@ return static function (ContainerConfigurator $containerConfigurator): void {

<br>

### MethodCallToMethodCallRector

Change method one method from one service to a method call to in another service

:wrench: **configure it!**

- class: [`Rector\Transform\Rector\MethodCall\MethodCallToMethodCallRector`](../rules/Transform/Rector/MethodCall/MethodCallToMethodCallRector.php)

```php
use Rector\Transform\Rector\MethodCall\MethodCallToMethodCallRector;
use Rector\Transform\ValueObject\MethodCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(MethodCallToMethodCallRector::class)
->call('configure', [[
MethodCallToMethodCallRector::METHOD_CALLS_TO_METHOD_CALLS => ValueObjectInliner::inline([
new MethodCallToMethodCall('FirstDependency', 'go', 'SecondDependency', 'away'),
]),
]]);
};
```


```diff
class SomeClass
{
public function __construct(
- private FirstDependency $firstDependency
+ private SecondDependency $secondDependency
) {
}

public function run()
{
- $this->firstDependency->go();
+ $this->secondDependency->away();
}
}
```

<br>

### MethodCallToPropertyFetchRector

Turns method call `"$this->something()"` to property fetch "$this->something"
Expand Down
8 changes: 3 additions & 5 deletions packages/PostRector/Collector/PropertyToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ public function isActive() : bool
}
/**
* @param \PhpParser\Node\Stmt\Class_ $class
* @param string $propertyName
* @param \PHPStan\Type\Type|null $propertyType
* @param int $propertyFlags
* @param \Rector\PostRector\ValueObject\PropertyMetadata $propertyMetadata
*/
public function addPropertyToClass($class, $propertyName, $propertyType, $propertyFlags) : void
public function addPropertyToClass($class, $propertyMetadata) : void
{
$uniqueHash = \spl_object_hash($class);
$this->propertiesByClass[$uniqueHash][] = new \Rector\PostRector\ValueObject\PropertyMetadata($propertyName, $propertyType, $propertyFlags);
$this->propertiesByClass[$uniqueHash][] = $propertyMetadata;
}
/**
* @param \PhpParser\Node\Stmt\Class_ $class
Expand Down
8 changes: 7 additions & 1 deletion packages/PostRector/DependencyInjection/PropertyAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\PostRector\ValueObject\PropertyMetadata;
final class PropertyAdder
{
/**
Expand Down Expand Up @@ -58,9 +59,14 @@ public function addPropertyToCollector(\PhpParser\Node\Stmt\Property $property)
$propertyName = $this->nodeNameResolver->getName($property);
$this->addConstructorDependencyToClass($classNode, $propertyType, $propertyName, $property->flags);
}
/**
* @deprecated
* Use directly @see PropertyToAddCollector::addPropertyToClass() directly with PropertyMetadata object
*/
public function addConstructorDependencyToClass(\PhpParser\Node\Stmt\Class_ $class, \PHPStan\Type\Type $propertyType, string $propertyName, int $propertyFlags = 0) : void
{
$this->propertyToAddCollector->addPropertyToClass($class, $propertyName, $propertyType, $propertyFlags);
$propertyMetadata = new \Rector\PostRector\ValueObject\PropertyMetadata($propertyName, $propertyType, $propertyFlags);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
$this->rectorChangeCollector->notifyNodeFileInfo($class);
}
public function addServiceConstructorDependencyToClass(\PhpParser\Node\Stmt\Class_ $class, \PHPStan\Type\ObjectType $objectType) : void
Expand Down
142 changes: 142 additions & 0 deletions rules/Transform/Rector/MethodCall/MethodCallToMethodCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare (strict_types=1);
namespace Rector\Transform\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\PropertyPresenceChecker;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Transform\ValueObject\MethodCallToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20210725\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\MethodCallToMethodCallRectorTest
*/
final class MethodCallToMethodCallRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
* @var string
*/
public const METHOD_CALLS_TO_METHOD_CALLS = 'method_calls_to_method_calls';
/**
* @var MethodCallToMethodCall[]
*/
private $methodCallsToMethodsCalls = [];
/**
* @var \Rector\Naming\Naming\PropertyNaming
*/
private $propertyNaming;
/**
* @var \Rector\Core\NodeAnalyzer\PropertyPresenceChecker
*/
private $propertyPresenceChecker;
public function __construct(\Rector\Naming\Naming\PropertyNaming $propertyNaming, \Rector\Core\NodeAnalyzer\PropertyPresenceChecker $propertyPresenceChecker)
{
$this->propertyNaming = $propertyNaming;
$this->propertyPresenceChecker = $propertyPresenceChecker;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change method one method from one service to a method call to in another service', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
private FirstDependency $firstDependency
) {
}
public function run()
{
$this->firstDependency->go();
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
private SecondDependency $secondDependency
) {
}
public function run()
{
$this->secondDependency->away();
}
}
CODE_SAMPLE
, [self::METHOD_CALLS_TO_METHOD_CALLS => [new \Rector\Transform\ValueObject\MethodCallToMethodCall('FirstDependency', 'go', 'SecondDependency', 'away')]])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
foreach ($this->methodCallsToMethodsCalls as $methodCallToMethodCall) {
if (!$node->var instanceof \PhpParser\Node\Expr\PropertyFetch) {
continue;
}
if (!$this->isMatch($node, $methodCallToMethodCall)) {
continue;
}
$propertyFetch = $node->var;
$class = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
$newObjectType = new \PHPStan\Type\ObjectType($methodCallToMethodCall->getNewType());
$newPropertyName = $this->matchNewPropertyName($methodCallToMethodCall, $class);
if ($newPropertyName === null) {
continue;
}
$this->addConstructorDependencyToClass($class, $newObjectType, $newPropertyName);
// rename property
$node->var = new \PhpParser\Node\Expr\PropertyFetch($propertyFetch->var, $newPropertyName);
// rename method
$node->name = new \PhpParser\Node\Identifier($methodCallToMethodCall->getNewMethod());
return $node;
}
return null;
}
/**
* @param array<string, MethodCallToMethodCall[]> $configuration
*/
public function configure(array $configuration) : void
{
$methodCallsToMethodsCalls = $configuration[self::METHOD_CALLS_TO_METHOD_CALLS] ?? [];
\RectorPrefix20210725\Webmozart\Assert\Assert::allIsAOf($methodCallsToMethodsCalls, \Rector\Transform\ValueObject\MethodCallToMethodCall::class);
$this->methodCallsToMethodsCalls = $methodCallsToMethodsCalls;
}
private function isMatch(\PhpParser\Node\Expr\MethodCall $methodCall, \Rector\Transform\ValueObject\MethodCallToMethodCall $methodCallToMethodCall) : bool
{
if (!$this->isObjectType($methodCall->var, new \PHPStan\Type\ObjectType($methodCallToMethodCall->getOldType()))) {
return \false;
}
return $this->isName($methodCall->name, $methodCallToMethodCall->getOldMethod());
}
private function matchNewPropertyName(\Rector\Transform\ValueObject\MethodCallToMethodCall $methodCallToMethodCall, \PhpParser\Node\Stmt\Class_ $class) : ?string
{
$newPropertyName = $this->propertyNaming->fqnToVariableName($methodCallToMethodCall->getNewType());
$propertyMetadata = new \Rector\PostRector\ValueObject\PropertyMetadata($newPropertyName, new \PHPStan\Type\ObjectType($methodCallToMethodCall->getNewType()), \PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE);
$classContextProperty = $this->propertyPresenceChecker->getClassContextProperty($class, $propertyMetadata);
if ($classContextProperty === null) {
return $newPropertyName;
}
// re-use existing proeprty name
return $this->getName($classContextProperty);
}
}
51 changes: 51 additions & 0 deletions rules/Transform/ValueObject/MethodCallToMethodCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare (strict_types=1);
namespace Rector\Transform\ValueObject;

final class MethodCallToMethodCall
{
/**
* @var string
*/
private $oldType;
/**
* @var string
*/
private $oldMethod;
/**
* @var string
*/
private $newType;
/**
* @var string
*/
private $newMethod;
/**
* @param class-string $oldType
* @param class-string $newType
*/
public function __construct(string $oldType, string $oldMethod, string $newType, string $newMethod)
{
$this->oldType = $oldType;
$this->oldMethod = $oldMethod;
$this->newType = $newType;
$this->newMethod = $newMethod;
}
public function getOldType() : string
{
return $this->oldType;
}
public function getOldMethod() : string
{
return $this->oldMethod;
}
public function getNewType() : string
{
return $this->newType;
}
public function getNewMethod() : string
{
return $this->newMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare (strict_types=1);
namespace Rector\TypeDeclaration\NodeAnalyzer;

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand All @@ -24,7 +25,7 @@ public function __construct(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFact
$this->nodeNameResolver = $nodeNameResolver;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $node
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Param|\PhpParser\Node\Stmt\Property $node
*/
public function detect($node) : bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '93add0f6c9aef0d30a46c16be1ac1ad013b6d3e4';
public const PACKAGE_VERSION = 'dfbd366b74df51ca25f509c779499cd9978b5045';
/**
* @var string
*/
public const RELEASE_DATE = '2021-07-25 12:48:53';
public const RELEASE_DATE = '2021-07-25 13:02:20';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210725\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
Expand Down
6 changes: 2 additions & 4 deletions src/Bootstrap/ExtensionConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public function provide() : array
}
$generatedConfigDirectory = \dirname($generatedConfigReflectionClass->getFileName());
foreach (\Rector\RectorInstaller\GeneratedConfig::EXTENSIONS as $extensionConfig) {
if (!isset($extensionConfig['extra']['includes'])) {
continue;
}
foreach ($extensionConfig['extra']['includes'] as $includedFile) {
$includedFiles = $extensionConfig['extra']['includes'] ?? [];
foreach ($includedFiles as $includedFile) {
$includedFilePath = $this->resolveIncludeFilePath($extensionConfig, $generatedConfigDirectory, $includedFile);
if ($includedFilePath === null) {
$includedFilePath = \sprintf('%s/%s', $extensionConfig['install_path'], $includedFile);
Expand Down
Loading

0 comments on commit 3f2ea1e

Please sign in to comment.