1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #1069 - module arithmetic always returns ints

This commit is contained in:
Matthew Brown 2018-11-07 08:45:26 -05:00
parent 6cf6d2cfda
commit 2065e0129e
2 changed files with 34 additions and 5 deletions

View File

@ -1011,7 +1011,9 @@ class BinaryOpChecker
if (($left_type_part instanceof TNumeric || $right_type_part instanceof TNumeric)
&& ($left_type_part->isNumericType() && $right_type_part->isNumericType())
) {
if (!$result_type) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} elseif (!$result_type) {
$result_type = Type::getNumeric();
} else {
$result_type = Type::combineUnionTypes(Type::getNumeric(), $result_type);
@ -1024,7 +1026,9 @@ class BinaryOpChecker
}
if ($left_type_part instanceof TInt && $right_type_part instanceof TInt) {
if (!$result_type) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} elseif (!$result_type) {
$result_type = Type::getInt(true);
} else {
$result_type = Type::combineUnionTypes(Type::getInt(true), $result_type);
@ -1037,7 +1041,9 @@ class BinaryOpChecker
}
if ($left_type_part instanceof TFloat && $right_type_part instanceof TFloat) {
if (!$result_type) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} elseif (!$result_type) {
$result_type = Type::getFloat();
} else {
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
@ -1064,7 +1070,9 @@ class BinaryOpChecker
}
}
if (!$result_type) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} elseif (!$result_type) {
$result_type = Type::getFloat();
} else {
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
@ -1089,7 +1097,9 @@ class BinaryOpChecker
}
}
if (!$result_type) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} elseif (!$result_type) {
$result_type = Type::getFloat();
} else {
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);

View File

@ -89,10 +89,29 @@ class BinaryOperationTest extends TestCase
'regularAddition' => [
'<?php
$a = 5 + 4;',
'assertions' => [
'$a' => 'int',
],
],
'differingNumericTypesAdditionInWeakMode' => [
'<?php
$a = 5 + 4.1;',
'assertions' => [
'$a' => 'float',
],
],
'modulo' => [
'<?php
$a = 25 % 2;
$b = 25.4 % 2;
$c = 25 % 2.5;
$d = 25.5 % 2.5;',
'assertions' => [
'$a' => 'int',
'$b' => 'int',
'$c' => 'int',
'$d' => 'int',
],
],
'numericAddition' => [
'<?php