mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-30 21:30:23 +00:00
add IsObjectOnIncompleteClassRector
This commit is contained in:
parent
b18a98f6f9
commit
f4e883f6f2
@ -18,3 +18,4 @@ services:
|
||||
Rector\Php\Rector\FunctionLike\ParamScalarTypehintRector: ~
|
||||
Rector\Php\Rector\FunctionLike\ReturnScalarTypehintRector: ~
|
||||
Rector\Php\Rector\FuncCall\GetClassOnNullRector: ~
|
||||
Rector\Php\Rector\FuncCall\IsObjectOnIncompleteClassRector: ~
|
||||
|
@ -0,0 +1,79 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see http://php.net/manual/en/migration72.incompatible.php#migration72.incompatible.is_object-on-incomplete_class
|
||||
* @see https://3v4l.org/SpiE6
|
||||
*/
|
||||
final class IsObjectOnIncompleteClassRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Incomplete class returns inverted bool on is_object()', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$incompleteObject = new __PHP_Incomplete_Class;
|
||||
$isObject = is_object($incompleteObject);
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$incompleteObject = new __PHP_Incomplete_Class;
|
||||
$isObject = ! is_object($incompleteObject);
|
||||
CODE_SAMPLE
|
||||
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isName($node, 'is_object')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$types = $this->getTypes($node->args[0]->value);
|
||||
if ($types !== ['__PHP_Incomplete_Class']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->shouldSkip($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$booleanNotNode = new BooleanNot($node);
|
||||
$node->setAttribute(Attribute::PARENT_NODE, $booleanNotNode);
|
||||
|
||||
return $booleanNotNode;
|
||||
|
||||
}
|
||||
|
||||
private function shouldSkip(FuncCall $funcCallNode): bool
|
||||
{
|
||||
$parentNode = $funcCallNode->getAttribute(Attribute::PARENT_NODE);
|
||||
if (!$parentNode instanceof BooleanNot) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
$incompleteObject = new __PHP_Incomplete_Class;
|
||||
$isObject = is_object($incompleteObject);
|
||||
|
||||
$classicObject = new stdClass();
|
||||
$isObject = is_object($classicObject);
|
||||
|
||||
/** @var __PHP_Incomplete_Class $classicObject */
|
||||
$classicObject = new stdClass();
|
||||
$isObject = is_object($classicObject);
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
$incompleteObject = new __PHP_Incomplete_Class;
|
||||
$isObject = !is_object($incompleteObject);
|
||||
|
||||
$classicObject = new stdClass();
|
||||
$isObject = is_object($classicObject);
|
||||
|
||||
/** @var __PHP_Incomplete_Class $classicObject */
|
||||
$classicObject = new stdClass();
|
||||
$isObject = !is_object($classicObject);
|
||||
|
||||
?>
|
@ -0,0 +1,21 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Tests\Rector\FuncCall\IsObjectOnIncompleteClassRector;
|
||||
|
||||
use Rector\Php\Rector\FuncCall\IsObjectOnIncompleteClassRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class IsObjectOnIncompleteClassRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([
|
||||
__DIR__ . '/Fixture/fixture.php.inc'
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return IsObjectOnIncompleteClassRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\Php\Rector\FuncCall\IsObjectOnIncompleteClassRector: ~
|
Loading…
Reference in New Issue
Block a user