Find the best Rector rule to solve your problem
Flip negated ternary of instanceof to direct use of object
-echo ! $object instanceof Product ? null : $object->getPrice();
+echo $object instanceof Product ? $object->getPrice() : null;
Remove dead instanceof check on type hinted variable
function run(stdClass $stdClass)
{
- if (! $stdClass instanceof stdClass) {
- return false;
- }
-
return true;
}
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;
}
}
Flip type control from null compare to use exclusive instanceof object
function process(?DateTime $dateTime)
{
- if ($dateTime === null) {
+ if (! $dateTime instanceof DateTime) {
return;
}
}
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
}
}
}
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';
}
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;
}
}