Skip to content

Commit e561212

Browse files
Revert rule removal of MatchAssertSameExpectedTypeRector (#519)
* Revert "Drop MatchAssertEqualsExpectedTypeRector (#517)" This reverts commit a5fc689. * Revert "[CodeQuality] Rename MatchAssertSameExpectedTypeRector to MatchAssertEqualsExpectedTypeRector to only apply on assertEquals (#514)" This reverts commit 84fe73a. * add test * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 4bb086c commit e561212

File tree

13 files changed

+395
-0
lines changed

13 files changed

+395
-0
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 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;
@@ -45,6 +46,7 @@
4546
ConstructClassMethodToSetUpTestCaseRector::class,
4647

4748
AssertSameTrueFalseToAssertTrueFalseRector::class,
49+
MatchAssertSameExpectedTypeRector::class,
4850

4951
AssertEqualsToSameRector::class,
5052
PreferPHPUnitThisCallRector::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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipArgumentLess extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertSame(1);
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipDocblockType extends TestCase
8+
{
9+
public function run()
10+
{
11+
$this->assertSame('123', $this->getOrderId());
12+
}
13+
14+
/**
15+
* @return int
16+
*/
17+
private function getOrderId()
18+
{
19+
return '123';
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MatchAssertSameExpectedTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipFirstClassCallable extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertSame(...);
12+
}
13+
}
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: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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\Int_;
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+
if ($node->isFirstClassCallable()) {
90+
return null;
91+
}
92+
93+
if (count($node->getArgs()) < 2) {
94+
return null;
95+
}
96+
97+
$expectedArg = $node->getArgs()[0];
98+
if (! $expectedArg->value instanceof String_ && ! $expectedArg->value instanceof Int_) {
99+
return null;
100+
}
101+
102+
$expectedType = $this->getType($expectedArg->value);
103+
104+
$variableExpr = $node->getArgs()[1]
105+
->value;
106+
$variableType = $this->nodeTypeResolver->getNativeType($variableExpr);
107+
108+
$directVariableType = TypeCombinator::removeNull($variableType);
109+
110+
if ($expectedType->isLiteralString()->yes() && $directVariableType->isInteger()->yes()) {
111+
// update expected type to provided type
112+
$expectedArg->value = new Int_((int) $expectedArg->value->value);
113+
114+
return $node;
115+
}
116+
117+
if ($expectedType->isInteger()->yes() && $directVariableType->isString()->yes()) {
118+
// update expected type to provided type
119+
$expectedArg->value = new String_((string) $expectedArg->value->value);
120+
121+
return $node;
122+
}
123+
124+
return null;
125+
}
126+
}

0 commit comments

Comments
 (0)