Skip to content

Commit 0d8fbf8

Browse files
authored
[CodeQuality] Add AddInstanceofAssertForNullableInstanceRector (#466)
* [CodeQuality] Add AddInstanceofAssertForNullableInstanceRector * update config sort packages * register rule to code quality set * cleanup * add collection
1 parent 757cd1f commit 0d8fbf8

File tree

16 files changed

+601
-17
lines changed

16 files changed

+601
-17
lines changed

composer.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
"phpstan/phpstan": "^2.1.8",
1313
"phpecs/phpecs": "^2.0",
1414
"phpstan/extension-installer": "^1.4",
15-
"symplify/vendor-patches": "^11.3",
15+
"symplify/vendor-patches": "^11.4",
1616
"tracy/tracy": "^2.10",
1717
"tomasvotruba/class-leak": "^1.2",
1818
"rector/swiss-knife": "^1.0",
19-
"phpstan/phpstan-webmozart-assert": "^2.0"
19+
"phpstan/phpstan-webmozart-assert": "^2.0",
20+
"rector/type-perfect": "^2.0"
2021
},
2122
"autoload": {
2223
"psr-4": {
@@ -47,17 +48,15 @@
4748
"rector": "vendor/bin/rector process --ansi"
4849
},
4950
"extra": {
50-
"enable-patching": true,
51-
"branch-alias": {
52-
"dev-main": "0.11-dev"
53-
}
51+
"enable-patching": true
5452
},
5553
"conflict": {
56-
"rector/rector": "<0.11"
54+
"rector/rector": "<2.0"
5755
},
5856
"minimum-stability": "dev",
5957
"prefer-stable": true,
6058
"config": {
59+
"sort-packages": true,
6160
"allow-plugins": {
6261
"cweagans/composer-patches": true,
6362
"rector/extension-installer": true,

config/sets/phpunit-code-quality.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector;
1010
use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector;
1111
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
12+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
1213
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector;
1314
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
1415
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
@@ -75,6 +76,9 @@
7576
AssertEmptyNullableObjectToAssertInstanceofRector::class,
7677
AssertCountWithZeroToAssertEmptyRector::class,
7778

79+
// avoid call on nullable object
80+
AddInstanceofAssertForNullableInstanceRector::class,
81+
7882
/**
7983
* Improve direct testing of your code, without mock creep. Make it simple, clear and easy to maintain:
8084
*

phpstan.neon

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ parameters:
1919
- */Fixture/*
2020
- */Expected/*
2121

22-
# to be enabled later once rector upgraded to use phpstan v2
23-
# # see https://github.com/rectorphp/type-perfect/
24-
# type_perfect:
25-
# no_mixed: true
26-
# null_over_false: true
27-
# narrow_param: true
28-
# narrow_return: true
22+
# see https://github.com/rectorphp/type-perfect/
23+
type_perfect:
24+
no_mixed: true
25+
null_over_false: true
26+
narrow_param: true
27+
narrow_return: true
2928

3029
ignoreErrors:
3130
# phpstan false positive
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+
};

0 commit comments

Comments
 (0)