Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SameNamespacedTypeSpecifier (#2169)
* narrow to FQN * add ObjectTypeSpecifier * require Scope * add SameNamespacedTypeSpecifier
- Loading branch information
1 parent
148dda7
commit 43aa4d9
Showing
2 changed files
with
37 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
rules/TypeDeclaration/PHPStan/TypeSpecifier/SameNamespacedTypeSpecifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |