12
12
use PHPStan \Rules \RuleErrorBuilder ;
13
13
use PHPStan \Rules \RuleLevelHelper ;
14
14
use PHPStan \ShouldNotHappenException ;
15
- use PHPStan \Type \BenevolentUnionType ;
15
+ use PHPStan \Type \ArrayType ;
16
16
use PHPStan \Type \ErrorType ;
17
17
use PHPStan \Type \FloatType ;
18
18
use PHPStan \Type \IntegerType ;
19
+ use PHPStan \Type \MixedType ;
20
+ use PHPStan \Type \NullType ;
19
21
use PHPStan \Type \ObjectWithoutClassType ;
20
22
use PHPStan \Type \Type ;
21
- use PHPStan \Type \TypeCombinator ;
22
23
use PHPStan \Type \UnionType ;
23
24
use PHPStan \Type \VerbosityLevel ;
24
25
use function get_class ;
@@ -59,7 +60,9 @@ public function processNode(Node $node, Scope $scope): array
59
60
return [];
60
61
}
61
62
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 ) || (!$ isLeftNumberType && !$ isRightNumberType )) {
63
66
return [];
64
67
}
65
68
@@ -80,10 +83,10 @@ public function processNode(Node $node, Scope $scope): array
80
83
}
81
84
82
85
if (
83
- ($ this -> isNumberType ( $ scope , $ node -> left ) && (
86
+ ($ isLeftNumberType && (
84
87
$ this ->isPossiblyNullableObjectType ($ scope , $ node ->right ) || $ this ->isPossiblyNullableArrayType ($ scope , $ node ->right )
85
88
))
86
- || ($ this -> isNumberType ( $ scope , $ node -> right ) && (
89
+ || ($ isRightNumberType && (
87
90
$ this ->isPossiblyNullableObjectType ($ scope , $ node ->left ) || $ this ->isPossiblyNullableArrayType ($ scope , $ node ->left )
88
91
))
89
92
) {
@@ -113,45 +116,18 @@ private function isNumberType(Scope $scope, Node\Expr $expr): bool
113
116
114
117
private function isPossiblyNullableObjectType (Scope $ scope , Node \Expr $ expr ): bool
115
118
{
116
- $ acceptedType = new ObjectWithoutClassType ();
119
+ $ type = $ scope ->getType ($ expr );
120
+ $ acceptedType = new UnionType ([new ObjectWithoutClassType (), new NullType ()]);
117
121
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 ();
139
123
}
140
124
141
125
private function isPossiblyNullableArrayType (Scope $ scope , Node \Expr $ expr ): bool
142
126
{
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 ()]);
153
129
154
- return !( $ type instanceof ErrorType) && $ type -> isArray ( )->yes ();
130
+ return !$ type-> isNull ()-> yes () && $ acceptedType -> isSuperTypeOf ( $ type )->yes ();
155
131
}
156
132
157
133
/** @return list<IdentifierRuleError> */
0 commit comments