Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;

final class SkipCheckVersionCompareTernary
{
public function run_v()
{
return version_compare(PHP_VERSION, '8.1','>=')
? hash( 'xxh128', $value )
: hash( 'md4', $value );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;

final class SkipCheckVersionCompareTernary2
{
public function run_v()
{
return version_compare(PHP_VERSION, '8.1','<')
? hash( 'md4', $value )
: hash( 'xxh128', $value );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeVisitor;
use PHPStan\Type\IntegerRangeType;
use Rector\DeadCode\ConditionResolver;
use Rector\DeadCode\ValueObject\VersionCompareCondition;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
Expand All @@ -36,6 +41,7 @@ final class DowngradeHashAlgorithmXxHashRector extends AbstractRector
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly ValueResolver $valueResolver,
private readonly ConditionResolver $conditionResolver
) {
}

Expand Down Expand Up @@ -74,14 +80,22 @@ public function run()
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
return [Ternary::class, FuncCall::class];
}

/**
* @param FuncCall $node
* @param Ternary|FuncCall $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): null|int|Node
{
if ($node instanceof Ternary) {
if ($this->isVersionCompareTernary($node)) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}

return null;
}

if ($this->shouldSkip($node)) {
return null;
}
Expand All @@ -104,6 +118,24 @@ public function refactor(Node $node): ?Node
return $node;
}

private function isVersionCompareTernary(Ternary $ternary): bool
{
if ($ternary->if instanceof Expr && $ternary->cond instanceof FuncCall) {
$versionCompare = $this->conditionResolver->resolveFromExpr($ternary->cond);
if ($versionCompare instanceof VersionCompareCondition && $versionCompare->getSecondVersion() === 80100) {
if ($versionCompare->getCompareSign() === '>=') {
return $ternary->if instanceof FuncCall && $this->isName($ternary->if, 'hash');
}

if ($versionCompare->getCompareSign() === '<') {
return $ternary->else instanceof FuncCall && $this->isName($ternary->else, 'hash');
}
}
}

return false;
}

private function shouldSkip(FuncCall $funcCall): bool
{
if ($funcCall->isFirstClassCallable()) {
Expand Down