-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CleaningParser - remove conditional code by PHP_VERSION_ID
- Loading branch information
1 parent
97a181e
commit 433511a
Showing
9 changed files
with
196 additions
and
6 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
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,106 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Parser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PHPStan\Php\PhpVersion; | ||
use function count; | ||
use function version_compare; | ||
|
||
class RemoveUnusedCodeByPhpVersionIdVisitor extends NodeVisitorAbstract | ||
{ | ||
|
||
public function __construct(private string $phpVersionString) | ||
{ | ||
} | ||
|
||
public function enterNode(Node $node): Node|int|null | ||
{ | ||
if ($node instanceof Node\Stmt\ClassLike) { | ||
return null; | ||
} | ||
if ($node instanceof Node\FunctionLike) { | ||
return null; | ||
} | ||
|
||
if (!$node instanceof Node\Stmt\If_) { | ||
return null; | ||
} | ||
|
||
if (count($node->elseifs) > 0) { | ||
return null; | ||
} | ||
|
||
if ($node->else === null) { | ||
return null; | ||
} | ||
|
||
$cond = $node->cond; | ||
if ( | ||
!$cond instanceof Node\Expr\BinaryOp\Smaller | ||
&& !$cond instanceof Node\Expr\BinaryOp\SmallerOrEqual | ||
&& !$cond instanceof Node\Expr\BinaryOp\Greater | ||
&& !$cond instanceof Node\Expr\BinaryOp\GreaterOrEqual | ||
&& !$cond instanceof Node\Expr\BinaryOp\Equal | ||
&& !$cond instanceof Node\Expr\BinaryOp\NotEqual | ||
&& !$cond instanceof Node\Expr\BinaryOp\Identical | ||
&& !$cond instanceof Node\Expr\BinaryOp\NotIdentical | ||
) { | ||
return null; | ||
} | ||
|
||
$operator = $cond->getOperatorSigil(); | ||
if ($operator === '===') { | ||
$operator = '=='; | ||
} | ||
if ($operator === '!==') { | ||
$operator = '!='; | ||
} | ||
|
||
$operands = $this->getOperands($cond->left, $cond->right); | ||
if ($operands === null) { | ||
return null; | ||
} | ||
|
||
$result = version_compare($operands[0], $operands[1], $operator); | ||
if ($result) { | ||
// remove else | ||
$node->cond = new Node\Expr\ConstFetch(new Node\Name('true')); | ||
$node->else = null; | ||
|
||
return $node; | ||
} | ||
|
||
// remove if | ||
$node->cond = new Node\Expr\ConstFetch(new Node\Name('false')); | ||
$node->stmts = []; | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @return array{string, string}|null | ||
*/ | ||
private function getOperands(Node\Expr $left, Node\Expr $right): ?array | ||
{ | ||
if ( | ||
$left instanceof Node\Scalar\LNumber | ||
&& $right instanceof Node\Expr\ConstFetch | ||
&& $right->name->toString() === 'PHP_VERSION_ID' | ||
) { | ||
return [(new PhpVersion($left->value))->getVersionString(), $this->phpVersionString]; | ||
} | ||
|
||
if ( | ||
$right instanceof Node\Scalar\LNumber | ||
&& $left instanceof Node\Expr\ConstFetch | ||
&& $left->name->toString() === 'PHP_VERSION_ID' | ||
) { | ||
return [$this->phpVersionString, (new PhpVersion($right->value))->getVersionString()]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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
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
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
10 changes: 10 additions & 0 deletions
10
tests/PHPStan/Parser/data/cleaning-php-version-after-74.php
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,10 @@ | ||
<?php | ||
declare (strict_types=1); | ||
namespace TestCleanPhpVersion; | ||
|
||
use const PHP_VERSION_ID; | ||
if (false) { | ||
} else { | ||
doBar1(); | ||
doBar2(); | ||
} |
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,9 @@ | ||
<?php | ||
declare (strict_types=1); | ||
namespace TestCleanPhpVersion; | ||
|
||
use const PHP_VERSION_ID; | ||
if (true) { | ||
doFoo1(); | ||
doFoo2(); | ||
} |
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,13 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TestCleanPhpVersion; | ||
|
||
use const PHP_VERSION_ID; | ||
|
||
if (PHP_VERSION_ID >= 80100) { | ||
doFoo1(); | ||
doFoo2(); | ||
} else { | ||
doBar1(); | ||
doBar2(); | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/PHPStan/Parser/data/cleaning-php-version-before2.php
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,13 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TestCleanPhpVersion; | ||
|
||
use const PHP_VERSION_ID; | ||
|
||
if (80100 <= PHP_VERSION_ID) { | ||
doFoo1(); | ||
doFoo2(); | ||
} else { | ||
doBar1(); | ||
doBar2(); | ||
} |