Skip to content

Commit 3a83d1d

Browse files
authored
[DowngradePhp80] Add DowngradeInstanceofStringableRector (#275)
* [DowngradePhp80] Add DowngradeInstanceofStringableRector * fix * fix * rectify
1 parent 2b141c8 commit 3a83d1d

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

config/set/downgrade-php80.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector;
2626
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
2727
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
28+
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector;
2829
use Rector\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector;
2930
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
3031
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionClassGetConstantsFilterRector;
@@ -89,5 +90,6 @@
8990
DowngradeMixedTypeTypedPropertyRector::class,
9091
RemoveReturnTypeDeclarationFromCloneRector::class,
9192
DowngradeEnumToConstantListClassRector::class,
93+
DowngradeInstanceofStringableRector::class,
9294
]);
9395
};
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see https://wiki.php.net/rfc/stringable
17+
* @see \Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector\DowngradeInstanceofStringableRectorTest
18+
*/
19+
final class DowngradeInstanceofStringableRector extends AbstractRector
20+
{
21+
public function getRuleDefinition(): RuleDefinition
22+
{
23+
return new RuleDefinition('change instanceof Stringable to method_exists', [
24+
new CodeSample(
25+
<<<'CODE_SAMPLE'
26+
$obj instanceof \Stringable;
27+
CODE_SAMPLE
28+
29+
,
30+
<<<'CODE_SAMPLE'
31+
method_exists($obj, '__toString');
32+
CODE_SAMPLE
33+
),
34+
]);
35+
}
36+
37+
/**
38+
* @return array<class-string<Node>>
39+
*/
40+
public function getNodeTypes(): array
41+
{
42+
return [Instanceof_::class];
43+
}
44+
45+
/**
46+
* @param Instanceof_ $node
47+
*/
48+
public function refactor(Node $node): ?Node
49+
{
50+
if (! $node->class instanceof FullyQualified) {
51+
return null;
52+
}
53+
54+
if (! $this->isName($node->class, 'Stringable')) {
55+
return null;
56+
}
57+
58+
return $this->nodeFactory->createFuncCall('method_exists', [$node->expr, new String_('__toString')]);
59+
}
60+
}

0 commit comments

Comments
 (0)