Repository navigation
Expand file tree
/
Copy pathGenericTypeTemplateTraverser.php
More file actions
90 lines (79 loc) · 3.45 KB
/
Copy pathGenericTypeTemplateTraverser.php
File metadata and controls
90 lines (79 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\Traverser;
use PhpParser\Node\Expr;
use PHPStan\Analyser\Generics\TemplateArgumentFrame;
use PHPStan\Turbo\ReferencedByTurboExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\UnresolvedTemplateArgumentType;
use PHPStan\Type\NarrowedSubjectType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverserCallable;
/**
* Substitutes the class's template types in `new Foo(...)` with what the
* constructor arguments inferred for them.
*
* Without a template argument frame (feature toggle off, file top level, a walk
* started outside any body) the inferred argument is generalized as it always
* was (`new Foo(1)` is `Foo<int>`). Under a frame the inferred argument is
* kept exact and, during the body's observation pass, wrapped in an
* UnresolvedTemplateArgumentType keyed by the site so the body's sends and
* method calls can decide it; the second pass substitutes the frame's
* resolution. An inferred argument that already carries another site's marker
* passes through - the outer result then resolves the inner site. So does any
* inferred argument of a covariant template: nothing the body does with the
* object can narrow it (see
* ResolvedFunctionVariantWithOriginal::unresolvedOrResolvedTemplateArgument()).
*/
#[ReferencedByTurboExtension(key: 'genericTypeTemplateTraverser')]
final class GenericTypeTemplateTraverser implements TypeTraverserCallable
{
public function __construct(
private readonly TemplateTypeMap $resolvedTemplateTypeMap,
private readonly Expr $site,
private readonly ?TemplateArgumentFrame $frame,
private readonly bool $allowUnresolved,
)
{
}
/**
* @param callable(Type): Type $traverse
*/
public function traverse(Type $type, callable $traverse): Type
{
if ($type instanceof TemplateType && !$type instanceof NarrowedSubjectType && !$type->isArgument()) {
$newType = $this->resolvedTemplateTypeMap->getType($type->getName());
if ($this->frame === null) {
if ($newType === null || $newType instanceof ErrorType) {
return $type->getDefault() ?? $type->getBound();
}
return TemplateTypeHelper::generalizeInferredTemplateType($type, $newType);
}
$initialType = $newType === null || $newType instanceof ErrorType ? null : $newType;
// a synthetic site (the parent constructor's `new`) always hands out
// markers - the real site re-keys and resolves them
$synthetic = $this->site->getAttribute(TemplateArgumentFrame::SYNTHETIC_SITE_ATTRIBUTE) === true;
if ($synthetic || ($this->allowUnresolved && $this->frame->isObserving())) {
if (!$synthetic) {
// a send in a body enclosing this one decided the site
$resolved = $this->frame->resolveSentFromEnclosingBody($this->site, $type->getName());
if ($resolved !== null) {
return $resolved;
}
}
if ($initialType instanceof UnresolvedTemplateArgumentType) {
return $initialType;
}
if (!$synthetic && $initialType !== null && !$initialType instanceof NeverType && $type->getVariance()->covariant()) {
return $initialType;
}
return new UnresolvedTemplateArgumentType($this->site, $type, $initialType);
}
return $this->frame->resolve($this->site, $type->getName()) ?? $initialType ?? $this->frame->resolveOrUnconstrained($this->site, $type);
}
return $traverse($type);
}
}