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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 04:36:58 +00:00
|
|
|
* @param FuncCall $node
|
2018-10-04 08:11:54 +00:00
|
|
|
*/
|
2018-10-15 04:36:58 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2018-10-04 08:11:54 +00:00
|
|
|
{
|
2018-10-15 04:36:58 +00:00
|
|
|
if (! $this->isName($node, 'pow')) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-04 08:11:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 04:36:58 +00:00
|
|
|
if (count($node->args) !== 2) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-04 08:11:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 04:36:58 +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);
|
|
|
|
}
|
|
|
|
}
|