Skip to content

Commit

Permalink
Make use of Class_, Interface_ (#4193)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 12, 2023
1 parent 21f8014 commit ca08d86
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions rules/Transform/Rector/ClassMethod/ReturnTypeWillChangeRector.php
Expand Up @@ -6,8 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -37,8 +35,10 @@ public function __construct(
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
private readonly ReflectionResolver $reflectionResolver
) {
$this->returnTypeChangedClassMethodReferences[] = new ClassMethodReference('ArrayAccess', 'getIterator');
$this->returnTypeChangedClassMethodReferences[] = new ClassMethodReference('ArrayAccess', 'offsetGet');
$this->returnTypeChangedClassMethodReferences = [
new ClassMethodReference('ArrayAccess', 'getIterator'),
new ClassMethodReference('ArrayAccess', 'offsetGet'),
];
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -78,56 +78,50 @@ public function offsetGet($offset)
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [Class_::class, Interface_::class];
}

/**
* @param ClassMethod $node
* @param Class_|Interface_ $node
*/
public function refactor(Node $node): ?Node
{
if ($this->phpAttributeAnalyzer->hasPhpAttribute($node, AttributeName::RETURN_TYPE_WILL_CHANGE)) {
return null;
}

// the return type is known, no need to add attribute
if ($node->returnType !== null) {
return null;
}

$classLike = $this->betterNodeFinder->findParentByTypes($node, [Class_::class, Interface_::class]);
if (! $classLike instanceof ClassLike) {
return null;
}

$classReflection = $this->reflectionResolver->resolveClassAndAnonymousClass($classLike);
$methodName = $node->name->toString();

$hasChanged = false;
$classReflection = $this->reflectionResolver->resolveClassAndAnonymousClass($node);

foreach ($this->returnTypeChangedClassMethodReferences as $returnTypeChangedClassMethodReference) {
if (! $classReflection->isSubclassOf($returnTypeChangedClassMethodReference->getClass())) {
foreach ($node->getMethods() as $classMethod) {
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, AttributeName::RETURN_TYPE_WILL_CHANGE)) {
continue;
}

if ($returnTypeChangedClassMethodReference->getMethod() !== $methodName) {
// the return type is known, no need to add attribute
if ($classMethod->returnType !== null) {
continue;
}

$node->attrGroups[] = $this->phpAttributeGroupFactory->createFromClass(
AttributeName::RETURN_TYPE_WILL_CHANGE
);
foreach ($this->returnTypeChangedClassMethodReferences as $returnTypeChangedClassMethodReference) {
if (! $classReflection->isSubclassOf($returnTypeChangedClassMethodReference->getClass())) {
continue;
}

$hasChanged = true;
if (! $this->isName($classMethod, $returnTypeChangedClassMethodReference->getMethod())) {
continue;
}

break;
$classMethod->attrGroups[] = $this->phpAttributeGroupFactory->createFromClass(
AttributeName::RETURN_TYPE_WILL_CHANGE
);

$hasChanged = true;
break;
}
}

if (! $hasChanged) {
return null;
if ($hasChanged) {
return $node;
}

return $node;
return null;
}

/**
Expand Down

0 comments on commit ca08d86

Please sign in to comment.