Skip to content

Commit 3984732

Browse files
authored
[PHPUnit10] Replace deleted PHPUnit methods: assertClassHasStaticAttribute, classHasStaticAttribute and assertClassNotHasStaticAttribute by property_exists() (#438)
sebastianbergmann/phpunit#4601
1 parent c0eaa38 commit 3984732

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MyPropertyExistsWithoutAssertRectorTest extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertClassHasStaticAttribute('property', 'stdClass');
12+
$this->assertClassHasStaticAttribute('property', stdClass::class);
13+
$this->classHasStaticAttribute('property', stdClass::class, 'message');
14+
$this->assertClassNotHasStaticAttribute('property', 'Namespaced\stdClass', 'message');
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class MyPropertyExistsWithoutAssertRectorTest extends TestCase
27+
{
28+
public function test()
29+
{
30+
$this->assertTrue(property_exists('stdClass', 'property'));
31+
$this->assertTrue(property_exists(stdClass::class, 'property'));
32+
$this->assertTrue(property_exists(stdClass::class, 'property'), 'message');
33+
$this->assertFalse(property_exists('Namespaced\stdClass', 'property'), 'message');
34+
}
35+
}
36+
37+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class PropertyExistsWithoutAssertRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(PropertyExistsWithoutAssertRector::class);
10+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\PHPUnit100\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Expr\FuncCall;
11+
use PhpParser\Node\Expr\MethodCall;
12+
use PhpParser\Node\Name;
13+
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
14+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
15+
use Rector\Rector\AbstractRector;
16+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
17+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
19+
/**
20+
* @url https://github.com/sebastianbergmann/phpunit/issues/4601
21+
*
22+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\PropertyExistsWithoutAssertRector\PropertyExistsWithoutAssertRectorTest
23+
*/
24+
final class PropertyExistsWithoutAssertRector extends AbstractRector
25+
{
26+
/**
27+
* @var array<string, string>
28+
*/
29+
private const RENAME_METHODS_WITH_OBJECT_MAP = [
30+
'assertClassHasStaticAttribute' => 'assertTrue',
31+
'classHasStaticAttribute' => 'assertTrue',
32+
'assertClassNotHasStaticAttribute' => 'assertFalse',
33+
];
34+
35+
public function __construct(
36+
private readonly IdentifierManipulator $identifierManipulator,
37+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
38+
) {
39+
}
40+
41+
public function getRuleDefinition(): RuleDefinition
42+
{
43+
return new RuleDefinition(
44+
'Replace delited PHPUnit methods: assertClassHasStaticAttribute, classHasStaticAttribute and assertClassNotHasStaticAttribute by property_exists()',
45+
[
46+
new CodeSample(
47+
<<<'CODE_SAMPLE'
48+
$this->assertClassHasStaticAttribute("Class", "property");
49+
$this->classHasStaticAttribute("Class", "property");
50+
$this->assertClassNotHasStaticAttribute("Class", "property");
51+
CODE_SAMPLE
52+
,
53+
<<<'CODE_SAMPLE'
54+
$this->assertTrue(property_exists("Class", "property"));
55+
$this->assertTrue(property_exists("Class", "property"));
56+
$this->assertFalse(property_exists("Class", "property"));
57+
CODE_SAMPLE
58+
),
59+
]
60+
);
61+
}
62+
63+
/**
64+
* @return array<class-string<Node>>
65+
*/
66+
public function getNodeTypes(): array
67+
{
68+
return [MethodCall::class];
69+
}
70+
71+
/**
72+
* @param MethodCall $node
73+
*/
74+
public function refactor(Node $node): ?Node
75+
{
76+
$map = self::RENAME_METHODS_WITH_OBJECT_MAP;
77+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, array_keys($map))) {
78+
return null;
79+
}
80+
81+
if ($node->isFirstClassCallable() || ! isset($node->getArgs()[0], $node->getArgs()[1])) {
82+
return null;
83+
}
84+
85+
$firstNode = new Arg($node->getArgs()[0]->value);
86+
87+
if ($node->getArgs()[1]->value instanceof ClassConstFetch) {
88+
$secondNode = $node->getArgs()[1];
89+
} else {
90+
$secondNode = new Arg($node->getArgs()[1]->value);
91+
}
92+
93+
$funcCall = new FuncCall(new Name('property_exists'), [$secondNode, $firstNode]);
94+
95+
$newArgs = $this->nodeFactory->createArgs([$funcCall]);
96+
97+
unset($node->args[0], $node->args[1]);
98+
$node->args = array_merge($newArgs, $node->getArgs());
99+
100+
$this->identifierManipulator->renameNodeWithMap($node, $map);
101+
102+
return $node;
103+
}
104+
}

0 commit comments

Comments
 (0)