99use PHPStan \Type \ErrorType ;
1010use PHPStan \Type \FloatType ;
1111use PHPStan \Type \IntegerType ;
12+ use PHPStan \Type \NullType ;
1213use PHPStan \Type \ObjectWithoutClassType ;
1314use PHPStan \Type \Type ;
15+ use PHPStan \Type \TypeCombinator ;
1416use PHPStan \Type \UnionType ;
1517use PHPStan \Type \VerbosityLevel ;
1618
@@ -48,10 +50,10 @@ public function processNode(Node $node, Scope $scope): array
4850
4951 if (
5052 ($ this ->isNumberType ($ scope , $ node ->left ) && (
51- $ this ->isObjectType ($ scope , $ node ->right ) || $ this ->isArrayType ($ scope , $ node ->right )
53+ $ this ->isPossiblyNullableObjectType ($ scope , $ node ->right ) || $ this ->isPossiblyNullableArrayType ($ scope , $ node ->right )
5254 ))
5355 || ($ this ->isNumberType ($ scope , $ node ->right ) && (
54- $ this ->isObjectType ($ scope , $ node ->left ) || $ this ->isArrayType ($ scope , $ node ->left )
56+ $ this ->isPossiblyNullableObjectType ($ scope , $ node ->left ) || $ this ->isPossiblyNullableArrayType ($ scope , $ node ->left )
5557 ))
5658 ) {
5759 return [
@@ -86,7 +88,7 @@ private function isNumberType(Scope $scope, Node\Expr $expr): bool
8688 return !$ acceptedType ->isSuperTypeOf ($ type )->no ();
8789 }
8890
89- private function isObjectType (Scope $ scope , Node \Expr $ expr ): bool
91+ private function isPossiblyNullableObjectType (Scope $ scope , Node \Expr $ expr ): bool
9092 {
9193 $ acceptedType = new ObjectWithoutClassType ();
9294
@@ -103,6 +105,10 @@ static function (Type $type) use ($acceptedType): bool {
103105 return false ;
104106 }
105107
108+ if (TypeCombinator::containsNull ($ type ) && !$ type instanceof NullType) {
109+ $ type = TypeCombinator::removeNull ($ type );
110+ }
111+
106112 $ isSuperType = $ acceptedType ->isSuperTypeOf ($ type );
107113 if ($ type instanceof \PHPStan \Type \BenevolentUnionType) {
108114 return !$ isSuperType ->no ();
@@ -111,7 +117,7 @@ static function (Type $type) use ($acceptedType): bool {
111117 return $ isSuperType ->yes ();
112118 }
113119
114- private function isArrayType (Scope $ scope , Node \Expr $ expr ): bool
120+ private function isPossiblyNullableArrayType (Scope $ scope , Node \Expr $ expr ): bool
115121 {
116122 $ type = $ this ->ruleLevelHelper ->findTypeToCheck (
117123 $ scope ,
@@ -122,6 +128,10 @@ static function (Type $type): bool {
122128 }
123129 )->getType ();
124130
131+ if (TypeCombinator::containsNull ($ type ) && !$ type instanceof NullType) {
132+ $ type = TypeCombinator::removeNull ($ type );
133+ }
134+
125135 return !($ type instanceof ErrorType) && $ type ->isArray ()->yes ();
126136 }
127137
0 commit comments