@@ -25,32 +25,17 @@ class StaticType implements TypeWithClassName
2525 use NonGenericTypeTrait;
2626 use UndecidedComparisonTypeTrait;
2727
28- private ? ClassReflection $ classReflection ;
28+ private ClassReflection $ classReflection ;
2929
3030 private ?\PHPStan \Type \ObjectType $ staticObjectType = null ;
3131
3232 private string $ baseClass ;
3333
3434 /**
3535 * @api
36- * @param string|ClassReflection $classReflection
3736 */
38- public function __construct ($ classReflection )
39- {
40- if (is_string ($ classReflection )) {
41- $ broker = Broker::getInstance ();
42- if ($ broker ->hasClass ($ classReflection )) {
43- $ classReflection = $ broker ->getClass ($ classReflection );
44- $ this ->classReflection = $ classReflection ;
45- $ this ->baseClass = $ classReflection ->getName ();
46- return ;
47- }
48-
49- $ this ->classReflection = null ;
50- $ this ->baseClass = $ classReflection ;
51- return ;
52- }
53-
37+ public function __construct (ClassReflection $ classReflection )
38+ {
5439 $ this ->classReflection = $ classReflection ;
5540 $ this ->baseClass = $ classReflection ->getName ();
5641 }
@@ -72,13 +57,18 @@ public function getAncestorWithClassName(string $className): ?TypeWithClassName
7257 return null ;
7358 }
7459
75- return $ this ->changeBaseClass ($ ancestor ->getClassReflection () ?? $ ancestor ->getClassName ());
60+ $ classReflection = $ ancestor ->getClassReflection ();
61+ if ($ classReflection !== null ) {
62+ return $ this ->changeBaseClass ($ classReflection );
63+ }
64+
65+ return null ;
7666 }
7767
7868 public function getStaticObjectType (): ObjectType
7969 {
8070 if ($ this ->staticObjectType === null ) {
81- if ($ this ->classReflection !== null && $ this -> classReflection ->isGeneric ()) {
71+ if ($ this ->classReflection ->isGeneric ()) {
8272 $ typeMap = $ this ->classReflection ->getActiveTemplateTypeMap ()->map (static function (string $ name , Type $ type ): Type {
8373 return TemplateTypeHelper::toArgument ($ type );
8474 });
@@ -88,7 +78,7 @@ public function getStaticObjectType(): ObjectType
8878 );
8979 }
9080
91- return $ this ->staticObjectType = new ObjectType ($ this ->baseClass , null , $ this ->classReflection );
81+ return $ this ->staticObjectType = new ObjectType ($ this ->classReflection -> getName () , null , $ this ->classReflection );
9282 }
9383
9484 return $ this ->staticObjectType ;
@@ -102,11 +92,6 @@ public function getReferencedClasses(): array
10292 return $ this ->getStaticObjectType ()->getReferencedClasses ();
10393 }
10494
105- public function getBaseClass (): string
106- {
107- return $ this ->baseClass ;
108- }
109-
11095 public function accepts (Type $ type , bool $ strictTypes ): TrinaryLogic
11196 {
11297 if ($ type instanceof CompoundType) {
@@ -247,9 +232,7 @@ private function transformStaticType(Type $type, ClassMemberAccessAnswerer $scop
247232 if ($ type instanceof StaticType) {
248233 $ classReflection = $ this ->classReflection ;
249234 $ isFinal = false ;
250- if ($ classReflection === null ) {
251- $ classReflection = $ this ->baseClass ;
252- } elseif ($ scope ->isInClass ()) {
235+ if ($ scope ->isInClass ()) {
253236 $ classReflection = $ scope ->getClassReflection ();
254237 $ isFinal = $ classReflection ->isFinal ();
255238 }
@@ -280,11 +263,7 @@ public function getConstant(string $constantName): ConstantReflection
280263 return $ this ->getStaticObjectType ()->getConstant ($ constantName );
281264 }
282265
283- /**
284- * @param ClassReflection|string $classReflection
285- * @return self
286- */
287- public function changeBaseClass ($ classReflection ): self
266+ public function changeBaseClass (ClassReflection $ classReflection ): self
288267 {
289268 return new self ($ classReflection );
290269 }
@@ -409,7 +388,12 @@ public function traverse(callable $cb): Type
409388 */
410389 public static function __set_state (array $ properties ): Type
411390 {
412- return new self ($ properties ['baseClass ' ]);
391+ $ broker = Broker::getInstance ();
392+ if ($ broker ->hasClass ($ properties ['baseClass ' ])) {
393+ return new self ($ broker ->getClass ($ properties ['baseClass ' ]));
394+ }
395+
396+ return new ErrorType ();
413397 }
414398
415399}
0 commit comments