-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathPropertyExistsWithoutAssertRector.php
More file actions
110 lines (94 loc) · 3.36 KB
/
PropertyExistsWithoutAssertRector.php
File metadata and controls
110 lines (94 loc) · 3.36 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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\PHPUnit100\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @url https://github.com/sebastianbergmann/phpunit/issues/4601
*
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\PropertyExistsWithoutAssertRectorTest
*/
final class PropertyExistsWithoutAssertRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const array RENAME_METHODS_WITH_OBJECT_MAP = [
'assertClassHasAttribute' => 'assertTrue',
'assertObjectHasAttribute' => 'assertTrue',
'assertClassHasStaticAttribute' => 'assertTrue',
// false
'assertClassNotHasStaticAttribute' => 'assertFalse',
'assertClassNotHasAttribute' => 'assertFalse',
'assertObjectNotHasAttribute' => 'assertFalse',
// no assert
'objectHasAttribute' => 'assertTrue',
'classHasAttribute' => 'assertTrue',
'classHasStaticAttribute' => 'assertTrue',
];
public function __construct(
private readonly IdentifierManipulator $identifierManipulator,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace removed assertClassHas*Attribute() with property_exists()',
[
new CodeSample(
<<<'CODE_SAMPLE'
$this->assertClassHasAttribute("Class", "property");
$this->assertClassHasStaticAttribute("Class", "property");
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$this->assertTrue(property_exists("Class", "property"));
$this->assertTrue(property_exists("Class", "property"));
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$map = self::RENAME_METHODS_WITH_OBJECT_MAP;
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, array_keys($map))) {
return null;
}
if ($node->isFirstClassCallable() || ! isset($node->getArgs()[0], $node->getArgs()[1])) {
return null;
}
$firstNode = new Arg($node->getArgs()[0]->value);
if ($node->getArgs()[1]->value instanceof ClassConstFetch) {
$secondNode = $node->getArgs()[1];
} else {
$secondNode = new Arg($node->getArgs()[1]->value);
}
$funcCall = new FuncCall(new Name('property_exists'), [$secondNode, $firstNode]);
$newArgs = $this->nodeFactory->createArgs([$funcCall]);
unset($node->args[0], $node->args[1]);
$node->args = array_merge($newArgs, $node->getArgs());
$this->identifierManipulator->renameNodeWithMap($node, $map);
return $node;
}
}