Skip to content

Commit c972338

Browse files
staabmondrejmirtes
authored andcommitted
Implement BinaryDivHandler
1 parent fe37546 commit c972338

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\ExprHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Stmt;
8+
use PHPStan\Analyser\ExpressionContext;
9+
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
10+
use PHPStan\Analyser\Generator\ExprAnalysisResult;
11+
use PHPStan\Analyser\Generator\ExprHandler;
12+
use PHPStan\Analyser\Generator\GeneratorScope;
13+
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Reflection\InitializerExprTypeResolver;
16+
use function array_merge;
17+
18+
/**
19+
* @implements ExprHandler<Expr\BinaryOp\Div>
20+
*/
21+
#[AutowiredService]
22+
final class BinaryDivHandler implements ExprHandler
23+
{
24+
25+
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
26+
{
27+
}
28+
29+
public function supports(Expr $expr): bool
30+
{
31+
return $expr instanceof Expr\BinaryOp\Div;
32+
}
33+
34+
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
35+
{
36+
$leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback);
37+
$rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback);
38+
39+
return new ExprAnalysisResult(
40+
$this->initializerExprTypeResolver->getDivTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type),
41+
$this->initializerExprTypeResolver->getDivTypeFromTypes($expr->left, $expr->right, $leftResult->nativeType, $rightResult->nativeType),
42+
$rightResult->scope,
43+
hasYield: $leftResult->hasYield || $rightResult->hasYield,
44+
isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating,
45+
throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints),
46+
impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints),
47+
specifiedTruthyTypes: new SpecifiedTypes(),
48+
specifiedFalseyTypes: new SpecifiedTypes(),
49+
specifiedNullTypes: new SpecifiedTypes(),
50+
);
51+
}
52+
53+
}

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,11 @@ public function getDivType(Expr $left, Expr $right, callable $getTypeCallback):
11741174
$leftType = $getTypeCallback($left);
11751175
$rightType = $getTypeCallback($right);
11761176

1177+
return $this->getDivTypeFromTypes($left, $right, $leftType, $rightType);
1178+
}
1179+
1180+
public function getDivTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
1181+
{
11771182
$leftTypes = $leftType->getConstantScalarTypes();
11781183
$rightTypes = $rightType->getConstantScalarTypes();
11791184
$leftTypesCount = count($leftTypes);

tests/PHPStan/Analyser/Generator/data/gnsr.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public function doPlus($a, $b, int $c, int $d): void
3737
assertNativeType('int', $c + $d);
3838
}
3939

40+
/**
41+
* @param int $a
42+
* @param int $b
43+
* @return void
44+
*/
45+
public function doDiv($a, $b, int $c, int $d): void
46+
{
47+
assertType('(float|int)', $a / $b);
48+
assertNativeType('(float|int)', $a / $b);
49+
assertType('1', 1 / 1);
50+
assertNativeType('1', 1 / 1);
51+
assertType('(float|int)', $c / $d);
52+
assertNativeType('(float|int)', $c / $d);
53+
}
54+
4055
/**
4156
* @param int $a
4257
* @param int $b

0 commit comments

Comments
 (0)