Skip to content

Commit

Permalink
[Transform] MethodCall to New (#6352)
Browse files Browse the repository at this point in the history
* working rule

* Fixes

* Skip first class callable
  • Loading branch information
peterfox authored Oct 11, 2024
1 parent 52e3fdf commit 28ba716
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class Fixture
{
public function run(\Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory $factory)
{
$response = $factory->createResponse([
'a' => 'b'
]);
}
}

?>
-----
<?php

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

class Fixture
{
public function run(\Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory $factory)
{
$response = new \Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\Response([
'a' => 'b'
]);
}
}

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

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

class SkipNonMatchingClass
{
public function run(Response $factory)
{
$response = $factory->jsonResponse([
'a' => 'b'
]);
}
}

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

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

class SkipNonMatchingMethod
{
public function run(\Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory $factory)
{
$response = $factory->jsonResponse([
'a' => 'b'
]);
}
}

?>
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\MethodCallToNewRector;

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

final class MethodCallToNewRectorTest 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,11 @@
<?php

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

class Response
{
public function __construct(public array $params)
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class ResponseFactory
{
public function createResponse(array $params): Response
{
return new Response($params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use PHPStan\Type\ObjectType;
use Rector\Config\RectorConfig;
use Rector\Transform\Rector\MethodCall\MethodCallToNewRector;
use Rector\Transform\ValueObject\MethodCallToNew;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(MethodCallToNewRector::class, [
new MethodCallToNew(
new ObjectType('Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory'),
'createResponse',
'Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\Response',
),
]);
};
84 changes: 84 additions & 0 deletions rules/Transform/Rector/MethodCall/MethodCallToNewRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\Rector\MethodCall;

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

/**
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\MethodCallToNewRectorTest
*/
class MethodCallToNewRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var MethodCallToNew[]
*/
private array $methodCallToNew;

/**
* @param MethodCallToNew[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, MethodCallToNew::class);
$this->methodCallToNew = $configuration;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change method call to new class',
[new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$object->createResponse(['a' => 1]);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
new Response(['a' => 1]);
CODE_SAMPLE
,
[new MethodCallToNew(new ObjectType('ResponseFactory'), 'createResponse', 'Response')]
)]
);
}

public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?New_
{
if ($node->isFirstClassCallable()) {
return null;
}

foreach ($this->methodCallToNew as $methodCallToNew) {
if (! $this->isName($node->name, $methodCallToNew->getMethodName())) {
continue;
}

if (! $this->isObjectType($node->var, $methodCallToNew->getObject())) {
continue;
}

return new New_(new FullyQualified($methodCallToNew->getNewClassString()), $node->args);
}

return null;
}
}
35 changes: 35 additions & 0 deletions rules/Transform/ValueObject/MethodCallToNew.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Rector\Transform\ValueObject;

use PHPStan\Type\ObjectType;

final readonly class MethodCallToNew
{
/**
* @param class-string $newClassString
*/
public function __construct(
private ObjectType $objectType,
private string $methodName,
private string $newClassString
) {
}

public function getObject(): ObjectType
{
return $this->objectType;
}

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

public function getNewClassString(): string
{
return $this->newClassString;
}
}

0 comments on commit 28ba716

Please sign in to comment.