Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FamilyTree] Remove AstResolver on FamilyRelationsAnalyzer #5117

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;

Expand All @@ -19,7 +18,6 @@ public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly PrivatesAccessor $privatesAccessor,
private readonly NodeNameResolver $nodeNameResolver,
private readonly AstResolver $astResolver,
) {
}

Expand Down Expand Up @@ -58,25 +56,29 @@ public function getClassLikeAncestorNames(Class_ | Interface_ | Name $classOrNam

if ($classOrName instanceof Name) {
$fullName = $this->nodeNameResolver->getName($classOrName);
$classLike = $this->astResolver->resolveClassFromName($fullName);
} else {
$classLike = $classOrName;
$classReflection = $this->reflectionProvider->getClass($fullName);
$ancestors = array_merge($classReflection->getParents(), $classReflection->getInterfaces());

return array_map(
static fn (ClassReflection $classReflection): string => $classReflection->getName(),
$ancestors
);
}

if ($classLike instanceof Interface_) {
foreach ($classLike->extends as $extendInterfaceName) {
if ($classOrName instanceof Interface_) {
foreach ($classOrName->extends as $extendInterfaceName) {
$ancestorNames[] = $this->nodeNameResolver->getName($extendInterfaceName);
$ancestorNames = array_merge($ancestorNames, $this->getClassLikeAncestorNames($extendInterfaceName));
}
}

if ($classLike instanceof Class_) {
if ($classLike->extends instanceof Name) {
$ancestorNames[] = $this->nodeNameResolver->getName($classLike->extends);
$ancestorNames = array_merge($ancestorNames, $this->getClassLikeAncestorNames($classLike->extends));
if ($classOrName instanceof Class_) {
if ($classOrName->extends instanceof Name) {
$ancestorNames[] = $this->nodeNameResolver->getName($classOrName->extends);
$ancestorNames = array_merge($ancestorNames, $this->getClassLikeAncestorNames($classOrName->extends));
}

foreach ($classLike->implements as $implement) {
foreach ($classOrName->implements as $implement) {
$ancestorNames[] = $this->nodeNameResolver->getName($implement);
$ancestorNames = array_merge($ancestorNames, $this->getClassLikeAncestorNames($implement));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\Fixture;

use Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\Source\ParentInterfaceWithStringable;

final class ExtendsWithInterface implements ParentInterfaceWithStringable
{
public function __toString()
{
return 'I can stringz';
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\Fixture;

use Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\Source\ParentInterfaceWithStringable;

final class ExtendsWithInterface implements ParentInterfaceWithStringable
{
public function __toString(): string
{
return 'I can stringz';
}
}
Comment on lines +23 to +29
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when parent interface already extends Stringable, it add return type: string, without re-adding Stringable into implements 👍

see https://3v4l.org/vgKoK


?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\Fixture;

class ParentChildReturnString
{
public function __toString()
{
return 'I can stringz';
}
}

class SomeChildReturnParentChildReturnString extends ParentChildReturnString
{
public function __toString()
{
return 'I can stringz';
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\Fixture;

class ParentChildReturnString implements \Stringable
{
public function __toString(): string
{
return 'I can stringz';
}
}

class SomeChildReturnParentChildReturnString extends ParentChildReturnString
{
public function __toString(): string
{
return 'I can stringz';
}
}

?>

This file was deleted.

11 changes: 5 additions & 6 deletions rules/Php80/Rector/Class_/StringableForToStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,17 @@ public function refactor(Node $node): ?Node
// 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);

if (in_array(self::STRINGABLE, $classLikeAncestorNames, true)) {
return null;
}
$isAncestorHasStringable = in_array(self::STRINGABLE, $classLikeAncestorNames, true);

$returnType = $this->returnTypeInferer->inferFunctionLike($toStringClassMethod);
if (! $returnType->isString()->yes()) {
$this->processNotStringType($toStringClassMethod);
}

// add interface
$node->implements[] = new FullyQualified(self::STRINGABLE);
if (! $isAncestorHasStringable) {
// add interface
$node->implements[] = new FullyQualified(self::STRINGABLE);
}

// add return type
if ($toStringClassMethod->returnType === null) {
Expand Down
3 changes: 3 additions & 0 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function __construct(
) {
}

/**
* @api downgrade
*/
public function resolveClassFromName(string $className): Class_ | Trait_ | Interface_ | Enum_ | null
{
if (! $this->reflectionProvider->hasClass($className)) {
Expand Down