Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;

use CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject;
use PHPUnit\Framework\TestCase;

final class IncludeAssertNotNull extends TestCase
{
public function test()
{
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;

$this->assertNotNull($someObject);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;

use CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject;
use PHPUnit\Framework\TestCase;

final class IncludeAssertNotNull extends TestCase
{
public function test()
{
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;

$this->assertInstanceOf(\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject::class, $someObject);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;

use CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject;
use PHPUnit\Framework\TestCase;

final class IncludeAssertNull extends TestCase
{
public function test()
{
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;

$this->assertNull($someObject);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Fixture;

use CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject;
use PHPUnit\Framework\TestCase;

final class IncludeAssertNull extends TestCase
{
public function test()
{
$someObject = mt_rand(0, 1) ? new SomeTypeObject() : null;

$this->assertNotInstanceOf(\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector\Source\SomeTypeObject::class, $someObject);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private function isTestFilePath(FuncCall $funcCall): bool
if (str_ends_with($className, 'Test')) {
return true;
}

return str_ends_with($className, 'TestCase');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public function __construct(

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change assertNotEmpty() on an object to more clear assertInstanceof()', [
new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition(
'Change assertNotEmpty() and assertNotNull() on an object to more clear assertInstanceof()',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

class SomeClass extends TestCase
Expand All @@ -46,8 +48,8 @@ public function test()
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

class SomeClass extends TestCase
Expand All @@ -60,8 +62,10 @@ public function test()
}
}
CODE_SAMPLE
),
]);
),

]
);
}

/**
Expand All @@ -81,7 +85,7 @@ public function refactor(Node $node): ?Node
return null;
}

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

Expand All @@ -105,7 +109,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

Expand Down