diff --git a/config/set/php/php80.yaml b/config/set/php/php80.yaml index ca8b234a748..63ed05d43b7 100644 --- a/config/set/php/php80.yaml +++ b/config/set/php/php80.yaml @@ -6,3 +6,4 @@ services: Rector\Php80\Rector\Class_\StringableForToStringRector: null Rector\Php80\Rector\Class_\AnnotationToAttributeRector: null Rector\Php80\Rector\FuncCall\ClassOnObjectRector: null + Rector\Php80\Rector\Ternary\GetDebugTypeRector: null diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index a5f65225563..6d7602d79cd 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -1,4 +1,4 @@ -# All 506 Rectors Overview +# All 507 Rectors Overview - [Projects](#projects) - [General](#general) @@ -7940,6 +7940,26 @@ Change get_class($object) to faster $object::class
+### `GetDebugTypeRector` + +- class: [`Rector\Php80\Rector\Ternary\GetDebugTypeRector`](/../master/rules/php80/src/Rector/Ternary/GetDebugTypeRector.php) +- [test fixtures](/../master/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/Fixture) + +Change ternary type resolve to get_debug_type() + +```diff + class SomeClass + { + public function run($value) + { +- return is_object($value) ? get_class($value) : gettype($value); ++ return get_debug_type($value); + } + } +``` + +
+ ### `StrContainsRector` - class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector`](/../master/rules/php80/src/Rector/NotIdentical/StrContainsRector.php) diff --git a/rules/php80/src/Rector/Ternary/GetDebugTypeRector.php b/rules/php80/src/Rector/Ternary/GetDebugTypeRector.php new file mode 100644 index 00000000000..c9cdde11d18 --- /dev/null +++ b/rules/php80/src/Rector/Ternary/GetDebugTypeRector.php @@ -0,0 +1,114 @@ +shouldSkip($node)) { + return null; + } + + if (! $this->areValuesIdentical($node)) { + return null; + } + + /** @var FuncCall $funcCall */ + $funcCall = $node->if; + $firstExpr = $funcCall->args[0]->value; + + return $this->createFuncCall('get_debug_type', [$firstExpr]); + } + + private function shouldSkip(Ternary $ternary): bool + { + if (! $this->isFuncCallName($ternary->cond, 'is_object')) { + return true; + } + + if ($ternary->if === null) { + return true; + } + + if (! $this->isFuncCallName($ternary->if, 'get_class')) { + return true; + } + + return ! $this->isFuncCallName($ternary->else, 'gettype'); + } + + private function areValuesIdentical(Ternary $ternary): bool + { + /** @var FuncCall $isObjectFuncCall */ + $isObjectFuncCall = $ternary->cond; + $firstExpr = $isObjectFuncCall->args[0]->value; + + /** @var FuncCall $getClassFuncCall */ + $getClassFuncCall = $ternary->if; + $secondExpr = $getClassFuncCall->args[0]->value; + + /** @var FuncCall $gettypeFuncCall */ + $gettypeFuncCall = $ternary->else; + $thirdExpr = $gettypeFuncCall->args[0]->value; + + if (! $this->areNodesEqual($firstExpr, $secondExpr)) { + return false; + } + + return $this->areNodesEqual($firstExpr, $thirdExpr); + } +} diff --git a/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/Fixture/fixture.php.inc b/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..7e4e5aa3075 --- /dev/null +++ b/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/GetDebugTypeRectorTest.php b/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/GetDebugTypeRectorTest.php new file mode 100644 index 00000000000..3daa3a46f10 --- /dev/null +++ b/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/GetDebugTypeRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return GetDebugTypeRector::class; + } +}