Skip to content

Commit

Permalink
[DeadCode] Skip UnwrapFutureCompatibleIfPhpVersionRector on higher th…
Browse files Browse the repository at this point in the history
…an current php provider (#685)
  • Loading branch information
samsonasik committed Aug 15, 2021
1 parent 9a7ce1b commit 5f28681
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector\Fixture;

class SkipHigherCurrent
{
public function run()
{
// $this->phpVersionProvider->provide() returns 10000 on test
if (version_compare(PHP_VERSION, '11.0', '<')) {
return 'a';
} else {
return 'b';
}
}
}

?>
11 changes: 10 additions & 1 deletion rules/DeadCode/ConditionEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\DeadCode\Contract\ConditionInterface;
use Rector\DeadCode\ValueObject\BinaryToVersionCompareCondition;
use Rector\DeadCode\ValueObject\VersionCompareCondition;

final class ConditionEvaluator
{
public function __construct(private PhpVersionProvider $phpVersionProvider)
{
}

/**
* @return bool|int|null
*/
Expand All @@ -31,10 +36,14 @@ public function evaluate(ConditionInterface $condition)
return null;
}

private function evaluateVersionCompareCondition(VersionCompareCondition $versionCompareCondition): bool | int
private function evaluateVersionCompareCondition(VersionCompareCondition $versionCompareCondition): bool | int | null
{
$compareSign = $versionCompareCondition->getCompareSign();
if ($compareSign !== null) {
if ($compareSign === '<' && $this->phpVersionProvider->provide() < $versionCompareCondition->getSecondVersion()) {
return null;
}

return version_compare(
(string) $versionCompareCondition->getFirstVersion(),
(string) $versionCompareCondition->getSecondVersion(),
Expand Down

0 comments on commit 5f28681

Please sign in to comment.