forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyExistsWithoutAssertRector.php
More file actions
134 lines (118 loc) · 4.13 KB
/
Copy pathPropertyExistsWithoutAssertRector.php
File metadata and controls
134 lines (118 loc) · 4.13 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\PHPUnit100\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\PropertyExistsWithoutAssertRectorTest
*/
final class PropertyExistsWithoutAssertRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const RENAME_METHODS_WITH_OBJECT_MAP = [
'assertObjectHasAttribute' => 'assertTrue',
'assertObjectNotHasAttribute' => 'assertFalse',
];
/**
* @var array<string, string>
*/
private const RENAME_METHODS_WITH_CLASS_MAP = [
'assertClassHasAttribute' => 'assertTrue',
'assertClassNotHasAttribute' => 'assertFalse',
];
public function __construct(
private readonly IdentifierManipulator $identifierManipulator,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turns PHPUnit TestCase assertObjectHasAttribute into `property_exists` comparisons',
[
new CodeSample(
<<<'CODE_SAMPLE'
$this->assertClassHasAttribute("property", "Class");
$this->assertClassNotHasAttribute("property", "Class");
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$this->assertFalse(property_exists(new Class, "property"));
$this->assertTrue(property_exists(new Class, "property"));
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class];
}
/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, [
'assertClassHasAttribute',
'assertClassNotHasAttribute',
'assertObjectNotHasAttribute',
'assertObjectHasAttribute',
])) {
return null;
}
$arguments = array_column($node->args, 'value');
if (
$arguments[0] instanceof String_ ||
$arguments[0] instanceof Variable ||
$arguments[0] instanceof ArrayDimFetch ||
$arguments[0] instanceof PropertyFetch
) {
$secondArg = $arguments[0];
} else {
return null;
}
if ($arguments[1] instanceof Variable) {
$firstArg = new Variable($arguments[1]->name);
$map = self::RENAME_METHODS_WITH_OBJECT_MAP;
} elseif ($arguments[1] instanceof String_) {
$firstArg = new New_(new FullyQualified($arguments[1]->value));
$map = self::RENAME_METHODS_WITH_CLASS_MAP;
} elseif ($arguments[1] instanceof PropertyFetch || $arguments[1] instanceof ArrayDimFetch) {
$firstArg = $arguments[1];
$map = self::RENAME_METHODS_WITH_OBJECT_MAP;
} else {
return null;
}
unset($node->args[0]);
unset($node->args[1]);
$propertyExistsFuncCall = new FuncCall(new Name('property_exists'), [
new Arg($firstArg),
new Arg($secondArg),
]);
$newArgs = $this->nodeFactory->createArgs([$propertyExistsFuncCall]);
$node->args = $this->appendArgs($newArgs, $node->getArgs());
$this->identifierManipulator->renameNodeWithMap($node, $map);
return $node;
}
}