2016-12-24 00:52:34 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class BinaryOperationTest extends TestCase
|
2016-12-24 00:52:34 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2016-12-24 00:52:34 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2016-12-24 00:52:34 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2016-12-24 00:52:34 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'regularAddition' => [
|
|
|
|
'<?php
|
2017-05-27 02:05:57 +02:00
|
|
|
$a = 5 + 4;',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'differingNumericTypesAdditionInWeakMode' => [
|
|
|
|
'<?php
|
2017-05-27 02:05:57 +02:00
|
|
|
$a = 5 + 4.1;',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'numericAddition' => [
|
|
|
|
'<?php
|
|
|
|
$a = "5";
|
2018-03-08 20:30:40 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (is_numeric($a)) {
|
|
|
|
$b = $a + 4;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'concatenation' => [
|
|
|
|
'<?php
|
2017-05-27 02:05:57 +02:00
|
|
|
$a = "Hey " . "Jude,";',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'concatenationWithNumberInWeakMode' => [
|
|
|
|
'<?php
|
2017-05-27 02:05:57 +02:00
|
|
|
$a = "hi" . 5;',
|
|
|
|
],
|
2018-03-18 21:58:54 +01:00
|
|
|
'possiblyInvalidAdditionOnBothSides' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : int {
|
|
|
|
return strpos($s, "a") + strpos($s, "b");
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['PossiblyFalseOperand'],
|
|
|
|
],
|
2018-04-16 22:36:06 +02:00
|
|
|
'bitwiseoperations' => [
|
|
|
|
'<?php
|
|
|
|
$a = 4 & 5;
|
|
|
|
$b = 2 | 3;
|
|
|
|
$c = 4 ^ 3;
|
|
|
|
$d = 1 << 2;
|
2018-04-17 00:19:18 +02:00
|
|
|
$e = 15 >> 2;
|
|
|
|
$f = "a" & "b";',
|
2018-04-16 22:36:06 +02:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'int',
|
|
|
|
'$c' => 'int',
|
|
|
|
'$d' => 'int',
|
|
|
|
'$e' => 'int',
|
2018-04-17 00:19:18 +02:00
|
|
|
'$f' => 'string',
|
2018-04-16 22:36:06 +02:00
|
|
|
],
|
|
|
|
],
|
2018-05-05 18:59:30 +02:00
|
|
|
'gmpOperations' => [
|
|
|
|
'<?php
|
|
|
|
$a = gmp_init(2);
|
|
|
|
$b = gmp_init(4);
|
|
|
|
$c = $a + $b;
|
2018-05-05 19:04:27 +02:00
|
|
|
$d = $c + 3;
|
2018-05-05 19:14:59 +02:00
|
|
|
echo $d;
|
|
|
|
$f = $a / $b;
|
|
|
|
$g = $a ** $b;
|
|
|
|
$h = $a % $b;
|
|
|
|
|
|
|
|
$i = 6 + $b;
|
|
|
|
$j = 6 - $b;
|
|
|
|
$k = 6 * $b;
|
|
|
|
$l = 6 / $b;
|
|
|
|
$m = 6 ** $b;
|
|
|
|
$n = 6 % $b;
|
|
|
|
|
|
|
|
$o = $a + 6;
|
|
|
|
$p = $a - 6;
|
|
|
|
$q = $a * 6;
|
|
|
|
$r = $a / 6;
|
|
|
|
$s = $a ** 6;
|
|
|
|
$t = $a % 6;',
|
2018-05-05 18:59:30 +02:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'GMP',
|
|
|
|
'$b' => 'GMP',
|
|
|
|
'$c' => 'GMP',
|
|
|
|
'$d' => 'GMP',
|
2018-05-05 19:14:59 +02:00
|
|
|
'$f' => 'GMP',
|
|
|
|
'$g' => 'GMP',
|
|
|
|
'$h' => 'GMP',
|
|
|
|
'$i' => 'GMP',
|
|
|
|
'$j' => 'GMP',
|
|
|
|
'$k' => 'GMP',
|
|
|
|
'$l' => 'GMP',
|
|
|
|
'$m' => 'GMP',
|
|
|
|
'$n' => 'GMP',
|
|
|
|
'$o' => 'GMP',
|
|
|
|
'$p' => 'GMP',
|
|
|
|
'$q' => 'GMP',
|
|
|
|
'$r' => 'GMP',
|
|
|
|
'$s' => 'GMP',
|
|
|
|
'$t' => 'GMP',
|
2018-05-05 18:59:30 +02:00
|
|
|
],
|
|
|
|
],
|
2018-05-28 21:31:20 +02:00
|
|
|
'booleanXor' => [
|
|
|
|
'<?php
|
|
|
|
$a = true ^ false;
|
|
|
|
$b = false ^ false;
|
|
|
|
$c = (true xor false);
|
|
|
|
$d = (false xor false);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'int',
|
|
|
|
'$c' => 'bool',
|
|
|
|
'$d' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
2018-06-01 03:57:17 +02:00
|
|
|
'ternaryAssignment' => [
|
|
|
|
'<?php
|
|
|
|
rand(0, 1) ? $a = 1 : $a = 2;
|
|
|
|
echo $a;',
|
|
|
|
],
|
2018-06-07 21:03:50 +02:00
|
|
|
'assignmentInRHS' => [
|
|
|
|
'<?php
|
|
|
|
$name = rand(0, 1) ? "hello" : null
|
|
|
|
if ($name !== null || ($name = rand(0, 1) ? "hello" : null) !== null) {}',
|
|
|
|
],
|
2018-06-18 19:16:51 +02:00
|
|
|
'floatIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = 1.1;
|
|
|
|
$a++;
|
|
|
|
$b = 1.1;
|
|
|
|
$b += 1;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'float',
|
2018-06-19 19:19:34 +02:00
|
|
|
'$b' => 'float',
|
2018-06-18 19:16:51 +02:00
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2016-12-24 00:52:34 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'badAddition' => [
|
|
|
|
'<?php
|
|
|
|
$a = "b" + 5;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidOperand',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'differingNumericTypesAdditionInStrictMode' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5 + 4.1;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
'error_levels' => [],
|
2017-05-27 02:05:57 +02:00
|
|
|
'strict_mode' => true,
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'concatenationWithNumberInStrictMode' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hi" . 5;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
'error_levels' => [],
|
2017-05-27 02:05:57 +02:00
|
|
|
'strict_mode' => true,
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'addArrayToNumber' => [
|
|
|
|
'<?php
|
|
|
|
$a = [1] + 1;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
'error_levels' => [],
|
2017-05-27 02:05:57 +02:00
|
|
|
'strict_mode' => true,
|
|
|
|
],
|
2018-03-08 20:30:40 +01:00
|
|
|
'additionWithClassInWeakMode' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hi" + (new stdClass);',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
2018-03-18 21:39:34 +01:00
|
|
|
'possiblyInvalidOperand' => [
|
|
|
|
'<?php
|
|
|
|
$b = rand(0, 1) ? [] : 4;
|
|
|
|
echo $b + 5;',
|
|
|
|
'error_message' => 'PossiblyInvalidOperand',
|
|
|
|
],
|
|
|
|
'possiblyInvalidConcat' => [
|
|
|
|
'<?php
|
|
|
|
$b = rand(0, 1) ? [] : "hello";
|
|
|
|
echo $b . "goodbye";',
|
|
|
|
'error_message' => 'PossiblyInvalidOperand',
|
|
|
|
],
|
2018-05-05 18:59:30 +02:00
|
|
|
'invalidGMPOperation' => [
|
|
|
|
'<?php
|
|
|
|
$a = gmp_init(2);
|
|
|
|
$b = "a" + $a;',
|
|
|
|
'error_message' => 'InvalidOperand - src/somefile.php:3 - Cannot add GMP to non-numeric type',
|
|
|
|
],
|
2018-06-18 19:16:51 +02:00
|
|
|
'stringIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
$a++;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
|
|
|
'falseIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
$a++;',
|
|
|
|
'error_message' => 'FalseOperand',
|
|
|
|
],
|
|
|
|
'trueIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = true;
|
|
|
|
$a++;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
}
|