rector/packages/Php/src/Rector/FuncCall/PowToExpRector.php

49 lines
1.1 KiB
PHP
Raw Normal View History

2018-10-04 08:11:54 +00:00
<?php declare(strict_types=1);
2018-10-05 09:05:03 +00:00
namespace Rector\Php\Rector\FuncCall;
2018-10-04 08:11:54 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Pow;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class PowToExpRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Changes pow(val, val2) to ** (exp) parameter',
[new CodeSample('pow(1, 2);', '1**2;')]
);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
2018-10-04 08:11:54 +00:00
*/
public function refactor(Node $node): ?Node
2018-10-04 08:11:54 +00:00
{
if (! $this->isName($node, 'pow')) {
return null;
2018-10-04 08:11:54 +00:00
}
if (count($node->args) !== 2) {
return null;
2018-10-04 08:11:54 +00:00
}
$firstArgument = $node->args[0]->value;
$secondArgument = $node->args[1]->value;
2018-10-04 08:11:54 +00:00
return new Pow($firstArgument, $secondArgument);
}
}