forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b18a98f
commit f4e883f
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/Php/src/Rector/FuncCall/IsObjectOnIncompleteClassRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/Php/tests/Rector/FuncCall/IsObjectOnIncompleteClassRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
...s/Rector/FuncCall/IsObjectOnIncompleteClassRector/IsObjectOnIncompleteClassRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/Php/tests/Rector/FuncCall/IsObjectOnIncompleteClassRector/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
services: | ||
Rector\Php\Rector\FuncCall\IsObjectOnIncompleteClassRector: ~ |