mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 01:41:00 +01:00
[PHP] Fix mktime rename with args [closes #1622]
This commit is contained in:
parent
f515ecca3d
commit
b32c32b682
@ -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: ~
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user