rector/rules/Php80/Rector/Ternary/GetDebugTypeRector.php

145 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2020-04-29 21:23:14 +00:00
<?php
declare (strict_types=1);
namespace Rector\Php80\Rector\Ternary;
2020-04-29 21:23:14 +00:00
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2020-04-29 21:23:14 +00:00
/**
2021-04-10 18:47:17 +00:00
* @changelog https://wiki.php.net/rfc/get_debug_type
2020-04-29 21:23:14 +00:00
*
* @see \Rector\Tests\Php80\Rector\Ternary\GetDebugTypeRector\GetDebugTypeRectorTest
2020-04-29 21:23:14 +00:00
*/
final class GetDebugTypeRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
2020-04-29 21:23:14 +00:00
{
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::GET_DEBUG_TYPE;
}
public function getRuleDefinition() : RuleDefinition
2020-04-29 21:23:14 +00:00
{
return new RuleDefinition('Change ternary type resolve to get_debug_type()', [new CodeSample(<<<'CODE_SAMPLE'
2020-04-29 21:23:14 +00:00
class SomeClass
{
public function run($value)
{
return is_object($value) ? get_class($value) : gettype($value);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2020-04-29 21:23:14 +00:00
class SomeClass
{
public function run($value)
{
return get_debug_type($value);
}
}
CODE_SAMPLE
)]);
2020-04-29 21:23:14 +00:00
}
/**
* @return array<class-string<Node>>
2020-04-29 21:23:14 +00:00
*/
public function getNodeTypes() : array
2020-04-29 21:23:14 +00:00
{
return [Ternary::class];
2020-04-29 21:23:14 +00:00
}
/**
* @param Ternary $node
2020-04-29 21:23:14 +00:00
*/
public function refactor(Node $node) : ?Node
2020-04-29 21:23:14 +00:00
{
if ($this->shouldSkip($node)) {
return null;
}
if (!$this->areValuesIdentical($node)) {
2020-04-29 21:23:14 +00:00
return null;
}
/** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */
$getClassFuncCallOrClassConstFetchClass = $node->if;
$firstExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall ? $getClassFuncCallOrClassConstFetchClass->getArgs()[0]->value : $getClassFuncCallOrClassConstFetchClass->class;
2021-01-30 21:41:25 +00:00
return $this->nodeFactory->createFuncCall('get_debug_type', [$firstExpr]);
2020-04-29 21:23:14 +00:00
}
public function providePolyfillPackage() : string
{
return PolyfillPackage::PHP_80;
}
private function shouldSkip(Ternary $ternary) : bool
2020-04-29 21:23:14 +00:00
{
if (!$ternary->cond instanceof FuncCall) {
return \true;
2020-04-29 21:23:14 +00:00
}
if ($ternary->cond->isFirstClassCallable()) {
return \true;
}
if (!isset($ternary->cond->getArgs()[0])) {
return \true;
}
if (!$this->nodeNameResolver->isName($ternary->cond, 'is_object')) {
return \true;
2020-04-29 21:23:14 +00:00
}
if (!$ternary->if instanceof FuncCall) {
if (!$ternary->if instanceof ClassConstFetch) {
return \true;
}
return $this->shouldSkipClassConstFetch($ternary->if);
2020-04-29 21:23:14 +00:00
}
if (!$this->nodeNameResolver->isName($ternary->if, 'get_class')) {
return \true;
}
if (!$ternary->else instanceof FuncCall) {
return \true;
}
if ($ternary->else->isFirstClassCallable()) {
return \true;
}
return !$this->nodeNameResolver->isName($ternary->else, 'gettype');
2020-04-29 21:23:14 +00:00
}
private function shouldSkipClassConstFetch(ClassConstFetch $classConstFetch) : bool
{
if (!$classConstFetch->name instanceof Identifier) {
return \true;
}
return $classConstFetch->name->toString() !== 'class';
}
private function areValuesIdentical(Ternary $ternary) : bool
2020-04-29 21:23:14 +00:00
{
/** @var FuncCall $isObjectFuncCall */
$isObjectFuncCall = $ternary->cond;
if ($isObjectFuncCall->isFirstClassCallable()) {
return \false;
}
$firstExpr = $isObjectFuncCall->getArgs()[0]->value;
/** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */
$getClassFuncCallOrClassConstFetchClass = $ternary->if;
if ($getClassFuncCallOrClassConstFetchClass instanceof FuncCall && !$getClassFuncCallOrClassConstFetchClass->args[0] instanceof Arg) {
return \false;
}
$secondExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall ? $getClassFuncCallOrClassConstFetchClass->getArgs()[0]->value : $getClassFuncCallOrClassConstFetchClass->class;
2020-04-29 21:23:14 +00:00
$gettypeFuncCall = $ternary->else;
if (!$gettypeFuncCall instanceof FuncCall) {
return \false;
}
if (!$gettypeFuncCall->args[0] instanceof Arg) {
return \false;
}
2020-04-29 21:23:14 +00:00
$thirdExpr = $gettypeFuncCall->args[0]->value;
if (!$this->nodeComparator->areNodesEqual($firstExpr, $secondExpr)) {
return \false;
2020-04-29 21:23:14 +00:00
}
return $this->nodeComparator->areNodesEqual($firstExpr, $thirdExpr);
2020-04-29 21:23:14 +00:00
}
}