-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit 28ba716ca753c2a2a1a0d22ef7aec12af2d81ba0
rectorphp/rector-src@28ba716 [Transform] MethodCall to New (#6352)
- Loading branch information
1 parent
7595ee2
commit c522933
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
rules/Transform/Rector/MethodCall/MethodCallToNewRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters