33namespace PHPStan \Type \Generic ;
44
55use PHPStan \TrinaryLogic ;
6- use PHPStan \Type \CompoundType ;
7- use PHPStan \Type \IntersectionType ;
86use PHPStan \Type \ObjectType ;
97use PHPStan \Type \ObjectWithoutClassType ;
108use PHPStan \Type \Traits \UndecidedComparisonCompoundTypeTrait ;
119use PHPStan \Type \Type ;
12- use PHPStan \Type \UnionType ;
13- use PHPStan \Type \VerbosityLevel ;
1410
1511final class TemplateObjectType extends ObjectType implements TemplateType
1612{
1713
1814 use UndecidedComparisonCompoundTypeTrait;
19-
20- private TemplateTypeScope $ scope ;
21-
22- private string $ name ;
23-
24- private TemplateTypeStrategy $ strategy ;
25-
26- private ObjectType $ bound ;
27-
28- private TemplateTypeVariance $ variance ;
15+ use TemplateTypeTrait {
16+ isSubTypeOf as isSubTypeOfTrait;
17+ }
2918
3019 public function __construct (
3120 TemplateTypeScope $ scope ,
@@ -44,43 +33,13 @@ public function __construct(
4433 $ this ->bound = new ObjectType ($ class );
4534 }
4635
47- public function getName (): string
48- {
49- return $ this ->name ;
50- }
51-
52- public function getScope (): TemplateTypeScope
53- {
54- return $ this ->scope ;
55- }
56-
57- public function getBound (): Type
58- {
59- return $ this ->bound ;
60- }
61-
62- public function describe (VerbosityLevel $ level ): string
36+ public function isSubTypeOf (Type $ type ): TrinaryLogic
6337 {
64- $ basicDescription = function () use ($ level ): string {
65- return sprintf (
66- '%s of %s ' ,
67- $ this ->name ,
68- parent ::describe ($ level )
69- );
70- };
71-
72- return $ level ->handle (
73- $ basicDescription ,
74- $ basicDescription ,
75- function () use ($ basicDescription ): string {
76- return sprintf ('%s (%s, %s) ' , $ basicDescription (), $ this ->scope ->describe (), $ this ->isArgument () ? 'argument ' : 'parameter ' );
77- }
78- );
79- }
38+ if ($ type instanceof ObjectWithoutClassType) {
39+ return TrinaryLogic::createYes ();
40+ }
8041
81- public function isArgument (): bool
82- {
83- return $ this ->strategy ->isArgument ();
42+ return $ this ->isSubTypeOfTrait ($ type );
8443 }
8544
8645 public function toArgument (): TemplateType
@@ -94,114 +53,6 @@ public function toArgument(): TemplateType
9453 );
9554 }
9655
97- public function isValidVariance (Type $ a , Type $ b ): TrinaryLogic
98- {
99- return $ this ->variance ->isValidVariance ($ a , $ b );
100- }
101-
102- public function subtract (Type $ type ): Type
103- {
104- return $ this ;
105- }
106-
107- public function getTypeWithoutSubtractedType (): Type
108- {
109- return $ this ;
110- }
111-
112- public function changeSubtractedType (?Type $ subtractedType ): Type
113- {
114- return $ this ;
115- }
116-
117- public function equals (Type $ type ): bool
118- {
119- return $ type instanceof self
120- && $ type ->scope ->equals ($ this ->scope )
121- && $ type ->name === $ this ->name
122- && parent ::equals ($ type );
123- }
124-
125- public function isAcceptedBy (Type $ acceptingType , bool $ strictTypes ): TrinaryLogic
126- {
127- return $ this ->isSubTypeOf ($ acceptingType );
128- }
129-
130- public function accepts (Type $ type , bool $ strictTypes ): TrinaryLogic
131- {
132- return $ this ->strategy ->accepts ($ this , $ type , $ strictTypes );
133- }
134-
135- public function isSuperTypeOf (Type $ type ): TrinaryLogic
136- {
137- if ($ type instanceof CompoundType) {
138- return $ type ->isSubTypeOf ($ this );
139- }
140-
141- return $ this ->getBound ()->isSuperTypeOf ($ type )
142- ->and (TrinaryLogic::createMaybe ());
143- }
144-
145- public function isSubTypeOf (Type $ type ): TrinaryLogic
146- {
147- if ($ type instanceof UnionType || $ type instanceof IntersectionType) {
148- return $ type ->isSuperTypeOf ($ this );
149- }
150-
151- if ($ type instanceof ObjectWithoutClassType) {
152- return TrinaryLogic::createYes ();
153- }
154-
155- if (!$ type instanceof TemplateType) {
156- return $ type ->isSuperTypeOf ($ this ->getBound ());
157- }
158-
159- if ($ this ->equals ($ type )) {
160- return TrinaryLogic::createYes ();
161- }
162-
163- if ($ type ->getBound ()->isSuperTypeOf ($ this ->getBound ())->no () &&
164- $ this ->getBound ()->isSuperTypeOf ($ type ->getBound ())->no ()) {
165- return TrinaryLogic::createNo ();
166- }
167-
168- return TrinaryLogic::createMaybe ();
169- }
170-
171- public function inferTemplateTypes (Type $ receivedType ): TemplateTypeMap
172- {
173- if ($ receivedType instanceof UnionType || $ receivedType instanceof IntersectionType) {
174- return $ receivedType ->inferTemplateTypesOn ($ this );
175- }
176-
177- if (
178- $ receivedType instanceof TemplateType
179- && $ this ->getBound ()->isSuperTypeOf ($ receivedType ->getBound ())->yes ()
180- ) {
181- return new TemplateTypeMap ([
182- $ this ->name => $ receivedType ,
183- ]);
184- }
185-
186- if ($ this ->getBound ()->isSuperTypeOf ($ receivedType )->yes ()) {
187- return new TemplateTypeMap ([
188- $ this ->name => TemplateTypeHelper::generalizeType ($ receivedType ),
189- ]);
190- }
191-
192- return TemplateTypeMap::createEmpty ();
193- }
194-
195- public function getReferencedTemplateTypes (TemplateTypeVariance $ positionVariance ): array
196- {
197- return [new TemplateTypeReference ($ this , $ positionVariance )];
198- }
199-
200- public function getVariance (): TemplateTypeVariance
201- {
202- return $ this ->variance ;
203- }
204-
20556 /**
20657 * @param mixed[] $properties
20758 * @return Type
0 commit comments