Skip to content

Commit 18d84eb

Browse files
committed
covert assertNotNull() in AssertEmptyNullableObjectToAssertInstanceofRector
1 parent 57a5555 commit 18d84eb

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
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 IncludeAssertNotNull extends TestCase
9+
{
10+
public function test()
11+
{
12+
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;
13+
14+
$this->assertNotNull($someObject);
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 IncludeAssertNotNull extends TestCase
28+
{
29+
public function test()
30+
{
31+
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;
32+
33+
$this->assertInstanceOf(\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject::class, $someObject);
34+
}
35+
}
36+
37+
?>
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 IncludeAssertNull extends TestCase
9+
{
10+
public function test()
11+
{
12+
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;
13+
14+
$this->assertNull($someObject);
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 IncludeAssertNull 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);
34+
}
35+
}
36+
37+
?>

rules/CodeQuality/Rector/MethodCall/AssertEmptyNullableObjectToAssertInstanceofRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030

3131
public function getRuleDefinition(): RuleDefinition
3232
{
33-
return new RuleDefinition('Change assertNotEmpty() on an object to more clear assertInstanceof()', [
33+
return new RuleDefinition('Change assertNotEmpty() and assertNotNull() on an object to more clear assertInstanceof()', [
3434
new CodeSample(
3535
<<<'CODE_SAMPLE'
3636
use PHPUnit\Framework\TestCase;
@@ -81,7 +81,7 @@ public function refactor(Node $node): ?Node
8181
return null;
8282
}
8383

84-
if (! $this->isNames($node->name, ['assertNotEmpty', 'assertEmpty'])) {
84+
if (! $this->isNames($node->name, ['assertNotEmpty', 'assertEmpty', 'assertNull', 'assertNotNull'])) {
8585
return null;
8686
}
8787

@@ -105,7 +105,7 @@ public function refactor(Node $node): ?Node
105105
return null;
106106
}
107107

108-
$methodName = $this->isName($node->name, 'assertEmpty') ? 'assertNotInstanceOf' : 'assertInstanceOf';
108+
$methodName = $this->isNames($node->name, ['assertEmpty', 'assertNull']) ? 'assertNotInstanceOf' : 'assertInstanceOf';
109109

110110
$node->name = new Identifier($methodName);
111111

0 commit comments

Comments
 (0)