Skip to content

Commit f7813ed

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

File tree

17 files changed

+624
-81
lines changed

17 files changed

+624
-81
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;
1515
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
1616
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
17+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector;
1718
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector;
1819
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\EntityDocumentCreateMockToDirectNewRector;
1920
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
@@ -132,5 +133,6 @@
132133
GetMockBuilderGetMockToCreateMockRector::class,
133134
EntityDocumentCreateMockToDirectNewRector::class,
134135
ReplaceAtMethodWithDesiredMatcherRector::class,
136+
BareCreateMockAssignToDirectUseRector::class,
135137
]);
136138
};
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MultipleUsesAsNoConnection extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);
12+
13+
$this->useMock($someMock);
14+
$this->useMockAgain($someMock);
15+
}
16+
17+
private function useMock($someMock)
18+
{
19+
}
20+
21+
private function useMockAgain($someMock)
22+
{
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
31+
32+
use PHPUnit\Framework\TestCase;
33+
34+
final class MultipleUsesAsNoConnection extends TestCase
35+
{
36+
public function test()
37+
{
38+
$this->useMock($this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class));
39+
$this->useMockAgain($this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class));
40+
}
41+
42+
private function useMock($someMock)
43+
{
44+
}
45+
46+
private function useMockAgain($someMock)
47+
{
48+
}
49+
}
50+
51+
?>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipUsedAsMock extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);
12+
13+
$someMock->expects($this->atLeastOnce())
14+
->method('some');
15+
16+
$this->useMock($someMock);
17+
}
18+
19+
private function useMock($someMock)
20+
{
21+
}
22+
}
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(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);
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(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class));
34+
}
35+
36+
private function useMock($someMock)
37+
{
38+
}
39+
}
40+
41+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source;
6+
7+
final class AnotherClass
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([BareCreateMockAssignToDirectUseRector::class]);

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
{

0 commit comments

Comments
 (0)