Skip to content

Commit ea98962

Browse files
committed
report invalid % operator usage
1 parent abab3ef commit ea98962

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,10 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
856856
return $this->getNeverType($leftType, $rightType);
857857
}
858858

859+
if ($leftType->toNumber() instanceof ErrorType || $rightType->toNumber() instanceof ErrorType) {
860+
return new ErrorType();
861+
}
862+
859863
$leftTypes = $leftType->getConstantScalarTypes();
860864
$rightTypes = $rightType->getConstantScalarTypes();
861865
$leftTypesCount = count($leftTypes);

tests/PHPStan/Rules/Operators/InvalidBinaryOperationRuleTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ public function testRule(): void
251251
'Binary operation "+" between int and array{} results in an error.',
252252
259,
253253
],
254+
[
255+
'Binary operation "%" between array and 3 results in an error.',
256+
267,
257+
],
258+
[
259+
'Binary operation "%" between 3 and array results in an error.',
260+
268,
261+
],
262+
[
263+
'Binary operation "%" between object and 3 results in an error.',
264+
270,
265+
],
266+
[
267+
'Binary operation "%" between 3 and object results in an error.',
268+
271,
269+
],
254270
]);
255271
}
256272

@@ -313,7 +329,10 @@ public function testBinaryMixed(): void
313329
'Binary operation "/" between T and 2 results in an error.',
314330
17,
315331
],
316-
// % is not handled yet
332+
[
333+
'Binary operation "%" between T and 2 results in an error.',
334+
18,
335+
],
317336
[
318337
'Binary operation "+" between T and 2 results in an error.',
319338
19,
@@ -358,7 +377,10 @@ public function testBinaryMixed(): void
358377
'Binary operation "/=" between 5 and T results in an error.',
359378
38,
360379
],
361-
// %= is not handled yet
380+
[
381+
'Binary operation "%=" between 5 and T results in an error.',
382+
41,
383+
],
362384
[
363385
'Binary operation "&=" between 5 and T results in an error.',
364386
44,
@@ -399,6 +421,10 @@ public function testBinaryMixed(): void
399421
'Binary operation "/" between mixed and 2 results in an error.',
400422
67,
401423
],
424+
[
425+
'Binary operation "%" between mixed and 2 results in an error.',
426+
68,
427+
],
402428
[
403429
'Binary operation "+" between mixed and 2 results in an error.',
404430
69,
@@ -443,6 +469,10 @@ public function testBinaryMixed(): void
443469
'Binary operation "/=" between 5 and mixed results in an error.',
444470
88,
445471
],
472+
[
473+
'Binary operation "%=" between 5 and mixed results in an error.',
474+
91,
475+
],
446476
[
447477
'Binary operation "&=" between 5 and mixed results in an error.',
448478
94,
@@ -483,6 +513,10 @@ public function testBinaryMixed(): void
483513
'Binary operation "/" between mixed and 2 results in an error.',
484514
117,
485515
],
516+
[
517+
'Binary operation "%" between mixed and 2 results in an error.',
518+
118,
519+
],
486520
[
487521
'Binary operation "+" between mixed and 2 results in an error.',
488522
119,
@@ -527,6 +561,10 @@ public function testBinaryMixed(): void
527561
'Binary operation "/=" between 5 and mixed results in an error.',
528562
138,
529563
],
564+
[
565+
'Binary operation "%=" between 5 and mixed results in an error.',
566+
141,
567+
],
530568
[
531569
'Binary operation "&=" between 5 and mixed results in an error.',
532570
144,

tests/PHPStan/Rules/Operators/data/invalid-binary.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,15 @@ function benevolentPlus(array $a, int $i): void {
258258
function (int $int) {
259259
$int + [];
260260
};
261+
262+
function testMod(array $a, object $o): void {
263+
echo 4 % 3;
264+
echo '4' % 3;
265+
echo 4 % '3';
266+
267+
echo $a % 3;
268+
echo 3 % $a;
269+
270+
echo $o % 3;
271+
echo 3 % $o;
272+
}

0 commit comments

Comments
 (0)