Find the best Rector rule to solve your problem


FlipNegatedTernaryInstanceofRector

Flip negated ternary of instanceof to direct use of object

-echo ! $object instanceof Product ? null : $object->getPrice();
+echo $object instanceof Product ? $object->getPrice() : null;
SETS:  Instanceof

RemoveDeadInstanceOfRector

Remove dead instanceof check on type hinted variable

 function run(stdClass $stdClass)
 {
-    if (! $stdClass instanceof stdClass) {
-        return false;
-    }
-
     return true;
 }

InlineIsAInstanceOfRector

Change is_a() with object and class name check to instanceof

 class SomeClass
 {
     public function run(object $object)
     {
-        return is_a($object, SomeType::class);
+        return $object instanceof SomeType;
     }
 }

FlipTypeControlToUseExclusiveTypeRector

Flip type control from null compare to use exclusive instanceof object

 function process(?DateTime $dateTime)
 {
-    if ($dateTime === null) {
+    if (! $dateTime instanceof DateTime) {
         return;
     }
 }

WhileNullableToInstanceofRector

Change while null compare to strict instanceof check

 final class SomeClass
 {
     public function run(?SomeClass $someClass)
     {
-        while ($someClass !== null) {
+        while ($someClass instanceof SomeClass) {
             // do something
         }
     }
 }
SETS:  Instanceof

BinaryOpNullableToInstanceofRector

Change && and || between nullable objects to instanceof compares

 function someFunction(?SomeClass $someClass)
 {
-    if ($someClass && $someClass->someMethod()) {
+    if ($someClass instanceof SomeClass && $someClass->someMethod()) {
         return 'yes';
     }

     return 'no';
 }
SETS:  Instanceof

EmptyOnNullableObjectToInstanceOfRector

Change empty() on nullable object to instanceof check

 class SomeClass
 {
     public function run(?AnotherObject $anotherObject)
     {
-        if (empty($anotherObject)) {
+        if (! $anotherObject instanceof AnotherObject) {
             return false;
         }

         return true;
     }
 }