Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

43 changes: 8 additions & 35 deletions rules/Transform/Rector/StaticCall/StaticCallToNewRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@
namespace Rector\Transform\Rector\StaticCall;

use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\StaticCallToNew;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Transform\Rector\StaticCall\StaticCallToNewRector\StaticCallToNewRectorTest
* @deprecated This rule is deprecated, as only used for 1 case now covered by dedicated ResponseCreateToNewRector in rector-symfony. Write a custom rule for the exact project case instead.
*/
final class StaticCallToNewRector extends AbstractRector implements ConfigurableRectorInterface
final class StaticCallToNewRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, StaticCallToNew[]>
*/
private array $staticCallsToNewsByMethod = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change static call to new instance', [
Expand Down Expand Up @@ -67,37 +61,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}

$class = $this->getName($node->class);
if ($class === null) {
return null;
}

foreach ($this->staticCallsToNewsByMethod[strtolower($methodName)] ?? [] as $staticCallToNew) {
if (! $this->isName($node->class, $staticCallToNew->getClass())) {
continue;
}

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

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as only used for 1 case now covered by a dedicated rule; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, StaticCallToNew::class);

$this->staticCallsToNewsByMethod = [];
foreach ($configuration as $staticCallToNew) {
$this->staticCallsToNewsByMethod[strtolower($staticCallToNew->getMethod())][] = $staticCallToNew;
}
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/StaticCallToNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Rector\Validation\RectorAssert;

/**
* @api used in deprecated StaticCallToNewRector configs
*/
final readonly class StaticCallToNew
{
public function __construct(
Expand Down
Loading