Skip to content

Commit

Permalink
Add SameNamespacedTypeSpecifier (#2169)
Browse files Browse the repository at this point in the history
* narrow to FQN

* add ObjectTypeSpecifier

* require Scope

* add SameNamespacedTypeSpecifier
  • Loading branch information
TomasVotruba committed Apr 26, 2022
1 parent 148dda7 commit 43aa4d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
26 changes: 0 additions & 26 deletions rules/TypeDeclaration/PHPStan/Type/ObjectTypeSpecifier.php
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\PHPStan\TypeSpecifier;

use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\TypeDeclaration\Contract\PHPStan\TypeWithClassTypeSpecifierInterface;

final class SameNamespacedTypeSpecifier implements TypeWithClassTypeSpecifierInterface
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
) {
}

public function match(ObjectType $objectType, Scope $scope): bool
{
$namespaceName = $scope->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);
}
}

0 comments on commit 43aa4d9

Please sign in to comment.