2020-10-20 22:01:47 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\New_;
|
2020-10-20 22:01:47 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-10-20 22:01:47 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://github.com/phpstan/phpstan-src/blob/699c420f8193da66927e54494a0afa0c323c6458/src/Rules/Classes/NewStaticRule.php
|
2020-10-20 22:01:47 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\New_\NewStaticToNewSelfRector\NewStaticToNewSelfRectorTest
|
2020-10-20 22:01:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class NewStaticToNewSelfRector extends AbstractRector
|
2020-10-20 22:01:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-10-20 22:01:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change unsafe new static() to new self()', [new CodeSample(<<<'CODE_SAMPLE'
|
2023-06-08 23:05:51 +00:00
|
|
|
final class SomeClass
|
2020-10-20 22:01:47 +00:00
|
|
|
{
|
|
|
|
public function build()
|
|
|
|
{
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2023-06-08 23:05:51 +00:00
|
|
|
final class SomeClass
|
2020-10-20 22:01:47 +00:00
|
|
|
{
|
|
|
|
public function build()
|
|
|
|
{
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-10-20 22:01:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-10-20 22:01:47 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-10-20 22:01:47 +00:00
|
|
|
{
|
2023-06-08 23:05:51 +00:00
|
|
|
return [Class_::class];
|
2020-10-20 22:01:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-08 23:05:51 +00:00
|
|
|
* @param Class_ $node
|
2020-10-20 22:01:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-10-20 22:01:47 +00:00
|
|
|
{
|
2023-06-08 23:05:51 +00:00
|
|
|
if (!$node->isFinal()) {
|
2020-10-20 22:01:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-08 23:05:51 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
$this->traverseNodesWithCallable($node, function (Node $node) use(&$hasChanged) : ?New_ {
|
|
|
|
if (!$node instanceof New_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->isName($node->class, ObjectReference::STATIC)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$hasChanged = \true;
|
|
|
|
$node->class = new Name(ObjectReference::SELF);
|
|
|
|
return $node;
|
|
|
|
});
|
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2020-10-20 22:01:47 +00:00
|
|
|
}
|
2023-06-08 23:05:51 +00:00
|
|
|
return null;
|
2020-10-20 22:01:47 +00:00
|
|
|
}
|
|
|
|
}
|