Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MyNamespace;

abstract class AbstractMigration {}

abstract class BaseMigration extends AbstractMigration {}

class MyMigration extends AbstractMigration {}

?>
-----
<?php

namespace MyNamespace;

abstract class AbstractMigration {}

abstract class BaseMigration extends AbstractMigration {}

class MyMigration extends \MyNamespace\BaseMigration {}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,13 @@
// test skip rename class const fetch when target rename is interface
// and interface class const fetch not found
'Symfony\Component\Serializer\Normalizer\ObjectNormalizer' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface',

/**
* // avoid error extends itself
* namespace MyNamespace;
*
* class MyMigration extends \MyNamespace\BaseMigration {}
*/
'MyNamespace\AbstractMigration' => 'MyNamespace\BaseMigration',
]);
};
28 changes: 21 additions & 7 deletions rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope): ?

// execute FullyQualified before Name on purpose so next Name check is pure Name node
if ($node instanceof FullyQualified) {
return $this->refactorName($node, $oldToNewClasses);
return $this->refactorName($node, $oldToNewClasses, $scope);
}

// Name as parent of FullyQualified executed for fallback annotation to attribute rename to Name
if ($node instanceof Name) {
$phpAttributeName = $node->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
if (is_string($phpAttributeName)) {
return $this->refactorName(new FullyQualified($phpAttributeName), $oldToNewClasses);
return $this->refactorName(new FullyQualified($phpAttributeName), $oldToNewClasses, $scope);
}

return null;
Expand Down Expand Up @@ -133,8 +133,11 @@ private function shouldSkip(string $newName, FullyQualified $fullyQualified): bo
/**
* @param array<string, string> $oldToNewClasses
*/
private function refactorName(FullyQualified $fullyQualified, array $oldToNewClasses): ?FullyQualified
{
private function refactorName(
FullyQualified $fullyQualified,
array $oldToNewClasses,
?Scope $scope
): ?FullyQualified {
if ($fullyQualified->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === true) {
return null;
}
Expand All @@ -146,7 +149,7 @@ private function refactorName(FullyQualified $fullyQualified, array $oldToNewCla
return null;
}

if (! $this->isClassToInterfaceValidChange($fullyQualified, $newName)) {
if (! $this->isClassToInterfaceValidChange($fullyQualified, $newName, $scope)) {
return null;
}

Expand Down Expand Up @@ -202,15 +205,26 @@ private function refactorClassLike(ClassLike $classLike, array $oldToNewClasses,
* - implements SomeInterface
* - implements SomeClass
*/
private function isClassToInterfaceValidChange(FullyQualified $fullyQualified, string $newClassName): bool
{
private function isClassToInterfaceValidChange(
FullyQualified $fullyQualified,
string $newClassName,
?Scope $scope
): bool {
if (! $this->reflectionProvider->hasClass($newClassName)) {
return true;
}

$classReflection = $this->reflectionProvider->getClass($newClassName);
// ensure new is not with interface
if ($fullyQualified->getAttribute(AttributeKey::IS_NEW_INSTANCE_NAME) !== true) {
if ($fullyQualified->getAttribute(AttributeKey::IS_CLASS_EXTENDS) === true && $scope instanceof Scope) {
$currentClassReflection = $scope->getClassReflection();

if ($currentClassReflection instanceof ClassReflection && $currentClassReflection->getName() === $newClassName) {
return false;
}
}

return $this->isValidClassNameChange($fullyQualified, $classReflection);
}

Expand Down
11 changes: 11 additions & 0 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -168,6 +169,16 @@ public function processNodes(
$mutatingScope = $this->resolveClassOrInterfaceScope($node, $mutatingScope);
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);

if ($node instanceof Class_) {
if ($node->extends instanceof FullyQualified) {
$node->extends->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

foreach ($node->implements as $implement) {
$implement->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}

return;
}

Expand Down
Loading