2020-04-24 12:00:49 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php80\Rector\Class_;
|
2020-04-24 12:00:49 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Cast\String_ as CastString_;
|
2023-02-13 14:43:04 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\ClassAnalyzer;
|
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\ValueObject\MethodName;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-04-24 12:00:49 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/stringable
|
2020-04-24 12:00:49 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\StringableForToStringRectorTest
|
2020-04-24 12:00:49 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class StringableForToStringRector extends AbstractRector implements MinPhpVersionInterface
|
2020-04-24 12:00:49 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-07-29 16:36:00 +00:00
|
|
|
* @var \Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer
|
2020-04-24 12:00:49 +00:00
|
|
|
*/
|
2021-07-29 16:36:00 +00:00
|
|
|
private $familyRelationsAnalyzer;
|
2021-12-13 12:42:56 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-12-13 12:42:56 +00:00
|
|
|
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
|
|
|
*/
|
|
|
|
private $returnTypeInferer;
|
2022-04-18 17:44:13 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\ClassAnalyzer
|
2022-04-18 17:44:13 +00:00
|
|
|
*/
|
|
|
|
private $classAnalyzer;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const STRINGABLE = 'Stringable';
|
2023-10-05 08:21:59 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasChanged = \false;
|
2023-09-20 12:55:35 +00:00
|
|
|
public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer, ReturnTypeInferer $returnTypeInferer, ClassAnalyzer $classAnalyzer, BetterNodeFinder $betterNodeFinder)
|
2020-04-24 12:00:49 +00:00
|
|
|
{
|
2021-07-29 16:36:00 +00:00
|
|
|
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
|
2021-12-13 12:42:56 +00:00
|
|
|
$this->returnTypeInferer = $returnTypeInferer;
|
2022-04-18 17:44:13 +00:00
|
|
|
$this->classAnalyzer = $classAnalyzer;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2020-04-24 12:00:49 +00:00
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::STRINGABLE;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-04-24 12:00:49 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add `Stringable` interface to classes with `__toString()` method', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-04-24 12:00:49 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return 'I can stringz';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-04-24 12:00:49 +00:00
|
|
|
class SomeClass implements Stringable
|
|
|
|
{
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return 'I can stringz';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-04-24 12:00:49 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-04-24 12:00:49 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-04-24 12:00:49 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2020-04-24 12:00:49 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2020-04-24 12:00:49 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-04-24 12:00:49 +00:00
|
|
|
{
|
2023-05-30 12:30:41 +00:00
|
|
|
if ($this->classAnalyzer->isAnonymousClass($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$toStringClassMethod = $node->getMethod(MethodName::TO_STRING);
|
|
|
|
if (!$toStringClassMethod instanceof ClassMethod) {
|
2020-04-24 12:00:49 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-10-05 08:21:59 +00:00
|
|
|
$this->hasChanged = \false;
|
2021-07-29 16:36:00 +00:00
|
|
|
// 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);
|
2023-10-05 07:33:50 +00:00
|
|
|
$isAncestorHasStringable = \in_array(self::STRINGABLE, $classLikeAncestorNames, \true);
|
2021-12-13 12:42:56 +00:00
|
|
|
$returnType = $this->returnTypeInferer->inferFunctionLike($toStringClassMethod);
|
2022-11-15 00:01:27 +00:00
|
|
|
if (!$returnType->isString()->yes()) {
|
2021-12-13 12:42:56 +00:00
|
|
|
$this->processNotStringType($toStringClassMethod);
|
|
|
|
}
|
2023-10-05 07:33:50 +00:00
|
|
|
if (!$isAncestorHasStringable) {
|
|
|
|
// add interface
|
|
|
|
$node->implements[] = new FullyQualified(self::STRINGABLE);
|
2023-10-05 08:21:59 +00:00
|
|
|
$this->hasChanged = \true;
|
2023-10-05 07:33:50 +00:00
|
|
|
}
|
2020-04-24 12:00:49 +00:00
|
|
|
// add return type
|
|
|
|
if ($toStringClassMethod->returnType === null) {
|
2023-02-13 14:43:04 +00:00
|
|
|
$toStringClassMethod->returnType = new Identifier('string');
|
2023-10-05 08:21:59 +00:00
|
|
|
$this->hasChanged = \true;
|
|
|
|
}
|
|
|
|
if (!$this->hasChanged) {
|
|
|
|
return null;
|
2020-04-24 12:00:49 +00:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processNotStringType(ClassMethod $toStringClassMethod) : void
|
2021-12-13 12:42:56 +00:00
|
|
|
{
|
2021-12-14 08:15:01 +00:00
|
|
|
if ($toStringClassMethod->isAbstract()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$hasReturn = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($toStringClassMethod, Return_::class);
|
2021-12-13 12:42:56 +00:00
|
|
|
if (!$hasReturn) {
|
2023-05-30 12:30:41 +00:00
|
|
|
$emptyStringReturn = new Return_(new String_(''));
|
|
|
|
$toStringClassMethod->stmts[] = $emptyStringReturn;
|
2023-10-05 08:21:59 +00:00
|
|
|
$this->hasChanged = \true;
|
2021-12-13 12:42:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable((array) $toStringClassMethod->stmts, function (Node $subNode) {
|
|
|
|
if (!$subNode instanceof Return_) {
|
2022-02-19 18:54:29 +00:00
|
|
|
return null;
|
2021-12-13 12:42:56 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$subNode->expr instanceof Expr) {
|
|
|
|
$subNode->expr = new String_('');
|
2022-02-19 18:54:29 +00:00
|
|
|
return null;
|
2021-12-13 12:42:56 +00:00
|
|
|
}
|
|
|
|
$type = $this->nodeTypeResolver->getType($subNode->expr);
|
2022-11-15 00:01:27 +00:00
|
|
|
if ($type->isString()->yes()) {
|
2022-02-19 18:54:29 +00:00
|
|
|
return null;
|
2021-12-13 12:42:56 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$subNode->expr = new CastString_($subNode->expr);
|
2023-10-05 08:21:59 +00:00
|
|
|
$this->hasChanged = \true;
|
2022-02-19 18:54:29 +00:00
|
|
|
return null;
|
2021-12-13 12:42:56 +00:00
|
|
|
});
|
|
|
|
}
|
2020-04-24 12:00:49 +00:00
|
|
|
}
|