-
-
Notifications
You must be signed in to change notification settings - Fork 708
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
b1143d5
commit c622589
Showing
5 changed files
with
179 additions
and
1 deletion.
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
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
99 changes: 99 additions & 0 deletions
99
rules/php80/src/Rector/Class_/StringableForToStringRector.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,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use Rector\Core\PhpParser\Node\Manipulator\ClassManipulator; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://wiki.php.net/rfc/stringable | ||
* | ||
* @see \Rector\Php80\Tests\Rector\Class_\StringableForToStringRector\StringableForToStringRectorTest | ||
*/ | ||
final class StringableForToStringRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const STRINGABLE = 'Stringable'; | ||
|
||
/** | ||
* @var ClassManipulator | ||
*/ | ||
private $classManipulator; | ||
|
||
public function __construct(ClassManipulator $classManipulator) | ||
{ | ||
$this->classManipulator = $classManipulator; | ||
} | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Add `Stringable` interface to classes with `__toString()` method', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
class SomeClass | ||
{ | ||
public function __toString() | ||
{ | ||
return 'I can stringz'; | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
class SomeClass implements Stringable | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'I can stringz'; | ||
} | ||
} | ||
PHP | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$toStringClassMethod = $node->getMethod('__toString'); | ||
if ($toStringClassMethod === null) { | ||
return null; | ||
} | ||
|
||
if ($this->classManipulator->hasInterface($node, self::STRINGABLE)) { | ||
return null; | ||
} | ||
|
||
// add interface | ||
$node->implements[] = new FullyQualified(self::STRINGABLE); | ||
|
||
// add return type | ||
|
||
if ($toStringClassMethod->returnType === null) { | ||
$toStringClassMethod->returnType = new Name('string'); | ||
} | ||
|
||
return $node; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
rules/php80/tests/Rector/Class_/StringableForToStringRector/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 | ||
|
||
namespace Rector\Php80\Tests\Rector\Class_\StringableForToStringRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function __toString() | ||
{ | ||
return 'I can stringz'; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Php80\Tests\Rector\Class_\StringableForToStringRector\Fixture; | ||
|
||
class SomeClass implements \Stringable | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'I can stringz'; | ||
} | ||
} | ||
|
||
?> |
30 changes: 30 additions & 0 deletions
30
...php80/tests/Rector/Class_/StringableForToStringRector/StringableForToStringRectorTest.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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\Tests\Rector\Class_\StringableForToStringRector; | ||
|
||
use Iterator; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\Php80\Rector\Class_\StringableForToStringRector; | ||
|
||
final class StringableForToStringRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $file): void | ||
{ | ||
$this->doTestFile($file); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return StringableForToStringRector::class; | ||
} | ||
} |