Find the best Rector rule to solve your problem. Searching through 671 rules.

Found 10 rules:

ExplicitNullableParamTypeRector

Make implicit nullable param to explicit

-function foo(string $param = null) {}
+function foo(?string $param = null) {}
SETS:  PHP 8.4

NullableCompareToNullRector

Changes negate of empty comparison of nullable value to explicit === or !== compare

 /** @var stdClass|null $value */
-if ($value) {
+if ($value !== null) {
 }

-if (!$value) {
+if ($value === null) {
 }
SETS:  Coding Style

RestoreDefaultNullToNullableTypePropertyRector

Add null default to properties with PHP 7.4 property nullable type

 class SomeClass
 {
-    public ?string $name;
+    public ?string $name = null;
 }
SETS:  PHP 7.4

SimplifyIfNullableReturnRector

Direct return on if nullable check before return

 class SomeClass
 {
     public function run()
     {
-        $value = $this->get();
-        if (! $value instanceof \stdClass) {
-            return null;
-        }
-
-        return $value;
+        return $this->get();
     }

     public function get(): ?stdClass {
     }
 }
SETS:  Code Quality

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;
     }
 }

ReturnNullableTypeRector

Add basic ? nullable type to class methods and functions, as of PHP 7.1

 final class SomeClass
 {
-    public function getData()
+    public function getData(): ?int
     {
         if (rand(0, 1)) {
             return null;
         }

         return 100;
     }
 }

AssertEmptyNullableObjectToAssertInstanceofRector

Change assertNotEmpty() and assertNotNull() on an object to more clear assertInstanceof()

 use PHPUnit\Framework\TestCase;

 class SomeClass extends TestCase
 {
     public function test()
     {
         $someObject = new stdClass();

-        $this->assertNotEmpty($someObject);
+        $this->assertInstanceof(stdClass::class, $someObject);
     }
 }
SETS:  Code Quality

AddInstanceofAssertForNullableInstanceRector

Add explicit instance assert between nullable object assign and method call on nullable object (spotted by PHPStan)

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function test()
     {
         $someObject = $this->getSomeObject();
+        $this->assertInstanceOf(SomeClass::class, $someObject);

         $value = $someObject->getSomeMethod();
     }

     private function getSomeObject(): ?SomeClass
     {
         if (mt_rand(0, 1)) {
             return new SomeClass();
         }

         return null;
     }
 }
SETS:  Code Quality