[PHP] Fix mktime rename with args [closes #1622]

This commit is contained in:
Tomas Votruba 2019-06-23 20:03:33 +02:00
parent f515ecca3d
commit b32c32b682
4 changed files with 119 additions and 3 deletions

View File

@ -21,7 +21,5 @@ services:
Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector: ~
Rector\Php\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector: ~
Rector\Rector\Function_\RenameFunctionRector:
# https://3v4l.org/1s1St
mktime: 'time'
Rector\Php\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector: ~
Rector\Php\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector: ~

View File

@ -0,0 +1,70 @@
<?php declare(strict_types=1);
namespace Rector\Php\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://3v4l.org/F5GE8
*/
final class RenameMktimeWithoutArgsToTimeRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$time = mktime(1, 2, 3);
$nextTime = mktime();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$time = mktime(1, 2, 3);
$nextTime = time();
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'mktime')) {
return null;
}
if (count($node->args) > 0) {
return null;
}
$node->name = new Node\Name('time');
return $node;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace Rector\Php\Tests\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector\Fixture;
class SomeClass
{
public function run()
{
$time = mktime(1, 2, 3);
$nextTime = mktime();
}
}
?>
-----
<?php
namespace Rector\Php\Tests\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector\Fixture;
class SomeClass
{
public function run()
{
$time = mktime(1, 2, 3);
$nextTime = time();
}
}
?>

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector;
use Rector\Php\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class RenameMktimeWithoutArgsToTimeRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}
protected function getRectorClass(): string
{
return RenameMktimeWithoutArgsToTimeRector::class;
}
}