2021-05-17 23:17:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Arguments\Rector\FuncCall;
|
2021-05-17 23:17:47 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use Rector\Arguments\ArgumentDefaultValueReplacer;
|
|
|
|
use Rector\Arguments\ValueObject\ReplaceFuncCallArgumentDefaultValue;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-01-01 00:20:45 +00:00
|
|
|
use RectorPrefix202401\Webmozart\Assert\Assert;
|
2021-05-17 23:17:47 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://php.watch/versions/8.1/version_compare-operator-restrictions
|
|
|
|
* @changelog https://github.com/rectorphp/rector/issues/6271
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\Arguments\Rector\FuncCall\FunctionArgumentDefaultValueReplacerRector\FunctionArgumentDefaultValueReplacerRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class FunctionArgumentDefaultValueReplacerRector extends AbstractRector implements ConfigurableRectorInterface
|
2021-05-17 23:17:47 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-17 23:17:47 +00:00
|
|
|
* @var \Rector\Arguments\ArgumentDefaultValueReplacer
|
|
|
|
*/
|
|
|
|
private $argumentDefaultValueReplacer;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var ReplaceFuncCallArgumentDefaultValue[]
|
|
|
|
*/
|
|
|
|
private $replacedArguments = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ArgumentDefaultValueReplacer $argumentDefaultValueReplacer)
|
2021-05-17 23:17:47 +00:00
|
|
|
{
|
|
|
|
$this->argumentDefaultValueReplacer = $argumentDefaultValueReplacer;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-05-17 23:17:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Streamline the operator arguments of version_compare function', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-05-17 23:17:47 +00:00
|
|
|
version_compare(PHP_VERSION, '5.6', 'gte');
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
version_compare(PHP_VERSION, '5.6', 'ge');
|
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
, [new ReplaceFuncCallArgumentDefaultValue('version_compare', 2, 'gte', 'ge')])]);
|
2021-05-17 23:17:47 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2021-05-17 23:17:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2021-05-17 23:17:47 +00:00
|
|
|
*/
|
2022-08-11 14:29:35 +00:00
|
|
|
public function refactor(Node $node) : ?\PhpParser\Node\Expr\FuncCall
|
2021-05-17 23:17:47 +00:00
|
|
|
{
|
2021-12-28 14:03:15 +00:00
|
|
|
$hasChanged = \false;
|
2021-05-17 23:17:47 +00:00
|
|
|
foreach ($this->replacedArguments as $replacedArgument) {
|
|
|
|
if (!$this->isName($node->name, $replacedArgument->getFunction())) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-28 14:03:15 +00:00
|
|
|
$changedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($changedNode instanceof Node) {
|
2021-12-28 14:03:15 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2021-05-17 23:17:47 +00:00
|
|
|
}
|
2021-12-28 14:03:15 +00:00
|
|
|
return null;
|
2021-05-17 23:17:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-05-17 23:17:47 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2021-05-17 23:17:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, ReplaceFuncCallArgumentDefaultValue::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->replacedArguments = $configuration;
|
2021-05-17 23:17:47 +00:00
|
|
|
}
|
|
|
|
}
|