Skip to content

Commit a592d08

Browse files
committed
Make static<…> called on a template type of a consistent class an application of that template type
Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
1 parent 146b56d commit a592d08

16 files changed

Lines changed: 570 additions & 87 deletions

‎src/Reflection/ConsistentTemplatesHelper.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection;
44

55
use PHPStan\DependencyInjection\AutowiredService;
6+
use PHPStan\Turbo\ReferencedByTurboExtension;
67
use function array_unshift;
78
use function array_values;
89

@@ -12,6 +13,7 @@
1213
* class of an object together with other type arguments is a type, like `static<U>`.
1314
*/
1415
#[AutowiredService]
16+
#[ReferencedByTurboExtension(key: 'consistentTemplatesHelper')]
1517
final class ConsistentTemplatesHelper
1618
{
1719

‎src/Reflection/ResolvedFunctionVariantWithOriginal.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\Generic\GenericObjectType;
1212
use PHPStan\Type\Generic\GenericStaticType;
1313
use PHPStan\Type\Generic\TemplateType;
14+
use PHPStan\Type\Generic\TemplateTypeApplication;
1415
use PHPStan\Type\Generic\TemplateTypeHelper;
1516
use PHPStan\Type\Generic\TemplateTypeMap;
1617
use PHPStan\Type\Generic\TemplateTypeVariance;
@@ -337,7 +338,8 @@ private function resolveResolvableTemplateTypes(Type $type, TemplateTypeVariance
337338
};
338339

339340
return TypeTraverser::map($type, function (Type $type, callable $traverse) use ($references, $objectCb): Type {
340-
if ($type instanceof GenericObjectType || $type instanceof GenericStaticType) {
341+
// TColl<…> holds its arguments like the object it will be
342+
if ($type instanceof GenericObjectType || $type instanceof GenericStaticType || $type instanceof TemplateTypeApplication) {
341343
return TypeTraverser::map($type, $objectCb);
342344
}
343345

‎src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Reflection\ResolvedMethodReflection;
1313
use PHPStan\Turbo\ShadowedByTurboExtension;
1414
use PHPStan\Type\Generic\GenericStaticType;
15+
use PHPStan\Type\Generic\TemplateTypeApplication;
1516
use PHPStan\Type\StaticType;
1617
use PHPStan\Type\ThisType;
1718
use PHPStan\Type\Type;
@@ -148,8 +149,13 @@ private function transformStaticType(Type $type): Type
148149
$calledOnTypeReflections = $this->calledOnType->getObjectClassReflections();
149150
if (count($calledOnTypeReflections) === 1) {
150151
$calledOnTypeReflection = $calledOnTypeReflections[0];
152+
$changed = $type->changeBaseClass($calledOnTypeReflection);
153+
$application = TemplateTypeApplication::ofCalledOnType($this->calledOnType, $changed);
154+
if ($application !== null) {
155+
return $application;
156+
}
151157

152-
return $traverse($type->changeBaseClass($calledOnTypeReflection)->getStaticObjectType());
158+
return $traverse($changed->getStaticObjectType());
153159
}
154160

155161
return $this->calledOnType;

‎src/Reflection/Type/CalledOnTypeUnresolvedPropertyPrototypeReflection.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Reflection\ResolvedPropertyReflection;
99
use PHPStan\Turbo\ShadowedByTurboExtension;
1010
use PHPStan\Type\Generic\GenericStaticType;
11+
use PHPStan\Type\Generic\TemplateTypeApplication;
1112
use PHPStan\Type\StaticType;
1213
use PHPStan\Type\Type;
1314
use PHPStan\Type\TypeTraverser;
@@ -90,7 +91,13 @@ private function transformStaticType(Type $type): Type
9091
if ($type instanceof GenericStaticType) {
9192
$fetchedOnTypeReflections = $this->fetchedOnType->getObjectClassReflections();
9293
if (count($fetchedOnTypeReflections) === 1) {
93-
return $traverse($type->changeBaseClass($fetchedOnTypeReflections[0])->getStaticObjectType());
94+
$changed = $type->changeBaseClass($fetchedOnTypeReflections[0]);
95+
$application = TemplateTypeApplication::ofCalledOnType($this->fetchedOnType, $changed);
96+
if ($application !== null) {
97+
return $application;
98+
}
99+
100+
return $traverse($changed->getStaticObjectType());
94101
}
95102

96103
return $this->fetchedOnType;

‎src/Type/Generic/TemplateTypeApplication.php‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
66
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
77
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8+
use PHPStan\Reflection\ClassMemberAccessAnswerer;
9+
use PHPStan\Reflection\ConsistentTemplatesHelper;
10+
use PHPStan\Reflection\ExtendedMethodReflection;
11+
use PHPStan\Reflection\ExtendedPropertyReflection;
12+
use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
13+
use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
814
use PHPStan\Turbo\ShadowedByTurboExtension;
915
use PHPStan\Type\AcceptsResult;
1016
use PHPStan\Type\CompoundType;
1117
use PHPStan\Type\IsSuperTypeOfResult;
1218
use PHPStan\Type\LateResolvableType;
19+
use PHPStan\Type\StaticType;
1320
use PHPStan\Type\Traits\LateResolvableTypeTrait;
1421
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
1522
use PHPStan\Type\Type;
@@ -48,6 +55,35 @@ public function __construct(
4855
{
4956
}
5057

58+
/**
59+
* `static<U>` called on a TColl, or on a TColl<…> that is not known yet, is TColl<U>
60+
* when the class has `@phpstan-consistent-templates`.
61+
*/
62+
public static function ofCalledOnType(Type $calledOnType, StaticType $static): ?self
63+
{
64+
if (!$static instanceof GenericStaticType) {
65+
return null;
66+
}
67+
68+
if ($calledOnType instanceof self) {
69+
if ($calledOnType->isResolvable()) {
70+
return null;
71+
}
72+
73+
return new self($calledOnType->constructor, $static, true);
74+
}
75+
76+
if (!$calledOnType instanceof TemplateType) {
77+
return null;
78+
}
79+
80+
if ((new ConsistentTemplatesHelper())->findConsistentTemplatesClass($static->getClassReflection()) === null) {
81+
return null;
82+
}
83+
84+
return new self($calledOnType, $static, true);
85+
}
86+
5187
public function isConsistent(): bool
5288
{
5389
return $this->consistent;
@@ -161,6 +197,68 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
161197
->union($this->application->getStaticObjectType()->inferTemplateTypes($receivedType));
162198
}
163199

200+
// the methods and properties of the class TColl will be, where `static` is that class
201+
202+
public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection
203+
{
204+
return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
205+
}
206+
207+
public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
208+
{
209+
return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty();
210+
}
211+
212+
public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
213+
{
214+
return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getTransformedProperty();
215+
}
216+
217+
public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
218+
{
219+
return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty();
220+
}
221+
222+
public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection
223+
{
224+
$prototype = $this->resolve()->getUnresolvedMethodPrototype($methodName, $scope);
225+
if ($this->isResolvable()) {
226+
return $prototype;
227+
}
228+
229+
return $prototype->withCalledOnType($this);
230+
}
231+
232+
public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
233+
{
234+
$prototype = $this->resolve()->getUnresolvedPropertyPrototype($propertyName, $scope);
235+
if ($this->isResolvable()) {
236+
return $prototype;
237+
}
238+
239+
return $prototype->withFechedOnType($this);
240+
}
241+
242+
public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
243+
{
244+
$prototype = $this->resolve()->getUnresolvedInstancePropertyPrototype($propertyName, $scope);
245+
if ($this->isResolvable()) {
246+
return $prototype;
247+
}
248+
249+
return $prototype->withFechedOnType($this);
250+
}
251+
252+
public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
253+
{
254+
$prototype = $this->resolve()->getUnresolvedStaticPropertyPrototype($propertyName, $scope);
255+
if ($this->isResolvable()) {
256+
return $prototype;
257+
}
258+
259+
return $prototype->withFechedOnType($this);
260+
}
261+
164262
/** @phpstan-assert-if-true self $type */
165263
private function isSameUnresolvedClass(Type $type): bool
166264
{
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TemplateTypeApplicationStatic;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @template-covariant T
9+
* @phpstan-consistent-templates
10+
*/
11+
interface Coll
12+
{
13+
14+
/**
15+
* @template U
16+
* @param callable(T): U $f
17+
* @return static<U>
18+
*/
19+
public function map(callable $f);
20+
21+
/** @return static */
22+
public function filter();
23+
24+
}
25+
26+
/**
27+
* @template-covariant T
28+
* @implements Coll<T>
29+
*/
30+
final class Vec implements Coll
31+
{
32+
33+
/** @var static<string> */
34+
public $strings;
35+
36+
public function map(callable $f)
37+
{
38+
throw new \Exception();
39+
}
40+
41+
public function filter()
42+
{
43+
return $this;
44+
}
45+
46+
}
47+
48+
/** @template-covariant T */
49+
interface Loose
50+
{
51+
52+
/**
53+
* @template U
54+
* @param callable(T): U $f
55+
* @return static<U>
56+
*/
57+
public function map(callable $f);
58+
59+
}
60+
61+
/**
62+
* @template TColl of Coll
63+
* @param TColl<int> $c
64+
* @return TColl<string>
65+
*/
66+
function stringify(Coll $c): Coll
67+
{
68+
assertType('TColl<decimal-int-string>', $c->map(static fn (int $i): string => (string) $i));
69+
assertType('TColl<int>', $c->filter());
70+
71+
return $c->map(static fn (int $i): string => (string) $i);
72+
}
73+
74+
/**
75+
* @template TColl of Coll<int>
76+
* @param TColl $c
77+
* @return TColl
78+
*/
79+
function plain(Coll $c): Coll
80+
{
81+
assertType('TColl<decimal-int-string>', $c->map(static fn (int $i): string => (string) $i));
82+
assertType('TColl of TemplateTypeApplicationStatic\Coll<int> (function TemplateTypeApplicationStatic\plain(), argument)', $c->filter());
83+
84+
return $c->filter();
85+
}
86+
87+
/**
88+
* @template TVec of Vec
89+
* @param TVec<int> $vec
90+
*/
91+
function property(Vec $vec): void
92+
{
93+
assertType('TVec<string>', $vec->strings);
94+
}
95+
96+
/**
97+
* @template TLoose of Loose<int>
98+
* @param TLoose $c
99+
*/
100+
function loose(Loose $c): void
101+
{
102+
// without @phpstan-consistent-templates the class is not known to hold strings
103+
assertType('TemplateTypeApplicationStatic\Loose<decimal-int-string>', $c->map(static fn (int $i): string => (string) $i));
104+
}
105+
106+
/**
107+
* @param Vec<int> $vec
108+
*/
109+
function test(Vec $vec): void
110+
{
111+
assertType('TemplateTypeApplicationStatic\Vec<string>', stringify($vec));
112+
assertType('TemplateTypeApplicationStatic\Vec<int>', plain($vec));
113+
assertType('TemplateTypeApplicationStatic\Vec<decimal-int-string>', $vec->map(static fn (int $i): string => (string) $i));
114+
}

‎tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ public function testTemplateTypeApplication(): void
690690
'Function TemplateTypeApplicationRules\\same() should return TColl<string> but returns TColl<int>.',
691691
36,
692692
],
693+
[
694+
'Function TemplateTypeApplicationRules\\mappedWrongly() should return TMappable<int> but returns TMappable<string>.',
695+
96,
696+
],
693697
]);
694698
}
695699

‎tests/PHPStan/Rules/Functions/data/template-type-application.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,65 @@ function test(Vec $strings, Vec $ints): void
5959
ints($strings);
6060
ints(new \stdClass());
6161
}
62+
63+
/**
64+
* @template-covariant T
65+
* @phpstan-consistent-templates
66+
*/
67+
interface Mappable
68+
{
69+
70+
/**
71+
* @template U
72+
* @param callable(T): U $f
73+
* @return static<U>
74+
*/
75+
public function map(callable $f);
76+
77+
}
78+
79+
/**
80+
* @template TMappable of Mappable
81+
* @param TMappable<int> $m
82+
* @return TMappable<string>
83+
*/
84+
function mapped(Mappable $m): Mappable
85+
{
86+
return $m->map(static fn (int $i): string => (string) $i);
87+
}
88+
89+
/**
90+
* @template TMappable of Mappable
91+
* @param TMappable<int> $m
92+
* @return TMappable<int>
93+
*/
94+
function mappedWrongly(Mappable $m): Mappable
95+
{
96+
return $m->map(static fn (int $i): string => (string) $i);
97+
}
98+
99+
/**
100+
* @template T
101+
* @phpstan-consistent-templates
102+
*/
103+
interface InvariantMappable
104+
{
105+
106+
/**
107+
* @template U
108+
* @param callable(T): U $f
109+
* @return static<U>
110+
*/
111+
public function map(callable $f);
112+
113+
}
114+
115+
/**
116+
* @template TMappable of InvariantMappable
117+
* @param TMappable<int> $m
118+
* @return TMappable<string>
119+
*/
120+
function mappedInvariant(InvariantMappable $m): InvariantMappable
121+
{
122+
return $m->map(static fn (int $i): string => (string) $i);
123+
}

0 commit comments

Comments
 (0)