diff --git a/rules/TypeDeclaration/PHPStan/Type/ObjectTypeSpecifier.php b/rules/TypeDeclaration/PHPStan/Type/ObjectTypeSpecifier.php index 1d675d8f985..25fe61dc050 100644 --- a/rules/TypeDeclaration/PHPStan/Type/ObjectTypeSpecifier.php +++ b/rules/TypeDeclaration/PHPStan/Type/ObjectTypeSpecifier.php @@ -43,11 +43,6 @@ public function narrowToFullyQualifiedOrAliasedObjectType( ObjectType $objectType, Scope|null $scope ): TypeWithClassName | NonExistingObjectType | UnionType | MixedType { - $sameNamespacedFullyQualifiedObjectType = $this->matchSameNamespacedObjectType($node, $objectType); - if ($sameNamespacedFullyQualifiedObjectType !== null) { - return $sameNamespacedFullyQualifiedObjectType; - } - if ($scope instanceof Scope) { foreach ($this->typeWithClassTypeSpecifiers as $typeWithClassTypeSpecifier) { if ($typeWithClassTypeSpecifier->match($objectType, $scope)) { @@ -194,27 +189,6 @@ private function matchShortenedObjectType( return null; } - private function matchSameNamespacedObjectType(Node $node, ObjectType $objectType): ?FullyQualifiedObjectType - { - $scope = $node->getAttribute(AttributeKey::SCOPE); - if (! $scope instanceof Scope) { - return null; - } - - $namespaceName = $scope->getNamespace(); - if ($namespaceName === null) { - return null; - } - - $namespacedObject = $namespaceName . '\\' . ltrim($objectType->getClassName(), '\\'); - - if ($this->reflectionProvider->hasClass($namespacedObject)) { - return new FullyQualifiedObjectType($namespacedObject); - } - - return null; - } - private function matchPartialNamespaceObjectType(ObjectType $objectType, UseUse $useUse): ?ShortenedObjectType { // partial namespace diff --git a/rules/TypeDeclaration/PHPStan/TypeSpecifier/SameNamespacedTypeSpecifier.php b/rules/TypeDeclaration/PHPStan/TypeSpecifier/SameNamespacedTypeSpecifier.php new file mode 100644 index 00000000000..d9cbb871338 --- /dev/null +++ b/rules/TypeDeclaration/PHPStan/TypeSpecifier/SameNamespacedTypeSpecifier.php @@ -0,0 +1,37 @@ +getNamespace(); + if ($namespaceName === null) { + return false; + } + + $namespacedClassName = $namespaceName . '\\' . ltrim($objectType->getClassName(), '\\'); + return $this->reflectionProvider->hasClass($namespacedClassName); + } + + public function resolveObjectReferenceType(ObjectType $objectType, Scope $scope): TypeWithClassName + { + $namespacedClassName = $scope->getNamespace() . '\\' . ltrim($objectType->getClassName(), '\\'); + return new FullyQualifiedObjectType($namespacedClassName); + } +}