mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-16 21:08:19 +01:00
9a4a5b2bc5
eb5df4dde7
Use vendor-patches main branch (#6453)
72 lines
3.0 KiB
PHP
72 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Equal;
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
use Rector\DeadCode\Contract\ConditionInterface;
|
|
use Rector\DeadCode\ValueObject\BinaryToVersionCompareCondition;
|
|
use Rector\DeadCode\ValueObject\VersionCompareCondition;
|
|
use Rector\Exception\ShouldNotHappenException;
|
|
use Rector\Php\PhpVersionProvider;
|
|
final class ConditionEvaluator
|
|
{
|
|
/**
|
|
* @readonly
|
|
*/
|
|
private PhpVersionProvider $phpVersionProvider;
|
|
public function __construct(PhpVersionProvider $phpVersionProvider)
|
|
{
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
}
|
|
/**
|
|
* @return bool|int|null
|
|
*/
|
|
public function evaluate(ConditionInterface $condition)
|
|
{
|
|
if ($condition instanceof VersionCompareCondition) {
|
|
return $this->evaluateVersionCompareCondition($condition);
|
|
}
|
|
if ($condition instanceof BinaryToVersionCompareCondition) {
|
|
return $this->isEvaluedAsTrue($condition);
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* @return bool|int|null
|
|
*/
|
|
private function evaluateVersionCompareCondition(VersionCompareCondition $versionCompareCondition)
|
|
{
|
|
$compareSign = $versionCompareCondition->getCompareSign();
|
|
if ($compareSign !== null) {
|
|
if ($compareSign === '<' && $this->phpVersionProvider->provide() < $versionCompareCondition->getSecondVersion()) {
|
|
return null;
|
|
}
|
|
return \version_compare((string) $versionCompareCondition->getFirstVersion(), (string) $versionCompareCondition->getSecondVersion(), $compareSign);
|
|
}
|
|
return \version_compare((string) $versionCompareCondition->getFirstVersion(), (string) $versionCompareCondition->getSecondVersion());
|
|
}
|
|
private function isEvaluedAsTrue(BinaryToVersionCompareCondition $binaryToVersionCompareCondition) : bool
|
|
{
|
|
$versionCompareResult = $this->evaluateVersionCompareCondition($binaryToVersionCompareCondition->getVersionCompareCondition());
|
|
if ($binaryToVersionCompareCondition->getBinaryClass() === Identical::class) {
|
|
return $binaryToVersionCompareCondition->getExpectedValue() === $versionCompareResult;
|
|
}
|
|
if ($binaryToVersionCompareCondition->getBinaryClass() === NotIdentical::class) {
|
|
return $binaryToVersionCompareCondition->getExpectedValue() !== $versionCompareResult;
|
|
}
|
|
if ($binaryToVersionCompareCondition->getBinaryClass() === Equal::class) {
|
|
// weak comparison on purpose
|
|
return $binaryToVersionCompareCondition->getExpectedValue() === $versionCompareResult;
|
|
}
|
|
if ($binaryToVersionCompareCondition->getBinaryClass() === NotEqual::class) {
|
|
// weak comparison on purpose
|
|
return $binaryToVersionCompareCondition->getExpectedValue() !== $versionCompareResult;
|
|
}
|
|
throw new ShouldNotHappenException();
|
|
}
|
|
}
|