mirror of https://github.com/rectorphp/rector.git
78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Php74\Rector\StaticCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Arg;
|
|
use PhpParser\Node\Expr\Cast\String_;
|
|
use PhpParser\Node\Expr\New_;
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
use PhpParser\Node\Name;
|
|
use PHPStan\Type\ObjectType;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @changelog https://wiki.php.net/rfc/deprecations_php_7_4 (not confirmed yet)
|
|
* @changelog https://3v4l.org/RTCUq
|
|
* @see \Rector\Tests\Php74\Rector\StaticCall\ExportToReflectionFunctionRector\ExportToReflectionFunctionRectorTest
|
|
*/
|
|
final class ExportToReflectionFunctionRector extends AbstractRector implements MinPhpVersionInterface
|
|
{
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return PhpVersionFeature::EXPORT_TO_REFLECTION_FUNCTION;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Change export() to ReflectionFunction alternatives', [new CodeSample(<<<'CODE_SAMPLE'
|
|
$reflectionFunction = ReflectionFunction::export('foo');
|
|
$reflectionFunctionAsString = ReflectionFunction::export('foo', true);
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
$reflectionFunction = new ReflectionFunction('foo');
|
|
$reflectionFunctionAsString = (string) new ReflectionFunction('foo');
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [StaticCall::class];
|
|
}
|
|
/**
|
|
* @param StaticCall $node
|
|
*/
|
|
public function refactor(Node $node) : ?Node
|
|
{
|
|
if (!$node->class instanceof Name) {
|
|
return null;
|
|
}
|
|
$callerType = $this->nodeTypeResolver->getType($node->class);
|
|
if (!$callerType->isSuperTypeOf(new ObjectType('ReflectionFunction'))->yes()) {
|
|
return null;
|
|
}
|
|
if (!$this->isName($node->name, 'export')) {
|
|
return null;
|
|
}
|
|
$firstArg = $node->getArgs()[0] ?? null;
|
|
if (!$firstArg instanceof Arg) {
|
|
return null;
|
|
}
|
|
$new = new New_($node->class, [new Arg($firstArg->value)]);
|
|
$secondArg = $node->getArgs()[1] ?? null;
|
|
if (!$secondArg instanceof Arg) {
|
|
return $new;
|
|
}
|
|
if ($this->valueResolver->isTrue($secondArg->value)) {
|
|
return new String_($new);
|
|
}
|
|
return $new;
|
|
}
|
|
}
|