Skip to content

Commit 3f62270

Browse files
authored
[CodeQuality] Add AddStubIntersectionVarToStubPropertyRector (#700)
* [CodeQuality] Skip AddIntersectionVarToMockObjectPropertyRector when @var already has intersection type * [CodeQuality] Add AddStubIntersectionVarToStubPropertyRector Add @var Stub&MockedClass intersection docblock to a bare native Stub property whose setUp() assigns createStub(). Generalize MockObjectPropertyDetector to be parametric on class name + create method, shared by both mock and stub rules. * [CodeQuality] Support self::createStub()/createMock() static calls in intersection @var rules Generalize MockObjectPropertyDetector to collect both $this->create*() (MethodCall) and self::create*() (StaticCall). Widen both rules' class resolvers accordingly. Add static-call fixtures. * [CodeQuality] Cover overwriting existing single @var to Stub intersection * [CodeQuality] Fix PHPStan: widen MethodCall|StaticCall param in RemoveNeverUsedMockPropertyRector * [CodeQuality] Merge BareVarToStubIntersectionRector into AddStubIntersectionVarToStubPropertyRector Iterate all native-Stub properties: derive the stubbed class from a setUp() createStub() arg when present, else from a bare single-class @var docblock. Absorbs and removes BareVarToStubIntersectionRector. Stub-first intersection ordering.
1 parent 51179e2 commit 3f62270

23 files changed

Lines changed: 490 additions & 241 deletions

‎config/sets/phpunit-code-quality.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rector\PHPUnit\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector;
77
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector;
88
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector;
9+
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector;
910
use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
1011
use Rector\PHPUnit\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector;
1112
use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector;
@@ -57,7 +58,6 @@
5758
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
5859
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
5960
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
60-
use Rector\PHPUnit\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
6161
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;
6262
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
6363
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
@@ -139,7 +139,7 @@
139139
ExpressionCreateMockToCreateStubRector::class,
140140
PropertyCreateMockToCreateStubRector::class,
141141
MockObjectVarToStubRector::class,
142-
BareVarToStubIntersectionRector::class,
142+
AddStubIntersectionVarToStubPropertyRector::class,
143143
InlineStubPropertyToCreateStubMethodCallRector::class,
144144

145145
// @test first, enable later

‎config/sets/phpunit-mock-to-stub.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddIntersectionVarToMockObjectPropertyRector;
7+
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector;
78
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;
89
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
910
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
1011
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
11-
use Rector\PHPUnit\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
1212
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;
1313

1414
return static function (RectorConfig $rectorConfig): void {
@@ -19,7 +19,7 @@
1919
ExpressionCreateMockToCreateStubRector::class,
2020
PropertyCreateMockToCreateStubRector::class,
2121
MockObjectVarToStubRector::class,
22-
BareVarToStubIntersectionRector::class,
2322
AddIntersectionVarToMockObjectPropertyRector::class,
23+
AddStubIntersectionVarToStubPropertyRector::class,
2424
]);
2525
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddIntersectionVarToMockObjectPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class StaticCallTest extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someServiceMock = self::createMock(\stdClass::class);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddIntersectionVarToMockObjectPropertyRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class StaticCallTest extends TestCase
26+
{
27+
/**
28+
* @var \PHPUnit\Framework\MockObject\MockObject&\stdClass
29+
*/
30+
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
31+
32+
protected function setUp(): void
33+
{
34+
$this->someServiceMock = self::createMock(\stdClass::class);
35+
}
36+
}
37+
38+
?>

rules-tests/PHPUnit120/Rector/Property/BareVarToStubIntersectionRector/BareVarToStubIntersectionRectorTest.php renamed to rules-tests/CodeQuality/Rector/Class_/AddStubIntersectionVarToStubPropertyRector/AddStubIntersectionVarToStubPropertyRectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
1010

11-
final class BareVarToStubIntersectionRectorTest extends AbstractRectorTestCase
11+
final class AddStubIntersectionVarToStubPropertyRectorTest extends AbstractRectorTestCase
1212
{
1313
#[DataProvider('provideData')]
1414
public function test(string $filePath): void
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class BareVarWithoutSetupTest extends TestCase
8+
{
9+
/**
10+
* @var FormBuilderInterface
11+
*/
12+
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
20+
21+
use PHPUnit\Framework\TestCase;
22+
23+
final class BareVarWithoutSetupTest extends TestCase
24+
{
25+
/**
26+
* @var \PHPUnit\Framework\MockObject\Stub&FormBuilderInterface
27+
*/
28+
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
29+
}
30+
31+
?>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SomeTest extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someServiceStub = $this->createStub(\stdClass::class);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class SomeTest extends TestCase
26+
{
27+
/**
28+
* @var \PHPUnit\Framework\MockObject\Stub&\stdClass
29+
*/
30+
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
31+
32+
protected function setUp(): void
33+
{
34+
$this->someServiceStub = $this->createStub(\stdClass::class);
35+
}
36+
}
37+
38+
?>
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\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class OverwriteExistingVarTest extends TestCase
8+
{
9+
/**
10+
* @var \stdClass
11+
*/
12+
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
13+
14+
protected function setUp(): void
15+
{
16+
$this->someServiceStub = $this->createStub(\stdClass::class);
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class OverwriteExistingVarTest extends TestCase
29+
{
30+
/**
31+
* @var \PHPUnit\Framework\MockObject\Stub&\stdClass
32+
*/
33+
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
34+
35+
protected function setUp(): void
36+
{
37+
$this->someServiceStub = $this->createStub(\stdClass::class);
38+
}
39+
}
40+
41+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipExistingIntersectionVarTest extends TestCase
8+
{
9+
/**
10+
* @var \PHPUnit\Framework\MockObject\Stub&\stdClass
11+
*/
12+
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
13+
14+
protected function setUp(): void
15+
{
16+
$this->someServiceStub = $this->createStub(\stdClass::class);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipMockObjectPropertyTest extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someServiceMock = $this->createStub(\stdClass::class);
14+
}
15+
}
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\Class_\AddStubIntersectionVarToStubPropertyRector\Fixture;
4+
5+
final class SkipNonTestClass
6+
{
7+
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
8+
9+
public function setUp(): void
10+
{
11+
$this->someServiceStub = $this->createStub(\stdClass::class);
12+
}
13+
}

0 commit comments

Comments
 (0)