Find the best Rector rule to solve your problem


RemoveDeadReturnRector

Remove last return in the functions, since does not do anything

 class SomeClass
 {
     public function run()
     {
         $shallWeDoThis = true;

         if ($shallWeDoThis) {
             return;
         }
-
-        return;
     }
 }
SETS:  Dead Code

RemoveNonExistingVarAnnotationRector

Removes non-existing @var annotations above the code

 class SomeClass
 {
     public function get()
     {
-        /** @var Training[] $trainings */
         return $this->getData();
     }
 }
SETS:  Dead Code

RemoveDeadZeroAndOneOperationRector

Remove operation with 1 and 0, that have no effect on the value

 class SomeClass
 {
     public function run()
     {
-        $value = 5 * 1;
-        $value = 5 + 0;
+        $value = 5;
+        $value = 5;
     }
 }
SETS:  Dead Code

RemoveAndTrueRector

Remove and true that has no added value

 class SomeClass
 {
     public function run()
     {
-        return true && 5 === 1;
+        return 5 === 1;
     }
 }
SETS:  Dead Code

RecastingRemovalRector

Removes recasting of the same type

 $string = '';
-$string = (string) $string;
+$string = $string;

 $array = [];
-$array = (array) $array;
+$array = $array;
SETS:  Dead Code

RemoveParentCallWithoutParentRector

Remove unused parent call with no parent class

 class OrphanClass
 {
     public function __construct()
     {
-         parent::__construct();
     }
 }
SETS:  PHP 8.0 Dead Code

RemoveDeadConditionAboveReturnRector

Remove dead condition above return

 final class SomeClass
 {
     public function go()
     {
-        if (1 === 1) {
-            return 'yes';
-        }
-
         return 'yes';
     }
 }
SETS:  Dead Code

RemoveUnusedPrivatePropertyRector

Remove unused private properties

 class SomeClass
 {
-    private $property;
 }
SETS:  Dead Code

RemoveUselessVarTagRector

Remove unused @var annotation for properties

 final class SomeClass
 {
-    /**
-     * @var string
-     */
     public string $name = 'name';
 }
SETS:  Dead Code

RemoveUselessReadOnlyTagRector

Remove useless @readonly annotation on native readonly type

 final class SomeClass
 {
-    /**
-     * @readonly
-     */
     private readonly string $name;

     public function __construct(string $name)
     {
         $this->name = $name;
     }
 }
SETS:  Dead Code

RemoveDoubleAssignRector

Simplify useless double assigns

-$value = 1;
 $value = 1;
SETS:  Dead Code

RemoveUnusedVariableAssignRector

Remove unused assigns to variables

 class SomeClass
 {
     public function run()
     {
-        $value = 5;
     }
 }
SETS:  Dead Code

RemoveUnreachableStatementRector

Remove unreachable statements

 class SomeClass
 {
     public function run()
     {
         return 5;
-
-        $removeMe = 10;
     }
 }
SETS:  Dead Code

RemovePhpVersionIdCheckRector

Remove unneeded PHP_VERSION_ID conditional checks

 class SomeClass
 {
     public function run()
     {
-        if (PHP_VERSION_ID < 80000) {
-            return;
-        }
-
         echo 'do something';
     }
 }
SETS:  Dead Code

RemoveDeadLoopRector

Remove loop with no body

 class SomeClass
 {
     public function run($values)
     {
-        for ($i=1; $i<count($values); ++$i) {
-        }
     }
 }
SETS:  Dead Code

RemoveDeadContinueRector

Remove useless continue at the end of loops

 while ($i < 10) {
     ++$i;
-    continue;
 }
SETS:  Dead Code

RemoveDeadIfForeachForRector

Remove if, foreach and for that does not do anything

 class SomeClass
 {
     public function run($value)
     {
-        if ($value) {
-        }
-
-        foreach ($values as $value) {
-        }
-
         return $value;
     }
 }
SETS:  Dead Code

RemoveNullPropertyInitializationRector

Remove initialization with null value from property declarations

 class SunshineCommand extends ParentClassWithNewConstructor
 {
-    private $myVar = null;
+    private $myVar;
 }
SETS:  Dead Code

RemoveDeadTryCatchRector

Remove dead try/catch

 class SomeClass
 {
     public function run()
     {
-        try {
-            // some code
-        }
-        catch (Throwable $throwable) {
-            throw $throwable;
-        }
     }
 }
SETS:  Dead Code

TernaryToBooleanOrFalseToBooleanAndRector

Change ternary of bool : false to && bool

 class SomeClass
 {
     public function go()
     {
-        return $value ? $this->getBool() : false;
+        return $value && $this->getBool();
     }

     private function getBool(): bool
     {
         return (bool) 5;
     }
 }
SETS:  Dead Code

RemoveDuplicatedCaseInSwitchRector

2 following switch keys with identical will be reduced to one result

 class SomeClass
 {
     public function run()
     {
         switch ($name) {
              case 'clearHeader':
                  return $this->modifyHeader($node, 'remove');
              case 'clearAllHeaders':
-                 return $this->modifyHeader($node, 'replace');
              case 'clearRawHeaders':
                  return $this->modifyHeader($node, 'replace');
              case '...':
                  return 5;
         }
     }
 }
SETS:  Dead Code

RemoveUnusedPrivateClassConstantRector

Remove unused class constants

 class SomeClass
 {
-    private const SOME_CONST = 'dead';
-
     public function run()
     {
     }
 }
SETS:  Dead Code

RemoveConcatAutocastRector

Remove (string) casting when it comes to concat, that does this by default

 class SomeConcatingClass
 {
     public function run($value)
     {
-        return 'hi ' . (string) $value;
+        return 'hi ' . $value;
     }
 }
