Skip to content

Commit 7d86878

Browse files
authored
[PHPUnit120] Add BareVarToStubIntersectionRector (#690)
* [PHPUnit120] Add BareVarToStubIntersectionRector to add &Stub to bare @var of Stub property * Null-check type early in isStubNativeType
1 parent a1dc81d commit 7d86878

16 files changed

Lines changed: 304 additions & 13 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
5858
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
5959
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
60+
use Rector\PHPUnit\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
6061
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;
6162
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
6263
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
@@ -138,6 +139,7 @@
138139
ExpressionCreateMockToCreateStubRector::class,
139140
PropertyCreateMockToCreateStubRector::class,
140141
MockObjectVarToStubRector::class,
142+
BareVarToStubIntersectionRector::class,
141143
InlineStubPropertyToCreateStubMethodCallRector::class,
142144

143145
// @test first, enable later

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
88
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
99
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
10+
use Rector\PHPUnit\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
1011
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;
1112

1213
return static function (RectorConfig $rectorConfig): void {
@@ -17,5 +18,6 @@
1718
ExpressionCreateMockToCreateStubRector::class,
1819
PropertyCreateMockToCreateStubRector::class,
1920
MockObjectVarToStubRector::class,
21+
BareVarToStubIntersectionRector::class,
2022
]);
2123
};

‎rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_compare_context.php.inc‎

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

33
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
44

5-
final class AssertCompareContext
5+
use Behat\Behat\Context\Context;
6+
7+
final class AssertCompareContext implements Context
68
{
79
public function some($response)
810
{
@@ -17,7 +19,9 @@ final class AssertCompareContext
1719

1820
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
1921

20-
final class AssertCompareContext
22+
use Behat\Behat\Context\Context;
23+
24+
final class AssertCompareContext implements Context
2125
{
2226
public function some($response)
2327
{

‎rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_null_compare_context.php.inc‎

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

33
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
44

5-
final class AssertNullCompareContext
5+
use Behat\Behat\Context\Context;
6+
7+
final class AssertNullCompareContext implements Context
68
{
79
public function some($response)
810
{
@@ -16,7 +18,9 @@ final class AssertNullCompareContext
1618

1719
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
1820

19-
final class AssertNullCompareContext
21+
use Behat\Behat\Context\Context;
22+
23+
final class AssertNullCompareContext implements Context
2024
{
2125
public function some($response)
2226
{

‎rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/method_exists_context.php.inc‎

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

33
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
44

5-
final class MethodExistsContext
5+
use Behat\Behat\Context\Context;
6+
7+
final class MethodExistsContext implements Context
68
{
79
public function some($response)
810
{
@@ -16,7 +18,9 @@ final class MethodExistsContext
1618

1719
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
1820

19-
final class MethodExistsContext
21+
use Behat\Behat\Context\Context;
22+
23+
final class MethodExistsContext implements Context
2024
{
2125
public function some($response)
2226
{

‎rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/simple_context.php.inc‎

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

33
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
44

5-
final class SimpleContext
5+
use Behat\Behat\Context\Context;
6+
7+
final class SimpleContext implements Context
68
{
79
public function some($response)
810
{
@@ -18,7 +20,9 @@ final class SimpleContext
1820

1921
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
2022

21-
final class SimpleContext
23+
use Behat\Behat\Context\Context;
24+
25+
final class SimpleContext implements Context
2226
{
2327
public function some($response)
2428
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
4+
5+
final class SchemaValidationContext
6+
{
7+
public function some($response)
8+
{
9+
assert($response instanceof \DateTime, 'only remaining option');
10+
}
11+
}
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\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class BareVarToStubIntersectionRectorTest 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class BareVarTest 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\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;
20+
21+
use PHPUnit\Framework\TestCase;
22+
23+
final class BareVarTest extends TestCase
24+
{
25+
/**
26+
* @var FormBuilderInterface&Stub
27+
*/
28+
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
29+
}
30+
31+
?>
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\PHPUnit120\Rector\Property\BareVarToStubIntersectionRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipAlreadyIntersectionTest extends TestCase
8+
{
9+
/**
10+
* @var FormBuilderInterface&Stub
11+
*/
12+
private \PHPUnit\Framework\MockObject\Stub $formBuilder;
13+
}

0 commit comments

Comments
 (0)