Skip to content

Commit f3a9bcb

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

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-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/FuncCall/AssertFuncCallToPHPUnitAssertRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private function isTestFilePath(FuncCall $funcCall): bool
149149
if (str_ends_with($className, 'Test')) {
150150
return true;
151151
}
152+
152153
return str_ends_with($className, 'TestCase');
153154
}
154155

rules/CodeQuality/Rector/MethodCall/AssertEmptyNullableObjectToAssertInstanceofRector.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ 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(
34+
'Change assertNotEmpty() and assertNotNull() on an object to more clear assertInstanceof()',
35+
[
3436
new CodeSample(
3537
<<<'CODE_SAMPLE'
3638
use PHPUnit\Framework\TestCase;
@@ -61,6 +63,7 @@ public function test()
6163
}
6264
CODE_SAMPLE
6365
),
66+
6467
]);
6568
}
6669

@@ -81,7 +84,7 @@ public function refactor(Node $node): ?Node
8184
return null;
8285
}
8386

84-
if (! $this->isNames($node->name, ['assertNotEmpty', 'assertEmpty'])) {
87+
if (! $this->isNames($node->name, ['assertNotEmpty', 'assertEmpty', 'assertNull', 'assertNotNull'])) {
8588
return null;
8689
}
8790

@@ -105,7 +108,10 @@ public function refactor(Node $node): ?Node
105108
return null;
106109
}
107110

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

110116
$node->name = new Identifier($methodName);
111117

0 commit comments

Comments
 (0)