Skip to content

Commit

Permalink
[Php80] Add $object::class support on GetDebugTypeRector (#3039)
Browse files Browse the repository at this point in the history
* [Php80] Add $object::class support already on GetDebugTypeRector

* implemented 🎉

* Final touch: clean up and add more fixtures

* Really Final touch: clean up, values identical already checked in areValuesIdentical() method

* Really final touch: method rename
  • Loading branch information
samsonasik committed Nov 8, 2022
1 parent 10346ee commit 10c6a30
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\GetDebugTypeRector\Fixture;

class ClassConstFetchClass
{
public function run($value)
{
return is_object($value) ? $value::class : gettype($value);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Ternary\GetDebugTypeRector\Fixture;

class ClassConstFetchClass
{
public function run($value)
{
return get_debug_type($value);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\GetDebugTypeRector\Fixture;

class SkipClassConstFetchIfDifferentName
{
public function run($value)
{
return is_object($value) ? $value::SOME_CONSTANT : gettype($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\GetDebugTypeRector\Fixture;

class SkipClassConstFetchIfDifferentObject
{
public function run($value, $differentObject)
{
return is_object($value) ? $differentObject::class : gettype($value);
}
}
60 changes: 40 additions & 20 deletions rules/Php80/Rector/Ternary/GetDebugTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

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\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand Down Expand Up @@ -77,17 +79,11 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var FuncCall $funcCall */
$funcCall = $node->if;
if (! isset($funcCall->args[0])) {
return null;
}

if (! $funcCall->args[0] instanceof Arg) {
return null;
}

$firstExpr = $funcCall->args[0]->value;
/** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */
$getClassFuncCallOrClassConstFetchClass = $node->if;
$firstExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall
? $getClassFuncCallOrClassConstFetchClass->args[0]->value
: $getClassFuncCallOrClassConstFetchClass->class;

return $this->nodeFactory->createFuncCall('get_debug_type', [$firstExpr]);
}
Expand All @@ -98,12 +94,24 @@ private function shouldSkip(Ternary $ternary): bool
return true;
}

if ($ternary->cond->isFirstClassCallable()) {
return true;
}

if (! isset($ternary->cond->args[0])) {
return true;
}

if (! $this->nodeNameResolver->isName($ternary->cond, 'is_object')) {
return true;
}

if (! $ternary->if instanceof FuncCall) {
return true;
if (! $ternary->if instanceof ClassConstFetch) {
return true;
}

return $this->shouldSkipClassConstFetch($ternary->if);
}

if (! $this->nodeNameResolver->isName($ternary->if, 'get_class')) {
Expand All @@ -114,26 +122,38 @@ private function shouldSkip(Ternary $ternary): bool
return true;
}

if ($ternary->else->isFirstClassCallable()) {
return true;
}

return ! $this->nodeNameResolver->isName($ternary->else, 'gettype');
}

private function shouldSkipClassConstFetch(ClassConstFetch $classConstFetch): bool
{
if (! $classConstFetch->name instanceof Identifier) {
return true;
}

return $classConstFetch->name->toString() !== 'class';
}

private function areValuesIdentical(Ternary $ternary): bool
{
/** @var FuncCall $isObjectFuncCall */
$isObjectFuncCall = $ternary->cond;
if (! $isObjectFuncCall->args[0] instanceof Arg) {
return false;
}

$firstExpr = $isObjectFuncCall->args[0]->value;

/** @var FuncCall $getClassFuncCall */
$getClassFuncCall = $ternary->if;
if (! $getClassFuncCall->args[0] instanceof Arg) {
/** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */
$getClassFuncCallOrClassConstFetchClass = $ternary->if;

if ($getClassFuncCallOrClassConstFetchClass instanceof FuncCall && ! $getClassFuncCallOrClassConstFetchClass->args[0] instanceof Arg) {
return false;
}

$secondExpr = $getClassFuncCall->args[0]->value;
$secondExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall
? $getClassFuncCallOrClassConstFetchClass->args[0]->value
: $getClassFuncCallOrClassConstFetchClass->class;

/** @var FuncCall $gettypeFuncCall */
$gettypeFuncCall = $ternary->else;
Expand Down

0 comments on commit 10c6a30

Please sign in to comment.