Skip to content

Commit

Permalink
Updated Rector to commit 6aed089476ea0167835fbbdb42d876cddb4c77d4
Browse files Browse the repository at this point in the history
rectorphp/rector-src@6aed089 [Php80] Skip when already implements Stringable and has string return type on StringableForToStringRector (#5120)
  • Loading branch information
TomasVotruba committed Oct 5, 2023
1 parent b1fd9df commit 90920e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions rules/Php80/Rector/Class_/StringableForToStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ final class StringableForToStringRector extends AbstractRector implements MinPhp
* @var string
*/
private const STRINGABLE = 'Stringable';
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer, ReturnTypeInferer $returnTypeInferer, ClassAnalyzer $classAnalyzer, BetterNodeFinder $betterNodeFinder)
{
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
Expand Down Expand Up @@ -105,6 +109,7 @@ public function refactor(Node $node) : ?Node
if (!$toStringClassMethod instanceof ClassMethod) {
return null;
}
$this->hasChanged = \false;
// warning, classes that implements __toString() will return Stringable interface even if they don't implemen it
// reflection cannot be used for real detection
$classLikeAncestorNames = $this->familyRelationsAnalyzer->getClassLikeAncestorNames($node);
Expand All @@ -116,10 +121,15 @@ public function refactor(Node $node) : ?Node
if (!$isAncestorHasStringable) {
// add interface
$node->implements[] = new FullyQualified(self::STRINGABLE);
$this->hasChanged = \true;
}
// add return type
if ($toStringClassMethod->returnType === null) {
$toStringClassMethod->returnType = new Identifier('string');
$this->hasChanged = \true;
}
if (!$this->hasChanged) {
return null;
}
return $node;
}
Expand All @@ -132,6 +142,7 @@ private function processNotStringType(ClassMethod $toStringClassMethod) : void
if (!$hasReturn) {
$emptyStringReturn = new Return_(new String_(''));
$toStringClassMethod->stmts[] = $emptyStringReturn;
$this->hasChanged = \true;
return;
}
$this->traverseNodesWithCallable((array) $toStringClassMethod->stmts, function (Node $subNode) {
Expand All @@ -147,6 +158,7 @@ private function processNotStringType(ClassMethod $toStringClassMethod) : void
return null;
}
$subNode->expr = new CastString_($subNode->expr);
$this->hasChanged = \true;
return null;
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '319e9f0694be4026734c932a3b6b8c31debdb0f8';
public const PACKAGE_VERSION = '6aed089476ea0167835fbbdb42d876cddb4c77d4';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-10-05 07:57:38';
public const RELEASE_DATE = '2023-10-05 08:18:12';
/**
* @var int
*/
Expand Down

0 comments on commit 90920e0

Please sign in to comment.