Skip to content

Commit

Permalink
Try simpler name scope factory
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 30, 2024
1 parent 554cad9 commit bca8bf2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 71 deletions.
9 changes: 0 additions & 9 deletions src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
Expand Down Expand Up @@ -397,14 +396,6 @@ public function getTemplateNames(): array
return $templateNames;
}

/**
* @return TemplateTagValueNode[]
*/
public function getTemplateTagValueNodes(): array
{
return $this->phpDocNode->getTemplateTagValues();
}

public function makeMultiLined(): void
{
$this->isSingleLine = false;
Expand Down
64 changes: 3 additions & 61 deletions src/StaticTypeMapper/Naming/NameScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,25 @@
namespace Rector\StaticTypeMapper\Naming;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Analyser\NameScope;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\AstResolver;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;

/**
* @see https://github.com/phpstan/phpstan-src/blob/8376548f76e2c845ae047e3010e873015b796818/src/Analyser/NameScope.php#L32
*/
final class NameScopeFactory
{
private StaticTypeMapper $staticTypeMapper;

private PhpDocInfoFactory $phpDocInfoFactory;

public function __construct(
private readonly UseImportsResolver $useImportsResolver,
private readonly AstResolver $astResolver,
private readonly ReflectionResolver $reflectionResolver
) {
}

// This is needed to avoid circular references

public function autowire(PhpDocInfoFactory $phpDocInfoFactory, StaticTypeMapper $staticTypeMapper): void
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->staticTypeMapper = $staticTypeMapper;
}

public function createNameScopeFromNodeWithoutTemplateTypes(Node $node): NameScope
{
$scope = $node->getAttribute(AttributeKey::SCOPE);
Expand All @@ -66,7 +45,7 @@ public function createNameScopeFromNodeWithoutTemplateTypes(Node $node): NameSco
public function createNameScopeFromNode(Node $node): NameScope
{
$nameScope = $this->createNameScopeFromNodeWithoutTemplateTypes($node);
$templateTypeMap = $this->templateTemplateTypeMap($node);

/** @var non-empty-string|null $namespace */
$namespace = $nameScope->getNamespace();

Expand All @@ -75,7 +54,8 @@ public function createNameScopeFromNode(Node $node): NameScope
$nameScope->getUses(),
$nameScope->getClassName(),
null,
$templateTypeMap
null
// $templateTypeMap
);
}

Expand Down Expand Up @@ -103,42 +83,4 @@ private function resolveUseNamesByAlias(array $useNodes): array

return $useNamesByAlias;
}

private function templateTemplateTypeMap(Node $node): TemplateTypeMap
{
$nodeTemplateTypes = $this->resolveTemplateTypesFromNode($node);

$classTemplateTypes = [];

$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if ($classReflection instanceof ClassReflection) {
$classLike = $this->astResolver->resolveClassFromClassReflection($classReflection);

if ($classLike instanceof ClassLike) {
$classTemplateTypes = $this->resolveTemplateTypesFromNode($classLike);
}
}

$templateTypes = [...$nodeTemplateTypes, ...$classTemplateTypes];
return new TemplateTypeMap($templateTypes);
}

/**
* @return Type[]
*/
private function resolveTemplateTypesFromNode(Node $node): array
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

$templateTypes = [];

foreach ($phpDocInfo->getTemplateTagValueNodes() as $templateTagValueNode) {
$templateTypes[$templateTagValueNode->name] = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType(
$templateTagValueNode,
$node
);
}

return $templateTypes;
}
}
2 changes: 1 addition & 1 deletion tests/Issues/IssueConditionalType/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Fixture
/**
* @template TValue of array<array|scalar>|scalar
*
* @return (TValue is (bool | float | int | string) ? (array | bool | float | int | string) : array<(array | bool | float | int | string)>)
* @return (TValue is scalar ? array|scalar : array<array|scalar>)
*/
public function resolveValue()
{
Expand Down

0 comments on commit bca8bf2

Please sign in to comment.