Skip to content

Commit

Permalink
[Transform] Add MethodCallToFuncCallRector (#3391)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 19, 2023
1 parent 9c6f41e commit 937bf3e
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 6 deletions.
43 changes: 41 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 413 Rules Overview
# 414 Rules Overview

<br>

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

- [Strict](#strict) (5)

- [Transform](#transform) (34)
- [Transform](#transform) (35)

- [TypeDeclaration](#typedeclaration) (36)

Expand Down Expand Up @@ -8305,6 +8305,45 @@ return static function (RectorConfig $rectorConfig): void {

<br>

### MethodCallToFuncCallRector

Change method call to function call

:wrench: **configure it!**

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

```php
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Transform\Rector\MethodCall\MethodCallToFuncCallRector;
use Rector\Transform\ValueObject\MethodCallToFuncCall;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(MethodCallToFuncCallRector::class, [
new MethodCallToFuncCall('SomeClass', 'render', 'view'),
]);
};
```


```diff
final class SomeClass
{
public function show()
{
- return $this->render('some_template');
+ return view('some_template');
}
}
```

<br>

### MethodCallToMethodCallRector

Change method one method from one service to a method call to in another service
Expand Down
6 changes: 3 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,6 @@ parameters:

- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'

# PHPStan 1.4.7
- '#Call to static method Webmozart\\Assert\\Assert\:\:allIsAOf|isAOf|assertInstanceOf\(\) with .* will always evaluate to true#'

-
message: '#Make callable type explicit#'
path: src/NodeManipulator/BinaryOpManipulator.php
Expand Down Expand Up @@ -805,3 +802,6 @@ parameters:

# nullable in external package
- '#Parameters should use "PhpParser\\Node\\Expr\\FuncCall\|string\|string\|PHPStan\\Analyser\\Scope" types as the only types passed to this method#'

- '#Call to static method Webmozart\\Assert\\Assert\:\:(allIsAOf|isAOf|assertInstanceOf|allIsInstanceOf)(.*?) will always evaluate to true#'
- '#Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with (.*?) will always evaluate to true#'
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Fixture;

use Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source\ParentControllerWithRender;

final class SomeClass extends ParentControllerWithRender
{
public function show()
{
return $this->render('some_template');
}
}

?>
-----
<?php

namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Fixture;

use Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source\ParentControllerWithRender;

final class SomeClass extends ParentControllerWithRender
{
public function show()
{
return \view('some_template');
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MethodCallToFuncCallRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

class ParentControllerWithRender
{
public function render()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Transform\Rector\MethodCall\MethodCallToFuncCallRector;
use Rector\Transform\ValueObject\MethodCallToFuncCall;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(MethodCallToFuncCallRector::class, [
new MethodCallToFuncCall(
'Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source\ParentControllerWithRender',
'render',
'view'
),
]);
};
95 changes: 95 additions & 0 deletions rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\MethodCallToFuncCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\MethodCallToFuncCallRectorTest
*/
final class MethodCallToFuncCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var MethodCallToFuncCall[]
*/
private array $methodCallsToFuncCalls = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change method call to function call', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function show()
{
return $this->render('some_template');
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function show()
{
return view('some_template');
}
}
CODE_SAMPLE
,
[new MethodCallToFuncCall('SomeClass', 'render', 'view')]
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallsToFuncCalls as $methodCallToFuncCall) {
if (! $this->isName($node->name, $methodCallToFuncCall->getMethodName())) {
continue;
}

if (! $this->isObjectType($node->var, new ObjectType($methodCallToFuncCall->getObjectType()))) {
continue;
}

return new FuncCall(new FullyQualified($methodCallToFuncCall->getFunctionName()), $node->getArgs());
}

return null;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsInstanceOf($configuration, MethodCallToFuncCall::class);
$this->methodCallsToFuncCalls = $configuration;
}
}
30 changes: 30 additions & 0 deletions rules/Transform/ValueObject/MethodCallToFuncCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\ValueObject;

final class MethodCallToFuncCall
{
public function __construct(
private readonly string $objectType,
private readonly string $methodName,
private readonly string $functionName
) {
}

public function getObjectType(): string
{
return $this->objectType;
}

public function getMethodName(): string
{
return $this->methodName;
}

public function getFunctionName(): string
{
return $this->functionName;
}
}
2 changes: 1 addition & 1 deletion src/ValueObject/Reporting/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
private readonly string $diffConsoleFormatted,
private readonly array $rectorsWithLineChanges = []
) {
Assert::allIsAOf($rectorsWithLineChanges, RectorWithLineChange::class);
Assert::allIsInstanceOf($rectorsWithLineChanges, RectorWithLineChange::class);
}

public function getDiff(): string
Expand Down

0 comments on commit 937bf3e

Please sign in to comment.