mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
96112cb1f0
2da49992cc
[Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
64 lines
2.3 KiB
PHP
64 lines
2.3 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 Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @changelog https://wiki.php.net/rfc/deprecations_php_7_4 (not confirmed yet)
|
|
* @see https://3v4l.org/RTCUq
|
|
* @see \Rector\Tests\Php74\Rector\StaticCall\ExportToReflectionFunctionRector\ExportToReflectionFunctionRectorTest
|
|
*/
|
|
final class ExportToReflectionFunctionRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change export() to ReflectionFunction alternatives', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 [\PhpParser\Node\Expr\StaticCall::class];
|
|
}
|
|
/**
|
|
* @param StaticCall $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$node->class instanceof \PhpParser\Node\Name) {
|
|
return null;
|
|
}
|
|
$callerType = $this->nodeTypeResolver->resolve($node->class);
|
|
if (!$callerType->isSuperTypeOf(new \PHPStan\Type\ObjectType('ReflectionFunction'))->yes()) {
|
|
return null;
|
|
}
|
|
if (!$this->isName($node->name, 'export')) {
|
|
return null;
|
|
}
|
|
$new = new \PhpParser\Node\Expr\New_($node->class, [new \PhpParser\Node\Arg($node->args[0]->value)]);
|
|
if (isset($node->args[1]) && $this->valueResolver->isTrue($node->args[1]->value)) {
|
|
return new \PhpParser\Node\Expr\Cast\String_($new);
|
|
}
|
|
return $new;
|
|
}
|
|
}
|