Skip to content

Commit 719435b

Browse files
committed
Add is_object() on DowngradeInstanceofStringableRector
1 parent 3a83d1d commit 719435b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\DowngradePhp80\Rector\Instanceof_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
89
use PhpParser\Node\Expr\Instanceof_;
910
use PhpParser\Node\Name\FullyQualified;
1011
use PhpParser\Node\Scalar\String_;
@@ -28,7 +29,7 @@ public function getRuleDefinition(): RuleDefinition
2829

2930
,
3031
<<<'CODE_SAMPLE'
31-
method_exists($obj, '__toString');
32+
is_object($obj) && method_exists($obj, '__toString');
3233
CODE_SAMPLE
3334
),
3435
]);
@@ -45,7 +46,7 @@ public function getNodeTypes(): array
4546
/**
4647
* @param Instanceof_ $node
4748
*/
48-
public function refactor(Node $node): ?Node
49+
public function refactor(Node $node): ?BooleanAnd
4950
{
5051
if (! $node->class instanceof FullyQualified) {
5152
return null;
@@ -55,6 +56,9 @@ public function refactor(Node $node): ?Node
5556
return null;
5657
}
5758

58-
return $this->nodeFactory->createFuncCall('method_exists', [$node->expr, new String_('__toString')]);
59+
return new BooleanAnd(
60+
$this->nodeFactory->createFuncCall('is_object', [$node->expr]),
61+
$this->nodeFactory->createFuncCall('method_exists', [$node->expr, new String_('__toString')])
62+
);
5963
}
6064
}

0 commit comments

Comments
 (0)