Skip to content

Commit 9d2e956

Browse files
authored
Add is_object() on DowngradeInstanceofStringableRector when possibly not object type (#276)
* Add is_object() on DowngradeInstanceofStringableRector * isobject
1 parent 3a83d1d commit 9d2e956

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector\Fixture;
4+
5+
class ExactlyObjectType
6+
{
7+
public function run(object $obj)
8+
{
9+
return $obj instanceof \Stringable;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector\Fixture;
18+
19+
class ExactlyObjectType
20+
{
21+
public function run(object $obj)
22+
{
23+
return method_exists($obj, '__toString');
24+
}
25+
}
26+
27+
?>

rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/fixture.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Fixture
2020
{
2121
public function run($obj)
2222
{
23-
return method_exists($obj, '__toString');
23+
return is_object($obj) && method_exists($obj, '__toString');
2424
}
2525
}
2626

rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Rector\DowngradePhp80\Rector\Instanceof_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
9+
use PhpParser\Node\Expr\FuncCall;
810
use PhpParser\Node\Expr\Instanceof_;
911
use PhpParser\Node\Name\FullyQualified;
1012
use PhpParser\Node\Scalar\String_;
@@ -28,7 +30,7 @@ public function getRuleDefinition(): RuleDefinition
2830

2931
,
3032
<<<'CODE_SAMPLE'
31-
method_exists($obj, '__toString');
33+
is_object($obj) && method_exists($obj, '__toString');
3234
CODE_SAMPLE
3335
),
3436
]);
@@ -45,7 +47,7 @@ public function getNodeTypes(): array
4547
/**
4648
* @param Instanceof_ $node
4749
*/
48-
public function refactor(Node $node): ?Node
50+
public function refactor(Node $node): null|FuncCall|BooleanAnd
4951
{
5052
if (! $node->class instanceof FullyQualified) {
5153
return null;
@@ -55,6 +57,13 @@ public function refactor(Node $node): ?Node
5557
return null;
5658
}
5759

58-
return $this->nodeFactory->createFuncCall('method_exists', [$node->expr, new String_('__toString')]);
60+
$nativeExprType = $this->nodeTypeResolver->getNativeType($node->expr);
61+
$funcCall = $this->nodeFactory->createFuncCall('method_exists', [$node->expr, new String_('__toString')]);
62+
63+
if ($nativeExprType->isObject()->yes()) {
64+
return $funcCall;
65+
}
66+
67+
return new BooleanAnd($this->nodeFactory->createFuncCall('is_object', [$node->expr]), $funcCall);
5968
}
6069
}

0 commit comments

Comments
 (0)