Find the best Rector rule to solve your problem
Remove last return in the functions, since does not do anything
class SomeClass
{
public function run()
{
$shallWeDoThis = true;
if ($shallWeDoThis) {
return;
}
-
- return;
}
}
Removes non-existing @var annotations above the code
class SomeClass
{
public function get()
{
- /** @var Training[] $trainings */
return $this->getData();
}
}
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;
}
}
Remove and true that has no added value
class SomeClass
{
public function run()
{
- return true && 5 === 1;
+ return 5 === 1;
}
}
Remove @var annotation for PHPUnit\Framework\MockObject\MockObject combined with native object type
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class SomeTest extends TestCase
{
- /**
- * @var SomeClass|MockObject
- */
private SomeClass $someProperty;
}
Removes recasting of the same type
$string = '';
-$string = (string) $string;
+$string = $string;
$array = [];
-$array = (array) $array;
+$array = $array;
Remove dead condition above return
final class SomeClass
{
public function go()
{
- if (1 === 1) {
- return 'yes';
- }
-
return 'yes';
}
}
Remove unused private properties
class SomeClass
{
- private $property;
}
Remove unused @var annotation for properties
final class SomeClass
{
- /**
- * @var string
- */
public string $name = 'name';
}
Remove useless @readonly annotation on native readonly type
final class SomeClass
{
- /**
- * @readonly
- */
private readonly string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
Remove unused assigns to variables
class SomeClass
{
public function run()
{
- $value = 5;
}
}
Remove unreachable statements
class SomeClass
{
public function run()
{
return 5;
-
- $removeMe = 10;
}
}
Remove unneeded PHP_VERSION_ID conditional checks
class SomeClass
{
public function run()
{
- if (PHP_VERSION_ID < 80000) {
- return;
- }
-
echo 'do something';
}
}
Remove loop with no body
class SomeClass
{
public function run($values)
{
- for ($i=1; $i<count($values); ++$i) {
- }
}
}
Remove useless continue at the end of loops
while ($i < 10) {
++$i;
- continue;
}
Remove if, foreach and for that does not do anything
class SomeClass
{
public function run($value)
{
- if ($value) {
- }
-
- foreach ($values as $value) {
- }
-
return $value;
}
}
Remove initialization with null value from property declarations
class SunshineCommand extends ParentClassWithNewConstructor
{
- private $myVar = null;
+ private $myVar;
}
Remove dead try/catch
class SomeClass
{
public function run()
{
- try {
- // some code
- }
- catch (Throwable $throwable) {
- throw $throwable;
- }
}
}
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;
}
}
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;
}
}
}
Remove unused class constants
class SomeClass
{
- private const SOME_CONST = 'dead';
-
public function run()
{
}
}
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;
}
}
Removes dead code statements
-$value = 5;
-$value;
+$value = 5;
Removes unneeded $value = $value assigns
function run() {
- $result = $result;
}
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;
}
}
}
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+';
Remove if/else if they have same content
class SomeClass
{
public function run()
{
- if (true) {
- return 1;
- } else {
- return 1;
- }
+ return 1;
}
}
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;
}
}
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';
}
}
Remove if condition that is always true
final class SomeClass
{
public function go()
{
- if (1 === 1) {
- return 'yes';
- }
+ return 'yes';
return 'no';
}
}
Remove dead instanceof check on type hinted variable
function run(stdClass $stdClass)
{
- if (! $stdClass instanceof stdClass) {
- return false;
- }
-
return true;
}
Remove unused key in foreach
$items = [];
-foreach ($items as $key => $value) {
+foreach ($items as $value) {
$result = $value;
}
Remove duplicated key in defined arrays.
$item = [
- 1 => 'A',
1 => 'B'
];
Remove @return docblock with same type as defined in PHP
use stdClass;
class SomeClass
{
- /**
- * @return stdClass
- */
public function foo(): stdClass
{
}
}
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();
}
}
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;
}
}
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)
{
}
}
Remove @var/@param/@return null docblock
class SomeClass
{
- /**
- * @return null
- */
public function foo()
{
return null;
}
}
Remove unused promoted property
class SomeClass
{
public function __construct(
- private $someUnusedDependency,
private $usedDependency
) {
}
public function getUsedDependency()
{
return $this->usedDependency;
}
}
Remove unused parameter in constructor
final class SomeClass
{
private $hey;
- public function __construct($hey, $man)
+ public function __construct($hey)
{
$this->hey = $hey;
}
}
Remove empty class methods not required by parents
class OrphanClass
{
- public function __construct()
- {
- }
}
Remove unused private method
final class SomeController
{
public function run()
{
return 5;
}
-
- private function skip()
- {
- return 10;
- }
}
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;
}
}
Removes useless variable assigns
function () {
- $a = true;
- return $a;
+ return true;
};