Skip to content

Commit 73cf02b

Browse files
committed
[PropertyInfo] Replace NameScope by TypeContext
1 parent 6298bfa commit 73cf02b

File tree

4 files changed

+20
-155
lines changed

4 files changed

+20
-155
lines changed

src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
use PHPStan\PhpDocParser\Parser\TokenIterator;
2525
use PHPStan\PhpDocParser\Parser\TypeParser;
2626
use PHPStan\PhpDocParser\ParserConfig;
27-
use Symfony\Component\PropertyInfo\PhpStan\NameScope;
28-
use Symfony\Component\PropertyInfo\PhpStan\NameScopeFactory;
2927
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
3028
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
3129
use Symfony\Component\PropertyInfo\Type as LegacyType;
3230
use Symfony\Component\PropertyInfo\Util\PhpStanTypeHelper;
3331
use Symfony\Component\TypeInfo\Exception\UnsupportedException;
3432
use Symfony\Component\TypeInfo\Type;
33+
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
3534
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
3635
use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver;
3736

@@ -48,7 +47,6 @@ final class PhpStanExtractor implements PropertyDescriptionExtractorInterface, P
4847

4948
private PhpDocParser $phpDocParser;
5049
private Lexer $lexer;
51-
private NameScopeFactory $nameScopeFactory;
5250

5351
private StringTypeResolver $stringTypeResolver;
5452
private TypeContextFactory $typeContextFactory;
@@ -60,7 +58,7 @@ final class PhpStanExtractor implements PropertyDescriptionExtractorInterface, P
6058
private array $accessorPrefixes;
6159
private array $arrayMutatorPrefixes;
6260

63-
/** @var array<string, NameScope> */
61+
/** @var array<string, TypeContext> */
6462
private array $contexts = [];
6563

