Skip to content

Commit b49146b

Browse files
committed
[code-quality] Add BareCreateMockAssignToDirectUseRector
1 parent f9c08fa commit b49146b

File tree

9 files changed

+200
-7
lines changed

9 files changed

+200
-7
lines changed
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\ClassMethod\BareCreateMockAssignToDirectUseRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class BareCreateMockAssignToDirectUseRectorTest 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: 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\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SomeClass extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->createMock('SomeClass');
12+
13+
$this->useMock($someMock);
14+
}
15+
16+
private function useMock($someMock)
17+
{
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class SomeClass extends TestCase
30+
{
31+
public function test()
32+
{
33+
$this->useMock($this->createMock('SomeClass'));
34+
}
35+
36+
private function useMock($someMock)
37+
{
38+
}
39+
}
40+
41+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->rules(
9+
rectorClasses: [\Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector::class]
10+
);
11+
};

rules-tests/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector/Fixture/skip_comment_inline.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -47,4 +47,4 @@ final class SkipCommentInline extends TestCase
4747
['content123', 12], // a comment inline
4848
];
4949
}
50-
}
50+
}

rules-tests/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector/Fixture/skip_no_array.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

rules-tests/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector/Fixture/skip_non_phpunit.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
44

55
final class SkipNonPhpUnit
66
{

rules-tests/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector/Fixture/skip_test_method.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

rules-tests/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector/Fixture/some_class.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -24,7 +24,7 @@ final class ImageBinaryTest extends TestCase
2424
-----
2525
<?php
2626

27-
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
27+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewlinedRector\Fixture;
2828

2929
use PHPUnit\Framework\TestCase;
3030

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use PhpParser\Node\Stmt\Foreach_;
10+
use Rector\PHPUnit\CodeQuality\NodeAnalyser\NullableObjectAssignCollector;
11+
use Rector\PHPUnit\CodeQuality\NodeFactory\AssertMethodCallFactory;
12+
use Rector\PHPUnit\CodeQuality\TypeAnalyzer\MethodCallParameterTypeResolver;
13+
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
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+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\BareCreateMockAssignToDirectUseRectorTest
21+
*/
22+
final class BareCreateMockAssignToDirectUseRector extends AbstractRector
23+
{
24+
public function __construct(
25+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
26+
private readonly NullableObjectAssignCollector $nullableObjectAssignCollector,
27+
private readonly AssertMethodCallFactory $assertMethodCallFactory,
28+
private readonly MethodCallParameterTypeResolver $methodCallParameterTypeResolver,
29+
private readonly AssertCallAnalyzer $assertCallAnalyzer,
30+
) {
31+
}
32+
33+
public function getRuleDefinition(): RuleDefinition
34+
{
35+
return new RuleDefinition(
36+
'Add explicit instance assert between above nullable object pass',
37+
[
38+
new CodeSample(
39+
<<<'CODE_SAMPLE'
40+
use PHPUnit\Framework\TestCase;
41+
42+
final class SomeTest extends TestCase
43+
{
44+
public function test()
45+
{
46+
$someObject = $this->createMock(SomeClass::class);
47+
$this->process($someObject);
48+
}
49+
50+
51+
private function process(SomeClass $someObject): void
52+
{
53+
}
54+
}
55+
CODE_SAMPLE
56+
57+
,
58+
<<<'CODE_SAMPLE'
59+
use PHPUnit\Framework\TestCase;
60+
61+
final class SomeTest extends TestCase
62+
{
63+
public function test()
64+
{
65+
$this->process($this->createMock(SomeClass::class));
66+
}
67+
68+
private function process(SomeClass $someObject): void
69+
{
70+
}
71+
}
72+
CODE_SAMPLE
73+
),
74+
]
75+
);
76+
}
77+
78+
/**
79+
* @return array<class-string<Node>>
80+
*/
81+
public function getNodeTypes(): array
82+
{
83+
return [ClassMethod::class, Foreach_::class];
84+
}
85+
86+
/**
87+
* @param ClassMethod|Foreach_ $node
88+
*/
89+
public function refactor(Node $node): ?Node
90+
{
91+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
92+
return null;
93+
}
94+
95+
if ($node->stmts === null || count($node->stmts) < 2) {
96+
return null;
97+
}
98+
99+
$hasChanged = false;
100+
101+
$next = 0;
102+
foreach ($node->stmts as $key => $stmt) {
103+
104+
++$next;
105+
}
106+
107+
if (! $hasChanged) {
108+
return null;
109+
}
110+
111+
return $node;
112+
}
113+
}

0 commit comments

Comments
 (0)