Skip to content

Commit 1af7377

Browse files
AdAdd support for boolean
1 parent 8cc1a93 commit 1af7377

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,30 +1446,38 @@ private function callOperatorTypeSpecifyingExtensions(Expr\BinaryOp $expr, Type
14461446
*/
14471447
private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $rightType): Type
14481448
{
1449+
$types = TypeCombinator::union($leftType, $rightType);
14491450
$leftNumberType = $leftType->toNumber();
14501451
$rightNumberType = $rightType->toNumber();
14511452

1452-
if ($rightNumberType instanceof IntegerRangeType || $rightNumberType instanceof ConstantIntegerType || $rightType instanceof UnionType) {
1453+
if (
1454+
!$types instanceof MixedType
1455+
&& (
1456+
$rightNumberType instanceof IntegerRangeType
1457+
|| $rightNumberType instanceof ConstantIntegerType
1458+
|| $rightNumberType instanceof UnionType
1459+
)
1460+
) {
14531461
if ($leftNumberType instanceof IntegerRangeType || $leftNumberType instanceof ConstantIntegerType) {
14541462
return $this->integerRangeMath(
14551463
$leftNumberType,
14561464
$expr,
14571465
$rightNumberType,
14581466
);
1459-
} elseif ($leftType instanceof UnionType) {
1467+
} elseif ($leftNumberType instanceof UnionType) {
14601468
$unionParts = [];
14611469

1462-
foreach ($leftType->getTypes() as $type) {
1470+
foreach ($leftNumberType->getTypes() as $type) {
14631471
$numberType = $type->toNumber();
14641472
if ($numberType instanceof IntegerRangeType || $numberType instanceof ConstantIntegerType) {
14651473
$unionParts[] = $this->integerRangeMath($numberType, $expr, $rightNumberType);
14661474
} else {
1467-
$unionParts[] = $type;
1475+
$unionParts[] = $numberType;
14681476
}
14691477
}
14701478

14711479
$union = TypeCombinator::union(...$unionParts);
1472-
if ($leftType instanceof BenevolentUnionType) {
1480+
if ($leftNumberType instanceof BenevolentUnionType) {
14731481
return TypeUtils::toBenevolentUnion($union)->toNumber();
14741482
}
14751483

@@ -1482,7 +1490,6 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
14821490
return $specifiedTypes;
14831491
}
14841492

1485-
$types = TypeCombinator::union($leftType, $rightType);
14861493
if (
14871494
$leftType->isArray()->yes()
14881495
|| $rightType->isArray()->yes()

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ public function dataFileAsserts(): iterable
12501250
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8917.php');
12511251
yield from $this->gatherAssertTypes(__DIR__ . '/data/ds-copy.php');
12521252
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8803.php');
1253+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8827.php');
12531254
yield from $this->gatherAssertTypes(__DIR__ . '/data/trait-type-alias.php');
12541255
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8609.php');
12551256
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/PhpDoc/data/bug-8609-function.php');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug8827;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
public function test(): void
10+
{
11+
$efferent = $afferent = 0;
12+
$nbElements = random_int(0, 30);
13+
14+
$elements = array_fill(0, $nbElements, random_int(0, 2));
15+
16+
foreach ($elements as $element)
17+
{
18+
$efferent += ($element === 1);
19+
$afferent += ($element === 2);
20+
}
21+
22+
assertType('int<0, max>', $efferent); // Expected: int<0, $nbElements> | Actual: 0|1
23+
assertType('int<0, max>', $afferent); // Expected: int<0, $nbElements> | Actual: 0|1
24+
25+
$instability = ($efferent + $afferent > 0) ? $efferent / ($afferent + $efferent) : 0;
26+
}
27+
}

0 commit comments

Comments
 (0)