Skip to content

Commit 2b08d3e

Browse files
committed
[code-quality] Add WithCallbackIdenticalToStandaloneAssertsRector
1 parent 6960a96 commit 2b08d3e

File tree

12 files changed

+586
-0
lines changed

12 files changed

+586
-0
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
4646
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
4747
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;
48+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector;
4849
use Rector\PHPUnit\CodeQuality\Rector\StmtsAwareInterface\DeclareStrictTypesTestsRector;
4950
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
5051
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
@@ -121,6 +122,7 @@
121122

122123
FinalizeTestCaseClassRector::class,
123124
DeclareStrictTypesTestsRector::class,
125+
WithCallbackIdenticalToStandaloneAssertsRector::class,
124126

125127
// prefer simple mocking
126128
GetMockBuilderGetMockToCreateMockRector::class,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipYieldFromExpr extends TestCase
8+
{
9+
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
10+
public function test(string $val1, string $val2): void
11+
{
12+
}
13+
14+
public static function dataProvider(): iterable
15+
{
16+
yield from self::someData();
17+
}
18+
19+
public static function someData(): iterable
20+
{
21+
yield ['value1', 'value2'];
22+
yield ['value3', 'value4'];
23+
yield ['value5', 'value6'];
24+
yield ['value7', 'value8'];
25+
}
26+
}
27+
28+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ClosureInstanceOf extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 && $args[0] instanceof \stdClass;
17+
}));
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class ClosureInstanceOf extends TestCase
30+
{
31+
public function test()
32+
{
33+
$someMock = $this->getMockBuilder('AnyType')->getMock();
34+
35+
$someMock->expects($this->any())
36+
->method('trans')
37+
->with($this->callback(function ($args): void {
38+
$this->assertInstanceOf(\stdClass::class, $args[0]);
39+
$this->assertCount(5, $args);
40+
}));
41+
}
42+
}
43+
44+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipMultipleStmts extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
$result = false;
17+
return count($args) === 5 && $args[0] === 'some_value';
18+
}));
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipOr extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 || $args[0] === 'some_value';
17+
}));
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class WithArrowFunction extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with(
16+
$this->callback(
17+
fn ($args): bool => count($args) === 5 && $args[0] instanceof \stdClass
18+
)
19+
);
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
28+
29+
use PHPUnit\Framework\TestCase;
30+
31+
final class WithArrowFunction extends TestCase
32+
{
33+
public function test()
34+
{
35+
$someMock = $this->getMockBuilder('AnyType')->getMock();
36+
37+
$someMock->expects($this->any())
38+
->method('trans')
39+
->with(
40+
$this->callback(
41+
function ($args): void {
42+
$this->assertInstanceOf(\stdClass::class, $args[0]);
43+
$this->assertCount(5, $args);
44+
}
45+
)
46+
);
47+
}
48+
}
49+
50+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class WithCallbackAssert extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 && $args[0] === 'some_value';
17+
}));
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class WithCallbackAssert extends TestCase
30+
{
31+
public function test()
32+
{
33+
$someMock = $this->getMockBuilder('AnyType')->getMock();
34+
35+
$someMock->expects($this->any())
36+
->method('trans')
37+
->with($this->callback(function ($args): void {
38+
$this->assertSame('some_value', $args[0]);
39+
$this->assertCount(5, $args);
40+
}));
41+
}
42+
}
43+
44+
?>
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\WithCallbackIdenticalToStandaloneAssertsRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class WithCallbackIdenticalToStandaloneAssertsRectorTest 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\WithCallbackIdenticalToStandaloneAssertsRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(WithCallbackIdenticalToStandaloneAssertsRector::class);
10+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\NodeFactory;
6+
7+
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\BinaryOp\Identical;
9+
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Expr\FuncCall;
11+
use PhpParser\Node\Expr\Instanceof_;
12+
use PhpParser\Node\Name;
13+
use PhpParser\Node\Name\FullyQualified;
14+
use PhpParser\Node\Scalar\Int_;
15+
use PhpParser\Node\Stmt;
16+
use PhpParser\Node\Stmt\Expression;
17+
use Rector\NodeNameResolver\NodeNameResolver;
18+
use Rector\PhpParser\Node\NodeFactory;
19+
20+
final readonly class FromBinaryAndAssertExpressionsFactory
21+
{
22+
public function __construct(
23+
private NodeFactory $nodeFactory,
24+
private NodeNameResolver $nodeNameResolver,
25+
) {
26+
}
27+
28+
/**
29+
* @param Expr[] $exprs
30+
* @return Stmt[]|null
31+
*/
32+
public function create(array $exprs): ?array
33+
{
34+
$assertMethodCalls = [];
35+
36+
foreach ($exprs as $expr) {
37+
if ($expr instanceof Instanceof_) {
38+
if (! $expr->class instanceof Name) {
39+
return null;
40+
}
41+
42+
$className = $expr->class->name;
43+
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
44+
'this',
45+
'assertInstanceOf',
46+
[new ClassConstFetch(new FullyQualified($className), 'class'), $expr->expr]
47+
);
48+
49+
continue;
50+
}
51+
52+
if ($expr instanceof Identical) {
53+
if ($expr->left instanceof FuncCall && $this->nodeNameResolver->isName($expr->left, 'count')) {
54+
if ($expr->right instanceof Int_) {
55+
$countedExpr = $expr->left->getArgs()[0]
56+
->value;
57+
58+
// create assertCount()
59+
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
60+
'this',
61+
'assertCount',
62+
[$expr->right, $countedExpr]
63+
);
64+
65+
continue;
66+
}
67+
68+
// unclear, fallback to no change
69+
return null;
70+
}
71+
72+
// create assertSame()
73+
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
74+
'this',
75+
'assertSame',
76+
[$expr->right, $expr->left]
77+
);
78+
} else {
79+
// not supported expr
80+
return null;
81+
}
82+
}
83+
84+
if ($assertMethodCalls === []) {
85+
return null;
86+
}
87+
88+
$stmts = [];
89+
foreach ($assertMethodCalls as $assertMethodCall) {
90+
$stmts[] = new Expression($assertMethodCall);
91+
}
92+
93+
return $stmts;
94+
}
95+
}

0 commit comments

Comments
 (0)