Skip to content

Commit 1679df4

Browse files
committed
[code-quality] Add MatchAssertSameExpectedTypeRector
1 parent 9f87185 commit 1679df4

File tree

7 files changed

+283
-0
lines changed

7 files changed

+283
-0
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertSameTrueFalseToAssertTrueFalseRector;
3232
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector;
3333
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\FlipAssertRector;
34+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector;
3435
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector;
3536
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector;
3637
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
@@ -43,7 +44,10 @@
4344
return static function (RectorConfig $rectorConfig): void {
4445
$rectorConfig->rules([
4546
ConstructClassMethodToSetUpTestCaseRector::class,
47+
4648
AssertSameTrueFalseToAssertTrueFalseRector::class,
49+
MatchAssertSameExpectedTypeRector::class,
50+
4751
AssertEqualsToSameRector::class,
4852
PreferPHPUnitThisCallRector::class,
4953
YieldDataProviderRector::class,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MatchAssertType extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertSame('123', $this->getOrderId());
12+
}
13+
14+
private function getOrderId(): int
15+
{
16+
return 123;
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class MatchAssertType extends TestCase
29+
{
30+
public function test()
31+
{
32+
$this->assertSame(123, $this->getOrderId());
33+
}
34+
35+
private function getOrderId(): int
36+
{
37+
return 123;
38+
}
39+
}
40+
41+
?>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MatchIntegerToString extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertSame(123, $this->getOrderId());
12+
}
13+
14+
private function getOrderId(): string
15+
{
16+
return '123';
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class MatchIntegerToString extends TestCase
29+
{
30+
public function test()
31+
{
32+
$this->assertSame('123', $this->getOrderId());
33+
}
34+
35+
private function getOrderId(): string
36+
{
37+
return '123';
38+
}
39+
}
40+
41+
?>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class NullableMatchAssertType extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertSame('123', $this->getOrderId());
12+
}
13+
14+
private function getOrderId(): ?int
15+
{
16+
return 123;
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class NullableMatchAssertType extends TestCase
29+
{
30+
public function test()
31+
{
32+
$this->assertSame(123, $this->getOrderId());
33+
}
34+
35+
private function getOrderId(): ?int
36+
{
37+
return 123;
38+
}
39+
}
40+
41+
?>
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\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class MatchAssertSameExpectedTypeRectorTest 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\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(MatchAssertSameExpectedTypeRector::class);
10+
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\StaticCall;
10+
use PhpParser\Node\Scalar\LNumber;
11+
use PhpParser\Node\Scalar\String_;
12+
use PHPStan\Type\TypeCombinator;
13+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\MatchAssertSameExpectedTypeRectorTest
20+
*/
21+
final class MatchAssertSameExpectedTypeRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
25+
) {
26+
}
27+
28+
public function getRuleDefinition(): RuleDefinition
29+
{
30+
return new RuleDefinition(
31+
'Correct expected type in assertSame() method to match strict type of passed variable',
32+
[
33+
new CodeSample(
34+
<<<'CODE_SAMPLE'
35+
use PHPUnit\Framework\TestCase;
36+
37+
class SomeTest extends TestCase
38+
{
39+
public function run()
40+
{
41+
$this->assertSame('123', $this->getOrderId());
42+
}
43+
44+
private function getOrderId(): int
45+
{
46+
return 123;
47+
}
48+
}
49+
CODE_SAMPLE
50+
,
51+
<<<'CODE_SAMPLE'
52+
use PHPUnit\Framework\TestCase;
53+
54+
class SomeTest extends TestCase
55+
{
56+
public function run()
57+
{
58+
$this->assertSame(123, $this->getOrderId());
59+
}
60+
61+
private function getOrderId(): int
62+
{
63+
return 123;
64+
}
65+
}
66+
CODE_SAMPLE
67+
),
68+
]
69+
);
70+
}
71+
72+
/**
73+
* @return array<class-string<Node>>
74+
*/
75+
public function getNodeTypes(): array
76+
{
77+
return [MethodCall::class, StaticCall::class];
78+
}
79+
80+
/**
81+
* @param MethodCall|StaticCall $node
82+
*/
83+
public function refactor(Node $node): ?Node
84+
{
85+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertSame', 'assertEquals'])) {
86+
return null;
87+
}
88+
89+
$expectedArg = $node->getArgs()[0];
90+
if (! $expectedArg->value instanceof String_ && ! $expectedArg->value instanceof LNumber) {
91+
return null;
92+
}
93+
94+
$expectedType = $this->getType($expectedArg->value);
95+
96+
$variableExpr = $node->getArgs()[1]
97+
->value;
98+
$variableType = $this->getType($variableExpr);
99+
100+
$directVariableType = TypeCombinator::removeNull($variableType);
101+
102+
if ($expectedType->isLiteralString()->yes() && $directVariableType->isInteger()->yes()) {
103+
// update expected type to provided type
104+
$expectedArg->value = new LNumber((int) $expectedArg->value->value);
105+
106+
return $node;
107+
}
108+
109+
if ($expectedType->isInteger()->yes() && $directVariableType->isString()->yes()) {
110+
// update expected type to provided type
111+
$expectedArg->value = new String_((string) $expectedArg->value->value);
112+
113+
return $node;
114+
}
115+
116+
return null;
117+
}
118+
}

0 commit comments

Comments
 (0)