55use PHPStan \Type \Traits \UndecidedComparisonCompoundTypeTrait ;
66use PHPStan \Type \Type ;
77
8+ /**
9+ * @method GenericObjectType getBound()
10+ */
811final class TemplateGenericObjectType extends GenericObjectType implements TemplateType
912{
1013
1114 use UndecidedComparisonCompoundTypeTrait;
1215 use TemplateTypeTrait;
1316
14- /**
15- * @param Type[] $types
16- */
1717 public function __construct (
1818 TemplateTypeScope $ scope ,
1919 TemplateTypeStrategy $ templateTypeStrategy ,
2020 TemplateTypeVariance $ templateTypeVariance ,
2121 string $ name ,
22- string $ mainType ,
23- array $ types
22+ GenericObjectType $ bound
2423 )
2524 {
26- parent ::__construct ($ mainType , $ types );
25+ parent ::__construct ($ bound -> getClassName () , $ bound -> getTypes () );
2726
2827 $ this ->scope = $ scope ;
2928 $ this ->strategy = $ templateTypeStrategy ;
3029 $ this ->variance = $ templateTypeVariance ;
3130 $ this ->name = $ name ;
32- $ this ->bound = new GenericObjectType ( $ mainType , $ types ) ;
31+ $ this ->bound = $ bound ;
3332 }
3433
3534 public function toArgument (): TemplateType
@@ -39,20 +38,34 @@ public function toArgument(): TemplateType
3938 new TemplateTypeArgumentStrategy (),
4039 $ this ->variance ,
4140 $ this ->name ,
42- $ this ->getClassName (),
43- $ this ->getTypes ()
41+ TemplateTypeHelper::toArgument ($ this ->getBound ())
4442 );
4543 }
4644
45+ public function traverse (callable $ cb ): Type
46+ {
47+ $ newBound = $ cb ($ this ->getBound ());
48+ if ($ this ->getBound () !== $ newBound && $ newBound instanceof GenericObjectType) {
49+ return new self (
50+ $ this ->scope ,
51+ $ this ->strategy ,
52+ $ this ->variance ,
53+ $ this ->name ,
54+ $ newBound
55+ );
56+ }
57+
58+ return $ this ;
59+ }
60+
4761 protected function recreate (string $ className , array $ types , ?Type $ subtractedType ): GenericObjectType
4862 {
4963 return new self (
5064 $ this ->scope ,
5165 $ this ->strategy ,
5266 $ this ->variance ,
5367 $ this ->name ,
54- $ className ,
55- $ types
68+ $ this ->getBound ()
5669 );
5770 }
5871
@@ -67,8 +80,7 @@ public static function __set_state(array $properties): Type
6780 $ properties ['strategy ' ],
6881 $ properties ['variance ' ],
6982 $ properties ['name ' ],
70- $ properties ['className ' ],
71- $ properties ['types ' ]
83+ $ properties ['bound ' ]
7284 );
7385 }
7486
0 commit comments