Skip to content

Commit 9840750

Browse files
committed
[CodeQuality] Add AddInstanceofAssertForNullableInstanceRector
1 parent 757cd1f commit 9840750

File tree

11 files changed

+527
-3
lines changed

11 files changed

+527
-3
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\AddInstanceofAssertForNullableInstanceRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddInstanceofAssertForNullableInstanceRectorTest 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,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
9+
10+
final class AddInstanceOnlyOnce extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$value = $someObject->getSomeMethod();
16+
17+
$this->assertSame(123, $value);
18+
19+
// we know the value here, no need to add instanceof
20+
$value = $someObject->getSomeMethod();
21+
}
22+
23+
private function getSomeObject(): ?SomeClassUsedInTests
24+
{
25+
if (mt_rand(0, 1)) {
26+
return new SomeClassUsedInTests();
27+
}
28+
29+
return null;
30+
}
31+
}
32+
33+
?>
34+
-----
35+
<?php
36+
37+
declare(strict_types=1);
38+
39+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
40+
41+
use PHPUnit\Framework\TestCase;
42+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
43+
44+
final class AddInstanceOnlyOnce extends TestCase
45+
{
46+
public function test(): void
47+
{
48+
$someObject = $this->getSomeObject();
49+
$this->assertInstanceof(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests::class, $someObject);
50+
$value = $someObject->getSomeMethod();
51+
52+
$this->assertSame(123, $value);
53+
54+
// we know the value here, no need to add instanceof
55+
$value = $someObject->getSomeMethod();
56+
}
57+
58+
private function getSomeObject(): ?SomeClassUsedInTests
59+
{
60+
if (mt_rand(0, 1)) {
61+
return new SomeClassUsedInTests();
62+
}
63+
64+
return null;
65+
}
66+
}
67+
68+
?>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
9+
10+
final class CallOnNullable extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$value = $someObject->getSomeMethod();
16+
17+
$this->assertSame(123, $value);
18+
}
19+
20+
private function getSomeObject(): ?SomeClassUsedInTests
21+
{
22+
if (mt_rand(0, 1)) {
23+
return new SomeClassUsedInTests();
24+
}
25+
26+
return null;
27+
}
28+
}
29+
30+
?>
31+
-----
32+
<?php
33+
34+
declare(strict_types=1);
35+
36+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
37+
38+
use PHPUnit\Framework\TestCase;
39+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
40+
41+
final class CallOnNullable extends TestCase
42+
{
43+
public function test(): void
44+
{
45+
$someObject = $this->getSomeObject();
46+
$this->assertInstanceof(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests::class, $someObject);
47+
$value = $someObject->getSomeMethod();
48+
49+
$this->assertSame(123, $value);
50+
}
51+
52+
private function getSomeObject(): ?SomeClassUsedInTests
53+
{
54+
if (mt_rand(0, 1)) {
55+
return new SomeClassUsedInTests();
56+
}
57+
58+
return null;
59+
}
60+
}
61+
62+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class SkipIntNullable extends TestCase
10+
{
11+
public function test(): void
12+
{
13+
$someObject = $this->getValue();
14+
$value = $someObject->getSomeMethod();
15+
16+
$this->assertSame(123, $value);
17+
}
18+
19+
private function getValue(): ?int
20+
{
21+
if (mt_rand(0, 1)) {
22+
return 1000;
23+
}
24+
25+
return null;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
9+
10+
final class SkipNonNullableType extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$value = $someObject->getSomeMethod();
16+
17+
$this->assertSame(123, $value);
18+
}
19+
20+
private function getSomeObject(): SomeClassUsedInTests
21+
{
22+
return new SomeClassUsedInTests();
23+
}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source;
6+
7+
final class SomeClassUsedInTests
8+
{
9+
public function getSomeMethod(): int
10+
{
11+
return 1000;
12+
}
13+
}
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\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AddInstanceofAssertForNullableInstanceRector::class);
10+
};

rules-tests/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/Fixture/skip_sole_type.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use PHPUnit\Framework\MockObject\MockObject;
66
use PHPUnit\Framework\TestCase;
77
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook;
88

9-
class SkipSoleType extends TestCase
9+
final class SkipSoleType extends TestCase
1010
{
1111
private MockObject $anyBook;
1212
}

rules-tests/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector/Fixture/union_type_mock.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use PHPUnit\Framework\MockObject\MockObject;
66
use PHPUnit\Framework\TestCase;
77
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook;
88

9-
class UnionTypeMock extends TestCase
9+
final class UnionTypeMock extends TestCase
1010
{
1111
private MockObject|AnyBook $anyBook;
1212
}
@@ -21,7 +21,7 @@ use PHPUnit\Framework\MockObject\MockObject;
2121
use PHPUnit\Framework\TestCase;
2222
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\Source\AnyBook;
2323

24-
class UnionTypeMock extends TestCase
24+
final class UnionTypeMock extends TestCase
2525
{
2626
private MockObject $anyBook;
2727
}

0 commit comments

Comments
 (0)