-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradeNullsafeToTernaryOperatorRector.php
More file actions
118 lines (99 loc) · 4.4 KB
/
DowngradeNullsafeToTernaryOperatorRector.php
File metadata and controls
118 lines (99 loc) · 4.4 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Rector\NullsafeMethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\DowngradeNullsafeToTernaryOperatorRectorTest
*/
final class DowngradeNullsafeToTernaryOperatorRector extends AbstractRector
{
private int $counter = 0;
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change nullsafe operator to ternary operator rector', [
new CodeSample(
<<<'CODE_SAMPLE'
$dateAsString = $booking->getStartDate()?->asDateTimeString();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$dateAsString = ($bookingGetStartDate = $booking->getStartDate()) ? $bookingGetStartDate->asDateTimeString() : null;
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, PropertyFetch::class, NullsafeMethodCall::class, NullsafePropertyFetch::class];
}
/**
* @param MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node
*/
public function refactor(Node $node): ?Ternary
{
static $currentFile = null;
if ($currentFile !== $this->file->getFilePath()) {
$this->counter = 0;
$currentFile = $this->file->getFilePath();
}
if ($node instanceof MethodCall || $node instanceof PropertyFetch) {
if ($node->var instanceof NullsafeMethodCall || $node->var instanceof NullsafePropertyFetch) {
$nullsafeVariable = $this->createNullsafeVariable();
$assign = new Assign($nullsafeVariable, $node->var->var);
if ($node instanceof MethodCall) {
if ($node->var instanceof NullsafeMethodCall) {
$methodCallOrPropertyFetch = new MethodCall(new MethodCall(
$nullsafeVariable,
$node->var->name,
$node->var->args
), $node->name, $node->args);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}
$methodCallOrPropertyFetch = new MethodCall(new PropertyFetch(
$nullsafeVariable,
$node->var->name
), $node->name, $node->args);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}
if ($node->var instanceof NullsafeMethodCall) {
$methodCallOrPropertyFetch = new PropertyFetch(new MethodCall(
$nullsafeVariable,
$node->var->name,
$node->var->args
), $node->name);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}
$methodCallOrPropertyFetch = new PropertyFetch(new PropertyFetch(
$nullsafeVariable,
$node->var->name
), $node->name);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}
return null;
}
$nullsafeVariable = $this->createNullsafeVariable();
$methodCallOrPropertyFetch = $node instanceof NullsafeMethodCall
? new MethodCall($nullsafeVariable, $node->name, $node->args)
: new PropertyFetch($nullsafeVariable, $node->name);
$assign = new Assign($nullsafeVariable, $node->var);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}
private function createNullsafeVariable(): Variable
{
$nullsafeVariableName = 'nullsafeVariable' . ++$this->counter;
return new Variable($nullsafeVariableName);
}
}