mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
96112cb1f0
2da49992cc
[Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
83 lines
2.6 KiB
PHP
83 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Removing\Rector\Class_;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\Removing\Rector\Class_\RemoveParentRector\RemoveParentRectorTest
|
|
*/
|
|
final class RemoveParentRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @api
|
|
* @var string
|
|
*/
|
|
public const PARENT_TYPES_TO_REMOVE = 'parents_types_to_remove';
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private $parentClassesToRemove = [];
|
|
/**
|
|
* @var \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver
|
|
*/
|
|
private $parentClassScopeResolver;
|
|
public function __construct(\Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver $parentClassScopeResolver)
|
|
{
|
|
$this->parentClassScopeResolver = $parentClassScopeResolver;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Removes extends class by name', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
final class SomeClass extends SomeParentClass
|
|
{
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
}
|
|
CODE_SAMPLE
|
|
, [self::PARENT_TYPES_TO_REMOVE => ['SomeParentClass']])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\Class_::class];
|
|
}
|
|
/**
|
|
* @param Class_ $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($node);
|
|
if ($parentClassName === null) {
|
|
return null;
|
|
}
|
|
foreach ($this->parentClassesToRemove as $parentClassToRemove) {
|
|
if ($parentClassName !== $parentClassToRemove) {
|
|
continue;
|
|
}
|
|
// remove parent class
|
|
$node->extends = null;
|
|
return $node;
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* @param array<string, class-string[]> $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
$this->parentClassesToRemove = $configuration[self::PARENT_TYPES_TO_REMOVE] ?? [];
|
|
}
|
|
}
|