Skip to content

Commit 0940d8b

Browse files
committed
[DowngradePhp80] Add DowngradeInstanceofStringableRector
1 parent 2b141c8 commit 0940d8b

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeInstanceofStringableRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
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 Fixture
6+
{
7+
public function run($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 Fixture
20+
{
21+
public function run($obj)
22+
{
23+
return method_exists($obj, '__toString');
24+
}
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector\Fixture;
4+
5+
class SkipDifferentObject
6+
{
7+
public function run($obj)
8+
{
9+
return $obj instanceof \stdClass;
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeInstanceofStringableRector::class);
10+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp80\Rector\Instanceof_;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Instanceof_;
9+
use PhpParser\Node\Name\FullyQualified;
10+
use PhpParser\Node\Scalar\String_;
11+
use Rector\PhpParser\Node\NodeFactory;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @see https://wiki.php.net/rfc/stringable
18+
* @see \Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector\DowngradeInstanceofStringableRectorTest
19+
*/
20+
final class DowngradeInstanceofStringableRector extends AbstractRector
21+
{
22+
public function __construct(
23+
private readonly NodeFactory $nodeFactory
24+
) {
25+
}
26+
27+
public function getRuleDefinition(): RuleDefinition
28+
{
29+
return new RuleDefinition('change instanceof Stringable to method_exists', [
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
$obj instanceof \Stringable;
33+
CODE_SAMPLE
34+
35+
,
36+
<<<'CODE_SAMPLE'
37+
method_exists($obj, '__toString');
38+
CODE_SAMPLE
39+
),
40+
]);
41+
}
42+
43+
/**
44+
* @return array<class-string<Node>>
45+
*/
46+
public function getNodeTypes(): array
47+
{
48+
return [Instanceof_::class];
49+
}
50+
51+
/**
52+
* @param Instanceof_ $node
53+
*/
54+
public function refactor(Node $node): ?Node
55+
{
56+
if (! $node->class instanceof FullyQualified) {
57+
return null;
58+
}
59+
60+
if (! $this->isName($node->class, 'Stringable')) {
61+
return null;
62+
}
63+
64+
return $this->nodeFactory->createFuncCall(
65+
'method_exists',
66+
[
67+
$node->expr,
68+
new String_('__toString'),
69+
]
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)