Skip to content

Commit

Permalink
add IsObjectOnIncompleteClassRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 14, 2018
1 parent b18a98f commit f4e883f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/level/php/php72.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
services:
Rector\Php\Rector\FuncCall\IsObjectOnIncompleteClassRector: ~

0 comments on commit f4e883f

Please sign in to comment.