-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule to simplify a last useless variable assignment. (#2963)
* Add new rule to simplify a last useless variable assignment. The rule `SimplifyUselessLastVariableAssignRector` try to remove the latest variable assignment before the variable will return. Variable assignment and return, must be in the same scope. This rule is inspired by `SimplifyUselessVariableRector` but will not replace it. * Remove exception handling.
- Loading branch information
Showing
26 changed files
with
1,309 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ty/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/assign_ops.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\Fixture; | ||
|
||
function () { | ||
$b += 1; | ||
return $b; | ||
}; | ||
|
||
function () { | ||
$e /= 1; | ||
return $e; | ||
}; | ||
|
||
function () { | ||
$f **= 1; | ||
return $f; | ||
}; | ||
|
||
function () { | ||
$m .= 1; | ||
return $m; | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\Fixture; | ||
|
||
function () { | ||
return $b + 1; | ||
}; | ||
|
||
function () { | ||
return $e / 1; | ||
}; | ||
|
||
function () { | ||
return $f ** 1; | ||
}; | ||
|
||
function () { | ||
return $m . 1; | ||
}; | ||
|
||
?> |
36 changes: 36 additions & 0 deletions
36
...y/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/empty_array.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\Fixture; | ||
|
||
class EmptyArray | ||
{ | ||
public function run() | ||
{ | ||
$content = []; | ||
if (\rand(0, 1)) { | ||
echo ''; | ||
} | ||
|
||
return $content; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\Fixture; | ||
|
||
class EmptyArray | ||
{ | ||
public function run() | ||
{ | ||
if (\rand(0, 1)) { | ||
echo ''; | ||
} | ||
|
||
return []; | ||
} | ||
} | ||
|
||
?> |
91 changes: 91 additions & 0 deletions
91
...ality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\Fixture; | ||
|
||
function () { | ||
$a = true; | ||
return $a; | ||
}; | ||
|
||
function sameVariableInDifferentScope() | ||
{ | ||
$n = array_map(function () { | ||
return $n + 1; | ||
}, []); | ||
|
||
return $n; | ||
} | ||
|
||
function moreVariableOneWithoutAssigment() { | ||
$o++; | ||
$o = 10; | ||
|
||
return $o; | ||
} | ||
|
||
function assigmentAsFunctionParametr() { | ||
doSomething($p = 0); | ||
return $p; | ||
} | ||
|
||
function assigmentAfterAssignment() { | ||
doSomething($qq = $q = 0); | ||
return $q; | ||
} | ||
|
||
function () { | ||
$a = 1; | ||
$b = 1; | ||
$c = [ | ||
$b-- => $a++, | ||
--$b => ++$a, | ||
]; | ||
return $c; | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\Fixture; | ||
|
||
function () { | ||
return true; | ||
}; | ||
|
||
function sameVariableInDifferentScope() | ||
{ | ||
$n = array_map(function () { | ||
return $n + 1; | ||
}, []); | ||
|
||
return $n; | ||
} | ||
|
||
function moreVariableOneWithoutAssigment() { | ||
$o++; | ||
|
||
return 10; | ||
} | ||
|
||
function assigmentAsFunctionParametr() { | ||
doSomething($p = 0); | ||
return $p; | ||
} | ||
|
||
function assigmentAfterAssignment() { | ||
doSomething($qq = $q = 0); | ||
return $q; | ||
} | ||
|
||
function () { | ||
$a = 1; | ||
$b = 1; | ||
$c = [ | ||
$b-- => $a++, | ||
--$b => ++$a, | ||
]; | ||
return $c; | ||
}; | ||
|
||
?> |
Oops, something went wrong.