Skip to content

Commit db623a6

Browse files
Sdd support for numeric string computation
1 parent f6e65bd commit db623a6

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,23 +1446,26 @@ private function callOperatorTypeSpecifyingExtensions(Expr\BinaryOp $expr, Type
14461446
*/
14471447
private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $rightType): Type
14481448
{
1449-
if (($leftType instanceof IntegerRangeType || $leftType instanceof ConstantIntegerType || $leftType instanceof UnionType) &&
1450-
($rightType instanceof IntegerRangeType || $rightType instanceof ConstantIntegerType || $rightType instanceof UnionType)
1449+
$leftNumberType = $leftType->toNumber();
1450+
$rightNumberType = $rightType->toNumber();
1451+
1452+
if (($leftNumberType instanceof IntegerRangeType || $leftNumberType instanceof ConstantIntegerType || $leftType instanceof UnionType) &&
1453+
($rightNumberType instanceof IntegerRangeType || $rightNumberType instanceof ConstantIntegerType || $rightType instanceof UnionType)
14511454
) {
14521455

1453-
if ($leftType instanceof ConstantIntegerType) {
1456+
if ($leftNumberType instanceof ConstantIntegerType) {
14541457
return $this->integerRangeMath(
1455-
$leftType,
1458+
$leftNumberType,
14561459
$expr,
1457-
$rightType,
1460+
$rightNumberType,
14581461
);
14591462
} elseif ($leftType instanceof UnionType) {
1460-
14611463
$unionParts = [];
14621464

14631465
foreach ($leftType->getTypes() as $type) {
1464-
if ($type instanceof IntegerRangeType || $type instanceof ConstantIntegerType) {
1465-
$unionParts[] = $this->integerRangeMath($type, $expr, $rightType);
1466+
$numberType = $type->toNumber();
1467+
if ($numberType instanceof IntegerRangeType || $numberType instanceof ConstantIntegerType) {
1468+
$unionParts[] = $this->integerRangeMath($numberType, $expr, $rightNumberType);
14661469
} else {
14671470
$unionParts[] = $type;
14681471
}
@@ -1493,8 +1496,6 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
14931496
return new ErrorType();
14941497
}
14951498

1496-
$leftNumberType = $leftType->toNumber();
1497-
$rightNumberType = $rightType->toNumber();
14981499
if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
14991500
return new ErrorType();
15001501
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ public function dataFileAsserts(): iterable
12491249
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8956.php');
12501250
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8917.php');
12511251
yield from $this->gatherAssertTypes(__DIR__ . '/data/ds-copy.php');
1252+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8803.php');
12521253
yield from $this->gatherAssertTypes(__DIR__ . '/data/trait-type-alias.php');
12531254
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8609.php');
12541255
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/PhpDoc/data/bug-8609-function.php');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug8803;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
public function sayHello(): void
10+
{
11+
$from = new \DateTimeImmutable('2023-01-30');
12+
for ($offset = 1; $offset <= 14; $offset++) {
13+
$value = $from->format('N') + $offset;
14+
15+
assertType("'1'|'2'|'3'|'4'|'5'|'6'|'7'", $from->format('N'));
16+
assertType('int<1, 14>', $offset);
17+
assertType('int<2, 21>', $value);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)