-
-
Notifications
You must be signed in to change notification settings - Fork 703
/
Copy pathDateTimeMethodCallToCarbonRector.php
93 lines (91 loc) · 2.65 KB
/
DateTimeMethodCallToCarbonRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare (strict_types=1);
namespace Rector\Carbon\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Carbon\NodeFactory\CarbonCallFactory;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector\DateTimeMethodCallToCarbonRectorTest
*/
final class DateTimeMethodCallToCarbonRector extends AbstractRector
{
/**
* @readonly
*/
private CarbonCallFactory $carbonCallFactory;
public function __construct(CarbonCallFactory $carbonCallFactory)
{
$this->carbonCallFactory = $carbonCallFactory;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Convert new DateTime() with a method call to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$date = (new \DateTime('today +20 day'))->format('Y-m-d');
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$date = \Carbon\Carbon::today()->addDays(20)->format('Y-m-d')
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node) : ?Node
{
if (!$node->var instanceof New_) {
return null;
}
$new = $node->var;
if (!$new->class instanceof Name) {
return null;
}
if (!$this->isName($new->class, 'DateTime') && !$this->isName($new->class, 'DateTimeImmutable')) {
return null;
}
if ($new->isFirstClassCallable()) {
return null;
}
if (\count($new->getArgs()) !== 1) {
// @todo handle in separate static call
return null;
}
$firstArg = $new->getArgs()[0];
if (!$firstArg->value instanceof String_) {
return null;
}
if ($this->isName($new->class, 'DateTime')) {
$carbonFullyQualified = new FullyQualified('Carbon\\Carbon');
} else {
$carbonFullyQualified = new FullyQualified('Carbon\\CarbonImmutable');
}
$carbonCall = $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
$node->var = $carbonCall;
return $node;
}
}