99use PHPStan \Type \Type ;
1010use PHPStan \Type \TypeCombinator ;
1111use PHPStan \Type \TypeTraverser ;
12+ use PHPStan \Type \UnionType ;
13+ use function array_filter ;
1214use function array_key_exists ;
1315use function array_keys ;
16+ use function array_merge ;
17+ use function array_values ;
1418use function count ;
1519use function spl_object_id ;
1620
@@ -32,18 +36,103 @@ public function __construct(
3236 /** @return array<string, Type> */
3337 public function solve (): array
3438 {
39+ $ this ->mergeEqualArguments ();
3540 foreach (array_keys ($ this ->observations ) as $ key ) {
3641 $ this ->resolveKey ($ key );
3742 }
43+ foreach ($ this ->representatives as $ key => $ representative ) {
44+ $ this ->resolutions [$ key ] = $ this ->resolveKey ($ representative );
45+ }
3846
3947 return $ this ->resolutions ;
4048 }
4149
50+ /** @var array<string, string> */
51+ private array $ representatives = [];
52+
53+ private function representative (string $ key ): string
54+ {
55+ $ representative = $ this ->representatives [$ key ] ?? $ key ;
56+ if ($ representative === $ key ) {
57+ return $ key ;
58+ }
59+ return $ this ->representatives [$ key ] = $ this ->representative ($ representative );
60+ }
61+
62+ /** Invariant arguments share one variable until all their bounds are known. */
63+ private function mergeEqualArguments (): void
64+ {
65+ $ ranks = [];
66+ foreach ($ this ->observations as $ key => $ observation ) {
67+ foreach ($ observation ['sends ' ] as [$ sent , $ variance ]) {
68+ if (!$ variance ->invariant () || !$ sent instanceof UnresolvedTemplateArgumentType) {
69+ continue ;
70+ }
71+ $ other = self ::key ($ sent ->getSite (), $ sent ->getTemplateName ());
72+ if (!isset ($ this ->observations [$ other ])) {
73+ continue ;
74+ }
75+ $ left = $ this ->representative ($ key );
76+ $ right = $ this ->representative ($ other );
77+ if ($ left === $ right ) {
78+ continue ;
79+ }
80+ $ leftRank = $ ranks [$ left ] ?? 0 ;
81+ $ rightRank = $ ranks [$ right ] ?? 0 ;
82+ if ($ leftRank < $ rightRank ) {
83+ $ this ->representatives [$ left ] = $ right ;
84+ } else {
85+ $ this ->representatives [$ right ] = $ left ;
86+ if ($ leftRank === $ rightRank ) {
87+ $ ranks [$ left ] = $ leftRank + 1 ;
88+ }
89+ }
90+ }
91+ }
92+ if ($ this ->representatives === []) {
93+ return ;
94+ }
95+ $ observations = [];
96+ foreach ($ this ->observations as $ key => $ observation ) {
97+ $ representative = $ this ->representative ($ key );
98+ $ this ->representatives [$ key ] = $ representative ;
99+ $ initial = $ observation ['initial ' ];
100+ $ observation ['initial ' ] = $ initial !== null ? $ this ->removeSelfBounds ($ initial , $ representative ) : null ;
101+ $ observation ['sends ' ] = array_values (array_filter ($ observation ['sends ' ], fn (array $ send ): bool => !$ send [0 ] instanceof UnresolvedTemplateArgumentType
102+ || $ this ->representative (self ::key ($ send [0 ]->getSite (), $ send [0 ]->getTemplateName ())) !== $ representative ));
103+ if (!isset ($ observations [$ representative ])) {
104+ $ observations [$ representative ] = $ observation ;
105+ continue ;
106+ }
107+ $ merged = $ observations [$ representative ];
108+ if ($ observation ['initial ' ] !== null ) {
109+ $ merged ['initial ' ] = $ merged ['initial ' ] !== null ? TypeCombinator ::union ($ merged ['initial ' ], $ observation ['initial ' ]) : $ observation ['initial ' ];
110+ }
111+ $ merged ['sends ' ] = array_merge ($ merged ['sends ' ], $ observation ['sends ' ]);
112+ $ merged ['lowerBounds ' ] = array_merge ($ merged ['lowerBounds ' ], $ observation ['lowerBounds ' ]);
113+ $ merged ['unconstrainingSend ' ] = $ merged ['unconstrainingSend ' ] || $ observation ['unconstrainingSend ' ];
114+ $ observations [$ representative ] = $ merged ;
115+ }
116+ $ this ->observations = $ observations ;
117+ }
118+
119+ private function removeSelfBounds (Type $ type , string $ key ): Type
120+ {
121+ if ($ type instanceof UnresolvedTemplateArgumentType && $ this ->representative (self ::key ($ type ->getSite (), $ type ->getTemplateName ())) === $ key ) {
122+ return new NeverType ();
123+ }
124+ if ($ type instanceof UnionType) {
125+ return $ type ->traverse (fn (Type $ member ): Type => $ this ->removeSelfBounds ($ member , $ key ));
126+ }
127+ return $ type ;
128+ }
129+
42130 /** @var array<string, true> */
43131 private array $ resolving = [];
44132
45133 private function resolveKey (string $ key ): Type
46134 {
135+ $ key = $ this ->representative ($ key );
47136 if (array_key_exists ($ key , $ this ->resolutions )) {
48137 return $ this ->resolutions [$ key ];
49138 }
@@ -86,7 +175,7 @@ private function substituteResolutions(Type $type): Type
86175
87176 private function substituteMarker (UnresolvedTemplateArgumentType $ marker ): Type
88177 {
89- $ key = self ::key ($ marker ->getSite (), $ marker ->getTemplateName ());
178+ $ key = $ this -> representative ( self ::key ($ marker ->getSite (), $ marker ->getTemplateName () ));
90179 if (array_key_exists ($ key , $ this ->observations )) {
91180 return $ this ->resolveKey ($ key );
92181 }
@@ -126,6 +215,7 @@ private function resolveObservation(array $observation): Type
126215 if (!$ templateVariance ->covariant () || $ acceptsAnything ) {
127216 $ covariantFallback = null ;
128217 foreach ($ observation ['sends ' ] as [$ sent , $ variance ]) {
218+ $ sent = $ this ->substituteResolutions ($ sent );
129219 if ($ variance ->contravariant ()) {
130220 // Foo<contravariant int> accepts Foo<X> for every X wider than int
131221 $ lowerBounds [] = $ sent ;
0 commit comments