SETS:  Dead Code

RemoveDeadStmtRector

Removes dead code statements

-$value = 5;
-$value;
+$value = 5;
SETS:  Dead Code

SimplifyMirrorAssignRector

Removes unneeded $value = $value assigns

 function run() {
-    $result = $result;
 }
SETS:  Dead Code

RemoveUnusedNonEmptyArrayBeforeForeachRector

Remove unused if check to non-empty array before foreach of the array

 class SomeClass
 {
     public function run()
     {
         $values = [];
-        if ($values !== []) {
-            foreach ($values as $value) {
-                echo $value;
-            }
+        foreach ($values as $value) {
+            echo $value;
         }
     }
 }
SETS:  Dead Code

UnwrapFutureCompatibleIfPhpVersionRector

Remove php version checks if they are passed

 // current PHP: 7.2
-if (version_compare(PHP_VERSION, '7.2', '<')) {
-    return 'is PHP 7.1-';
-} else {
-    return 'is PHP 7.2+';
-}
+return 'is PHP 7.2+';
SETS:  Dead Code

SimplifyIfElseWithSameContentRector

Remove if/else if they have same content

 class SomeClass
 {
     public function run()
     {
-        if (true) {
-            return 1;
-        } else {
-            return 1;
-        }
+        return 1;
     }
 }
SETS:  Dead Code

RemoveTypedPropertyDeadInstanceOfRector

Remove dead instanceof check on type hinted property

 final class SomeClass
 {
     private $someObject;

     public function __construct(SomeObject $someObject)
     {
         $this->someObject = $someObject;
     }

     public function run()
     {
-        if ($this->someObject instanceof SomeObject) {
-            return true;
-        }
-
-        return false;
+        return true;
     }
 }
SETS:  Dead Code

ReduceAlwaysFalseIfOrRector

Reduce always false in a if ( || ) condition

 class SomeClass
 {
     public function run(int $number)
     {
-        if (! is_int($number) || $number > 50) {
+        if ($number > 50) {
             return 'yes';
         }

         return 'no';
     }
 }
SETS:  Dead Code

RemoveAlwaysTrueIfConditionRector

Remove if condition that is always true

 final class SomeClass
 {
     public function go()
     {
-        if (1 === 1) {
-            return 'yes';
-        }
+        return 'yes';

         return 'no';
     }
 }
SETS:  Dead Code

RemoveDeadInstanceOfRector

Remove dead instanceof check on type hinted variable

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

RemoveUnusedForeachKeyRector

Remove unused key in foreach

 $items = [];
-foreach ($items as $key => $value) {
+foreach ($items as $value) {
     $result = $value;
 }
SETS:  Dead Code

RemoveDuplicatedArrayKeyRector

Remove duplicated key in defined arrays.

 $item = [
-    1 => 'A',
     1 => 'B'
 ];
SETS:  Dead Code

RemoveUselessReturnTagRector

Remove @return docblock with same type as defined in PHP

 use stdClass;

 class SomeClass
 {
-    /**
-     * @return stdClass
-     */
     public function foo(): stdClass
     {
     }
 }
SETS:  Dead Code

RemoveUselessReturnExprInConstructRector

Remove useless return Expr in __construct()

 class SomeClass
 {
     public function __construct()
     {
         if (rand(0, 1)) {
             $this->init();
-            return true;
+            return;
         }

         if (rand(2, 3)) {
-            return parent::construct();
+            parent::construct();
+            return;
         }

         $this->execute();
     }
 }
SETS:  Dead Code

RemoveUnusedPublicMethodParameterRector

Remove unused parameter in public method on final class without extends and interface

 final class SomeClass
 {
-    public function run($a, $b)
+    public function run($a)
     {
         echo $a;
     }
 }
SETS:  Dead Code

RemoveUselessParamTagRector

Remove @param docblock with same type as parameter type

 class SomeClass
 {
     /**
-     * @param string $a
      * @param string $b description
      */
     public function foo(string $a, string $b)
     {
     }
 }
SETS:  Dead Code

RemoveNullTagValueNodeRector

Remove @var/@param/@return null docblock

 class SomeClass
 {
-    /**
-     * @return null
-     */
     public function foo()
     {
         return null;
     }
 }
SETS:  Dead Code

RemoveUnusedPromotedPropertyRector

Remove unused promoted property

 class SomeClass
 {
     public function __construct(
-        private $someUnusedDependency,
         private $usedDependency
     ) {
     }

     public function getUsedDependency()
     {
         return $this->usedDependency;
     }
 }
SETS:  Dead Code

RemoveUnusedConstructorParamRector

Remove unused parameter in constructor

 final class SomeClass
 {
     private $hey;

-    public function __construct($hey, $man)
+    public function __construct($hey)
     {
         $this->hey = $hey;
     }
 }
SETS:  Dead Code

RemoveEmptyClassMethodRector

Remove empty class methods not required by parents

 class OrphanClass
 {
-    public function __construct()
-    {
-    }
 }
SETS:  Dead Code

RemoveUnusedPrivateMethodRector

Remove unused private method

 final class SomeController
 {
     public function run()
     {
         return 5;
     }
-
-    private function skip()
-    {
-        return 10;
-    }
 }
SETS:  Dead Code

RemoveUnusedPrivateMethodParameterRector

Remove unused parameter, if not required by interface or parent class

 class SomeClass
 {
-    private function run($value, $value2)
+    private function run($value)
     {
          $this->value = $value;
     }
 }
SETS:  Dead Code

Configurable

SimplifyUselessVariableRector

Removes useless variable assigns

 function () {
-    $a = true;
-    return $a;
+    return true;
 };
SETS:  Dead Code