Skip to content

Commit

Permalink
Merge pull request #6167 from orklah/non-div-with-numeric-and-int
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Jul 25, 2021
2 parents 783dc61 + 55245cf commit 82dfbbc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,15 @@ private static function analyzeNonDivOperands(
}

if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} elseif (!$result_type) {
$result_type = Type::getNumeric();
$new_result_type = Type::getInt();
} else {
$new_result_type = new Type\Union([new TFloat(), new TInt()]);
}

if (!$result_type) {
$result_type = $new_result_type;
} else {
$result_type = Type::combineUnionTypes(Type::getNumeric(), $result_type);
$result_type = Type::combineUnionTypes($new_result_type, $result_type);
}

$has_valid_right_operand = true;
Expand Down
20 changes: 20 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,26 @@ function foo(int $s1): string {
return "foo" . $s1;
}',
],
'numericWithInt' => [
'<?php
/** @return numeric */
function getNumeric(){
return 1;
}
$a = getNumeric();
$a++;
$b = getNumeric() * 2;
$c = 1 - getNumeric();
$d = 2;
$d -= getNumeric();
',
'assertions' => [
'$a' => 'float|int',
'$b' => 'float|int',
'$c' => 'float|int',
'$d' => 'float|int',
],
],
'encapsedStringWithIntIncludingLiterals' => [
'<?php
/**
Expand Down

0 comments on commit 82dfbbc

Please sign in to comment.