1212use PHPStan \Rules \RuleErrorBuilder ;
1313use PHPStan \Rules \RuleLevelHelper ;
1414use PHPStan \ShouldNotHappenException ;
15- use PHPStan \Type \BenevolentUnionType ;
15+ use PHPStan \Type \ArrayType ;
1616use PHPStan \Type \ErrorType ;
1717use PHPStan \Type \FloatType ;
1818use PHPStan \Type \IntegerType ;
19+ use PHPStan \Type \MixedType ;
20+ use PHPStan \Type \NullType ;
1921use PHPStan \Type \ObjectWithoutClassType ;
2022use PHPStan \Type \Type ;
21- use PHPStan \Type \TypeCombinator ;
2223use PHPStan \Type \UnionType ;
2324use PHPStan \Type \VerbosityLevel ;
2425use function get_class ;
@@ -59,7 +60,9 @@ public function processNode(Node $node, Scope $scope): array
5960 return [];
6061 }
6162
62- if ($ this ->isNumberType ($ scope , $ node ->left ) && $ this ->isNumberType ($ scope , $ node ->right )) {
63+ $ isLeftNumberType = $ this ->isNumberType ($ scope , $ node ->left );
64+ $ isRightNumberType = $ this ->isNumberType ($ scope , $ node ->right );
65+ if ($ isLeftNumberType === $ isRightNumberType ) {
6366 return [];
6467 }
6568
@@ -80,10 +83,10 @@ public function processNode(Node $node, Scope $scope): array
8083 }
8184
8285 if (
83- ($ this -> isNumberType ( $ scope , $ node -> left ) && (
86+ ($ isLeftNumberType && (
8487 $ this ->isPossiblyNullableObjectType ($ scope , $ node ->right ) || $ this ->isPossiblyNullableArrayType ($ scope , $ node ->right )
8588 ))
86- || ($ this -> isNumberType ( $ scope , $ node -> right ) && (
89+ || ($ isRightNumberType && (
8790 $ this ->isPossiblyNullableObjectType ($ scope , $ node ->left ) || $ this ->isPossiblyNullableArrayType ($ scope , $ node ->left )
8891 ))
8992 ) {
@@ -113,45 +116,18 @@ private function isNumberType(Scope $scope, Node\Expr $expr): bool
113116
114117 private function isPossiblyNullableObjectType (Scope $ scope , Node \Expr $ expr ): bool
115118 {
116- $ acceptedType = new ObjectWithoutClassType ();
119+ $ type = $ scope ->getType ($ expr );
120+ $ acceptedType = new UnionType ([new ObjectWithoutClassType (), new NullType ()]);
117121
118- $ type = $ this ->ruleLevelHelper ->findTypeToCheck (
119- $ scope ,
120- $ expr ,
121- '' ,
122- static fn (Type $ type ): bool => $ acceptedType ->isSuperTypeOf ($ type )->yes (),
123- )->getType ();
124-
125- if ($ type instanceof ErrorType) {
126- return false ;
127- }
128-
129- if (TypeCombinator::containsNull ($ type ) && !$ type ->isNull ()->yes ()) {
130- $ type = TypeCombinator::removeNull ($ type );
131- }
132-
133- $ isSuperType = $ acceptedType ->isSuperTypeOf ($ type );
134- if ($ type instanceof BenevolentUnionType) {
135- return !$ isSuperType ->no ();
136- }
137-
138- return $ isSuperType ->yes ();
122+ return !$ type ->isNull ()->yes () && $ acceptedType ->isSuperTypeOf ($ type )->yes ();
139123 }
140124
141125 private function isPossiblyNullableArrayType (Scope $ scope , Node \Expr $ expr ): bool
142126 {
143- $ type = $ this ->ruleLevelHelper ->findTypeToCheck (
144- $ scope ,
145- $ expr ,
146- '' ,
147- static fn (Type $ type ): bool => $ type ->isArray ()->yes (),
148- )->getType ();
149-
150- if (TypeCombinator::containsNull ($ type ) && !$ type ->isNull ()->yes ()) {
151- $ type = TypeCombinator::removeNull ($ type );
152- }
127+ $ type = $ scope ->getType ($ expr );
128+ $ acceptedType = new UnionType ([new ArrayType (new MixedType (), new MixedType ()), new NullType ()]);
153129
154- return !( $ type instanceof ErrorType) && $ type -> isArray ( )->yes ();
130+ return !$ type-> isNull ()-> yes () && $ acceptedType -> isSuperTypeOf ( $ type )->yes ();
155131 }
156132
157133 /** @return list<IdentifierRuleError> */
0 commit comments