Skip to content

Commit 15a2aab

Browse files
add custom message support to AssertEmptyNullableObjectToAssertInstanceofRector (#461)
* add custom message support to AssertEmptyNullableObjectToAssertInstanceofRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent ead02de commit 15a2aab

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;
4+
5+
use CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class KeepCustomMessage extends TestCase
9+
{
10+
public function test()
11+
{
12+
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;
13+
14+
$this->assertNull($someObject, 'some message');
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;
23+
24+
use CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject;
25+
use PHPUnit\Framework\TestCase;
26+
27+
final class KeepCustomMessage extends TestCase
28+
{
29+
public function test()
30+
{
31+
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;
32+
33+
$this->assertNotInstanceOf(\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject::class, $someObject, 'some message');
34+
}
35+
}
36+
37+
?>

rules/CodeQuality/Rector/MethodCall/AssertEmptyNullableObjectToAssertInstanceofRector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function test()
4747
}
4848
}
4949
CODE_SAMPLE
50-
5150
,
5251
<<<'CODE_SAMPLE'
5352
use PHPUnit\Framework\TestCase;
@@ -99,7 +98,6 @@ public function refactor(Node $node): ?Node
9998
}
10099

101100
$firstArgType = $this->getType($firstArg->value);
102-
103101
if (! $firstArgType instanceof UnionType) {
104102
return null;
105103
}
@@ -118,9 +116,15 @@ public function refactor(Node $node): ?Node
118116

119117
$fullyQualified = new FullyQualified($pureType->getClassName());
120118

119+
$customMessageArg = $node->getArgs()[1] ?? null;
120+
121121
$node->args[0] = new Arg(new ClassConstFetch($fullyQualified, 'class'));
122122
$node->args[1] = $firstArg;
123123

124+
if ($customMessageArg instanceof Arg) {
125+
$node->args[] = $customMessageArg;
126+
}
127+
124128
return $node;
125129
}
126130
}

0 commit comments

Comments
 (0)