Skip to content

Commit

Permalink
[CodingStyle] Skip indirect version number on VersionCompareFuncCallT…
Browse files Browse the repository at this point in the history
…oConstantRector (#344)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 1, 2021
1 parent 735327d commit 9575dd2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector\Fixture;

class SkipIndirectStringNumber
{
private const MIN_PHP_VERSION = '7.3';

public function run()
{
version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<');
version_compare(self::MIN_PHP_VERSION, PHP_VERSION, '<');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\PhpVersionFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -113,6 +112,12 @@ public function refactor(Node $node): ?Node

$left = $this->getNewNodeForArg($node->args[0]->value);
$right = $this->getNewNodeForArg($node->args[1]->value);
if ($left === null) {
return null;
}
if ($right === null) {
return null;
}

/** @var String_ $operator */
$operator = $node->args[2]->value;
Expand All @@ -129,7 +134,7 @@ private function isPhpVersionConstant(Expr $expr): bool
return $expr->name->toString() === 'PHP_VERSION';
}

private function getNewNodeForArg(Expr $expr): ConstFetch | LNumber
private function getNewNodeForArg(Expr $expr): ConstFetch | LNumber | null
{
if ($this->isPhpVersionConstant($expr)) {
return new ConstFetch(new Name('PHP_VERSION_ID'));
Expand All @@ -138,10 +143,10 @@ private function getNewNodeForArg(Expr $expr): ConstFetch | LNumber
return $this->getVersionNumberFormVersionString($expr);
}

private function getVersionNumberFormVersionString(Expr $expr): LNumber
private function getVersionNumberFormVersionString(Expr $expr): ?LNumber
{
if (! $expr instanceof String_) {
throw new ShouldNotHappenException();
return null;
}

$value = $this->phpVersionFactory->createIntVersion($expr->value);
Expand Down

0 comments on commit 9575dd2

Please sign in to comment.