Skip to content

Commit

Permalink
Updated Rector to commit 28ba716ca753c2a2a1a0d22ef7aec12af2d81ba0
Browse files Browse the repository at this point in the history
rectorphp/rector-src@28ba716 [Transform] MethodCall to New (#6352)
  • Loading branch information
TomasVotruba committed Oct 11, 2024
1 parent 7595ee2 commit c522933
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
67 changes: 67 additions & 0 deletions rules/Transform/Rector/MethodCall/MethodCallToNewRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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 RectorPrefix202410\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\MethodCallToNewRectorTest
*/
class MethodCallToNewRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var MethodCallToNew[]
*/
private $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;
}
}
45 changes: 45 additions & 0 deletions rules/Transform/ValueObject/MethodCallToNew.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

use PHPStan\Type\ObjectType;
final class MethodCallToNew
{
/**
* @readonly
* @var \PHPStan\Type\ObjectType
*/
private $objectType;
/**
* @readonly
* @var string
*/
private $methodName;
/**
* @var class-string
* @readonly
*/
private $newClassString;
/**
* @param class-string $newClassString
*/
public function __construct(ObjectType $objectType, string $methodName, string $newClassString)
{
$this->objectType = $objectType;
$this->methodName = $methodName;
$this->newClassString = $newClassString;
}
public function getObject() : ObjectType
{
return $this->objectType;
}
public function getMethodName() : string
{
return $this->methodName;
}
public function getNewClassString() : string
{
return $this->newClassString;
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '52e3fdfb48a507be997bd0f47256ad14854be844';
public const PACKAGE_VERSION = '28ba716ca753c2a2a1a0d22ef7aec12af2d81ba0';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-10-11 15:33:12';
public const RELEASE_DATE = '2024-10-11 10:42:38';
/**
* @var int
*/
Expand Down
2 changes: 2 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,7 @@
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToStaticCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToFuncCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToNewRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToNewRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToPropertyFetchRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToPropertyFetchRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToStaticCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\ReplaceParentCallByPropertyCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php',
Expand All @@ -2410,6 +2411,7 @@
'Rector\\Transform\\ValueObject\\FuncCallToMethodCall' => $baseDir . '/rules/Transform/ValueObject/FuncCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\FuncCallToStaticCall' => $baseDir . '/rules/Transform/ValueObject/FuncCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToFuncCall' => $baseDir . '/rules/Transform/ValueObject/MethodCallToFuncCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToNew' => $baseDir . '/rules/Transform/ValueObject/MethodCallToNew.php',
'Rector\\Transform\\ValueObject\\MethodCallToPropertyFetch' => $baseDir . '/rules/Transform/ValueObject/MethodCallToPropertyFetch.php',
'Rector\\Transform\\ValueObject\\MethodCallToStaticCall' => $baseDir . '/rules/Transform/ValueObject/MethodCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\NewToStaticCall' => $baseDir . '/rules/Transform/ValueObject/NewToStaticCall.php',
Expand Down
2 changes: 2 additions & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -2613,6 +2613,7 @@ class ComposerStaticInit7c12491db1a700dd78980ecb6595c088
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToFuncCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToNewRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToNewRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToPropertyFetchRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToPropertyFetchRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\ReplaceParentCallByPropertyCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php',
Expand All @@ -2629,6 +2630,7 @@ class ComposerStaticInit7c12491db1a700dd78980ecb6595c088
'Rector\\Transform\\ValueObject\\FuncCallToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/FuncCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\FuncCallToStaticCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/FuncCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToFuncCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToFuncCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToNew' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToNew.php',
'Rector\\Transform\\ValueObject\\MethodCallToPropertyFetch' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToPropertyFetch.php',
'Rector\\Transform\\ValueObject\\MethodCallToStaticCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\NewToStaticCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/NewToStaticCall.php',
Expand Down

0 comments on commit c522933

Please sign in to comment.