Skip to content

Commit

Permalink
[DeadCode] Ensure no stmts check on RemovePhpVersionIdCheckRector (#2260
Browse files Browse the repository at this point in the history
)

* [DeadCode] Ensure no stmts check on RemovePhpVersionIdCheckRector

* [DeadCode] Ensure no stmts check on RemovePhpVersionIdCheckRector

* Fixed 🎉

* more fixture
  • Loading branch information
samsonasik committed May 7, 2022
1 parent 2dd8c6b commit fb8260c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector\Fixture;

class NoStmts
{
public function run()
{
if (PHP_VERSION_ID > 80000) {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector\Fixture;

class NoStmts
{
public function run()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector\Fixture;

class NoStmts2
{
public function run()
{
if (80000 > PHP_VERSION_ID) {

}

echo 'do something';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector\Fixture;

class NoStmts2
{
public function run()
{
echo 'do something';
}
}

?>
83 changes: 46 additions & 37 deletions rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public function getNodeTypes(): array

/**
* @param If_ $node
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
public function refactor(Node $node): ?array
public function refactor(Node $node): null|If_|array
{
/**
* $this->phpVersionProvider->provide() fallback is here as $currentFileProvider must be accessed after initialization
Expand Down Expand Up @@ -128,9 +128,9 @@ public function refactor(Node $node): ?array
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function processSmaller(ConstFetch $constFetch, Smaller $smaller, If_ $if): ?array
private function processSmaller(ConstFetch $constFetch, Smaller $smaller, If_ $if): null|If_|array
{
if ($smaller->left === $constFetch) {
return $this->processSmallerLeft($smaller, $if);
Expand All @@ -144,13 +144,13 @@ private function processSmaller(ConstFetch $constFetch, Smaller $smaller, If_ $i
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function processGreaterOrEqual(
ConstFetch $constFetch,
GreaterOrEqual $greaterOrEqual,
If_ $if,
): ?array {
): null|If_|array {
if ($greaterOrEqual->left === $constFetch) {
return $this->processGreaterOrEqualLeft($greaterOrEqual, $if);
}
Expand All @@ -162,10 +162,7 @@ private function processGreaterOrEqual(
return null;
}

/**
* @return null
*/
private function processSmallerLeft(Smaller $smaller, If_ $if)
private function processSmallerLeft(Smaller $smaller, If_ $if): ?If_
{
$value = $smaller->right;
if (! $value instanceof LNumber) {
Expand All @@ -174,49 +171,57 @@ private function processSmallerLeft(Smaller $smaller, If_ $if)

if ($this->phpVersion >= $value->value) {
$this->removeNode($if);
return $if;
}

return null;
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function processSmallerRight(Smaller $smaller, If_ $if): ?array
private function processSmallerRight(Smaller $smaller, If_ $if): null|If_|array
{
$value = $smaller->left;
if (! $value instanceof LNumber) {
return null;
}

if ($this->phpVersion >= $value->value) {
return $if->stmts;
if ($this->phpVersion < $value->value) {
return null;
}

return null;
if ($if->stmts === []) {
$this->removeNode($if);
return $if;
}

return $if->stmts;
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function processGreaterOrEqualLeft(GreaterOrEqual $greaterOrEqual, If_ $if): array|null
private function processGreaterOrEqualLeft(GreaterOrEqual $greaterOrEqual, If_ $if): null|If_|array
{
$value = $greaterOrEqual->right;
if (! $value instanceof LNumber) {
return null;
}

if ($this->phpVersion >= $value->value) {
return $if->stmts;
if ($this->phpVersion < $value->value) {
return null;
}

return null;
if ($if->stmts === []) {
$this->removeNode($if);
return $if;
}

return $if->stmts;
}

/**
* @return null
*/
private function processGreaterOrEqualRight(GreaterOrEqual $greaterOrEqual, If_ $if)
private function processGreaterOrEqualRight(GreaterOrEqual $greaterOrEqual, If_ $if): ?If_
{
$value = $greaterOrEqual->left;
if (! $value instanceof LNumber) {
Expand All @@ -225,15 +230,16 @@ private function processGreaterOrEqualRight(GreaterOrEqual $greaterOrEqual, If_

if ($this->phpVersion >= $value->value) {
$this->removeNode($if);
return $if;
}

return null;
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function processGreater(ConstFetch $constFetch, Greater $greater, If_ $if): ?array
private function processGreater(ConstFetch $constFetch, Greater $greater, If_ $if): null|If_|array
{
if ($greater->left === $constFetch) {
return $this->processGreaterLeft($greater, $if);
Expand All @@ -247,26 +253,28 @@ private function processGreater(ConstFetch $constFetch, Greater $greater, If_ $i
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function processGreaterLeft(Greater $greater, If_ $if): ?array
private function processGreaterLeft(Greater $greater, If_ $if): null|If_|array
{
$value = $greater->right;
if (! $value instanceof LNumber) {
return null;
}

if ($this->phpVersion >= $value->value) {
return $if->stmts;
if ($this->phpVersion < $value->value) {
return null;
}

return null;
if ($if->stmts === []) {
$this->removeNode($if);
return $if;
}

return $if->stmts;
}

/**
* @return null
*/
private function processGreaterRight(Greater $greater, If_ $if)
private function processGreaterRight(Greater $greater, If_ $if): ?If_
{
$value = $greater->left;
if (! $value instanceof LNumber) {
Expand All @@ -275,15 +283,16 @@ private function processGreaterRight(Greater $greater, If_ $if)

if ($this->phpVersion >= $value->value) {
$this->removeNode($if);
return $if;
}

return null;
}

/**
* @return Stmt[]|null
* @return null|If_|Stmt[]
*/
private function refactorConstFetch(ConstFetch $constFetch, If_ $if, BinaryOp $binaryOp): ?array
private function refactorConstFetch(ConstFetch $constFetch, If_ $if, BinaryOp $binaryOp): null|If_|array
{
if ($binaryOp instanceof Smaller) {
return $this->processSmaller($constFetch, $binaryOp, $if);
Expand Down

0 comments on commit fb8260c

Please sign in to comment.