Skip to content

Commit fe23a4d

Browse files
[code-quality] Add AddInstanceofAssertForNullableArgumentRector (#601)
* [code-quality] Add AddInstanceofAssertForNullableArgumentRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent bfd8312 commit fe23a4d

File tree

12 files changed

+494
-36
lines changed

12 files changed

+494
-36
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@
101101

102102
// avoid call on nullable object
103103
AddInstanceofAssertForNullableInstanceRector::class,
104+
105+
// enable next after testing
106+
// \Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector::class,
107+
104108
AssertArrayCastedObjectToAssertSameRector::class,
105109

106110
/**
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\AddInstanceofAssertForNullableArgumentRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddInstanceofAssertForNullableArgumentRectorTest 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\AddInstanceofAssertForNullableArgumentRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests;
9+
10+
final class PassingNullableArg extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$this->process($someObject);
16+
}
17+
18+
private function getSomeObject(): ?SomeClassUsedInTests
19+
{
20+
if (mt_rand(0, 1)) {
21+
return new SomeClassUsedInTests();
22+
}
23+
24+
return null;
25+
}
26+
27+
private function process(SomeClassUsedInTests $someObject): void
28+
{
29+
// non nullable required
30+
}
31+
}
32+
33+
?>
34+
-----
35+
<?php
36+
37+
declare(strict_types=1);
38+
39+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Fixture;
40+
41+
use PHPUnit\Framework\TestCase;
42+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests;
43+
44+
final class PassingNullableArg extends TestCase
45+
{
46+
public function test(): void
47+
{
48+
$someObject = $this->getSomeObject();
49+
$this->assertInstanceOf(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests::class, $someObject);
50+
$this->process($someObject);
51+
}
52+
53+
private function getSomeObject(): ?SomeClassUsedInTests
54+
{
55+
if (mt_rand(0, 1)) {
56+
return new SomeClassUsedInTests();
57+
}
58+
59+
return null;
60+
}
61+
62+
private function process(SomeClassUsedInTests $someObject): void
63+
{
64+
// non nullable required
65+
}
66+
}
67+
68+
?>
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\AddInstanceofAssertForNullableArgumentRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests;
9+
10+
final class SkipNonNullableType extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
16+
$this->process($someObject);
17+
}
18+
19+
private function getSomeObject(): SomeClassUsedInTests
20+
{
21+
return new SomeClassUsedInTests();
22+
}
23+
24+
private function process(SomeClassUsedInTests $someObject)
25+
{
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source;
6+
7+
final class SomeClassUsedInTests
8+
{
9+
public function getSomeMethod(): int
10+
{
11+
return 1000;
12+
}
13+
14+
public function getSomeNestedObject(): ?SomeNestedObject
15+
{
16+
return new SomeNestedObject();
17+
}
18+
}
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\AddInstanceofAssertForNullableArgumentRector\Source;
6+
7+
final class SomeNestedObject
8+
{
9+
public function getNumber(): int
10+
{
11+
return 456;
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\AddInstanceofAssertForNullableArgumentRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AddInstanceofAssertForNullableArgumentRector::class);
10+
};
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\CodeQuality\NodeFactory;
6+
7+
use PhpParser\Node\Arg;
8+
use PhpParser\Node\Expr\ClassConstFetch;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Name\FullyQualified;
12+
use PhpParser\Node\Stmt\Expression;
13+
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToType;
14+
15+
final class AssertMethodCallFactory
16+
{
17+
public function createAssertInstanceOf(VariableNameToType $variableNameToType): Expression
18+
{
19+
$args = [
20+
new Arg(new ClassConstFetch(new FullyQualified($variableNameToType->getObjectType()), 'class')),
21+
new Arg(new Variable($variableNameToType->getVariableName())),
22+
];
23+
24+
$methodCall = new MethodCall(new Variable('this'), 'assertInstanceOf', $args);
25+
26+
return new Expression($methodCall);
27+
}
28+
}

0 commit comments

Comments
 (0)