6664
/**
@@ -91,7 +89,6 @@ public function __construct(?array $mutatorPrefixes = null, ?array $accessorPref
9189
$this->phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser());
9290
$this->lexer = new Lexer();
9391
}
94-
$this->nameScopeFactory = new NameScopeFactory();
9592
$this->stringTypeResolver = new StringTypeResolver();
9693
$this->typeContextFactory = new TypeContextFactory($this->stringTypeResolver);
9794
}
@@ -133,8 +130,9 @@ public function getTypes(string $class, string $property, array $context = []):
133130
continue;
134131
}
135132

136-
$nameScope ??= $this->contexts[$class.'/'.$declaringClass] ??= $this->nameScopeFactory->create($class, $declaringClass);
137-
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value, $nameScope) as $type) {
133+
$typeContext = $this->contexts[$class.'/'.$declaringClass] ??= $this->typeContextFactory->createFromClassName($class, $declaringClass);
134+
135+
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value, $typeContext) as $type) {
138136
switch ($type->getClassName()) {
139137
case 'self':
140138
case 'static':
@@ -177,7 +175,7 @@ public function getTypesFromConstructor(string $class, string $property): ?array
177175
}
178176

179177
$types = [];
180-
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode, $this->nameScopeFactory->create($class)) as $type) {
178+
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode, $this->typeContextFactory->createFromClassName($class)) as $type) {
181179
$types[] = $type;
182180
}
183181

src/Symfony/Component/PropertyInfo/PhpStan/NameScope.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Symfony/Component/PropertyInfo/PhpStan/NameScopeFactory.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/Symfony/Component/PropertyInfo/Util/PhpStanTypeHelper.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
2727
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
2828
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
29-
use Symfony\Component\PropertyInfo\PhpStan\NameScope;
3029
use Symfony\Component\PropertyInfo\Type;
30+
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
3131

3232
/**
3333
* Transforms a php doc tag value to a {@link Type} instance.
@@ -43,10 +43,10 @@ final class PhpStanTypeHelper
4343
*
4444
* @return Type[]
4545
*/
46-
public function getTypes(PhpDocTagValueNode $node, NameScope $nameScope): array
46+
public function getTypes(PhpDocTagValueNode $node, TypeContext $typeContext): array
4747
{
4848
if ($node instanceof ParamTagValueNode || $node instanceof ReturnTagValueNode || $node instanceof VarTagValueNode) {
49-
return $this->compressNullableType($this->extractTypes($node->type, $nameScope));
49+
return $this->compressNullableType($this->extractTypes($node->type, $typeContext));
5050
}
5151

5252
return [];
@@ -98,7 +98,7 @@ private function compressNullableType(array $types): array
9898
/**
9999
* @return Type[]
100100
*/
101-
private function extractTypes(TypeNode $node, NameScope $nameScope): array
101+
private function extractTypes(TypeNode $node, TypeContext $typeContext): array
102102
{
103103
if ($node instanceof UnionTypeNode) {
104104
$types = [];
@@ -107,7 +107,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
107107
// It's safer to fall back to other extractors here, as resolving const types correctly is not easy at the moment
108108
return [];
109109
}
110-
foreach ($this->extractTypes($type, $nameScope) as $subType) {
110+
foreach ($this->extractTypes($type, $typeContext) as $subType) {
111111
$types[] = $subType;
112112
}
113113
}
@@ -119,7 +119,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
119119
return [new Type(Type::BUILTIN_TYPE_STRING)];
120120
}
121121

122-
[$mainType] = $this->extractTypes($node->type, $nameScope);
122+
[$mainType] = $this->extractTypes($node->type, $typeContext);
123123

124124
if (Type::BUILTIN_TYPE_INT === $mainType->getBuiltinType()) {
125125
return [$mainType];
@@ -135,14 +135,14 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
135135
$collectionKeyTypes = $mainType->getCollectionKeyTypes();
136136
$collectionKeyValues = [];
137137
if (1 === \count($node->genericTypes)) {
138-
foreach ($this->extractTypes($node->genericTypes[0], $nameScope) as $subType) {
138+
foreach ($this->extractTypes($node->genericTypes[0], $typeContext) as $subType) {
139139
$collectionKeyValues[] = $subType;
140140
}
141141
} elseif (2 === \count($node->genericTypes)) {
142-
foreach ($this->extractTypes($node->genericTypes[0], $nameScope) as $keySubType) {
142+
foreach ($this->extractTypes($node->genericTypes[0], $typeContext) as $keySubType) {
143143
$collectionKeyTypes[] = $keySubType;
144144
}
145-
foreach ($this->extractTypes($node->genericTypes[1], $nameScope) as $valueSubType) {
145+
foreach ($this->extractTypes($node->genericTypes[1], $typeContext) as $valueSubType) {
146146
$collectionKeyValues[] = $valueSubType;
147147
}
148148
}
@@ -153,13 +153,13 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
153153
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)];
154154
}
155155
if ($node instanceof ArrayTypeNode) {
156-
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [new Type(Type::BUILTIN_TYPE_INT)], $this->extractTypes($node->type, $nameScope))];
156+
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, [new Type(Type::BUILTIN_TYPE_INT)], $this->extractTypes($node->type, $typeContext))];
157157
}
158158
if ($node instanceof CallableTypeNode || $node instanceof CallableTypeParameterNode) {
159159
return [new Type(Type::BUILTIN_TYPE_CALLABLE)];
160160
}
161161
if ($node instanceof NullableTypeNode) {
162-
$subTypes = $this->extractTypes($node->type, $nameScope);
162+
$subTypes = $this->extractTypes($node->type, $typeContext);
163163
if (\count($subTypes) > 1) {
164164
$subTypes[] = new Type(Type::BUILTIN_TYPE_NULL);
165165

@@ -169,7 +169,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
169169
return [new Type($subTypes[0]->getBuiltinType(), true, $subTypes[0]->getClassName(), $subTypes[0]->isCollection(), $subTypes[0]->getCollectionKeyTypes(), $subTypes[0]->getCollectionValueTypes())];
170170
}
171171
if ($node instanceof ThisTypeNode) {
172-
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $nameScope->resolveRootClass())];
172+
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $typeContext->getCalledClass())];
173173
}
174174
if ($node instanceof IdentifierTypeNode) {
175175
if (\in_array($node->name, Type::$builtinTypes, true)) {
@@ -190,7 +190,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
190190
'mixed' => [], // mixed seems to be ignored in all other extractors
191191
'parent' => [new Type(Type::BUILTIN_TYPE_OBJECT, false, $node->name)],
192192
'static',
193-
'self' => [new Type(Type::BUILTIN_TYPE_OBJECT, false, $nameScope->resolveRootClass())],
193+
'self' => [new Type(Type::BUILTIN_TYPE_OBJECT, false, $typeContext->getCalledClass())],
194194
'class-string',
195195
'html-escaped-string',
196196
'lowercase-string',
@@ -205,7 +205,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
205205
'number' => [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT)],
206206
'numeric' => [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT), new Type(Type::BUILTIN_TYPE_STRING)],
207207
'array-key' => [new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_INT)],
208-
default => [new Type(Type::BUILTIN_TYPE_OBJECT, false, $nameScope->resolveStringName($node->name))],
208+
default => [new Type(Type::BUILTIN_TYPE_OBJECT, false, $typeContext->normalize($node->name))],
209209
};
210210
}
211211

0 commit comments

Comments
 (0)