2016-12-24 00:52:34 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function class_exists;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class BinaryOperationTest extends TestCase
|
2016-12-24 00:52:34 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2016-12-24 00:52:34 +01:00
|
|
|
|
2018-07-17 02:52:58 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGMPOperations()
|
|
|
|
{
|
|
|
|
if (class_exists('GMP') === false) {
|
|
|
|
$this->markTestSkipped('Cannot run test, base class "GMP" does not exist!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
$a = gmp_init(2);
|
|
|
|
$b = gmp_init(4);
|
|
|
|
$c = $a + $b;
|
|
|
|
$d = $c + 3;
|
|
|
|
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;'
|
|
|
|
);
|
|
|
|
|
|
|
|
$assertions = [
|
|
|
|
'$a' => 'GMP',
|
|
|
|
'$b' => 'GMP',
|
|
|
|
'$c' => 'GMP',
|
|
|
|
'$d' => 'GMP',
|
|
|
|
'$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',
|
|
|
|
];
|
|
|
|
|
|
|
|
$context = new \Psalm\Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$actual_vars = [];
|
|
|
|
foreach ($assertions as $var => $_) {
|
|
|
|
if (isset($context->vars_in_scope[$var])) {
|
|
|
|
$actual_vars[$var] = (string)$context->vars_in_scope[$var];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($assertions, $actual_vars);
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:25:59 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStrictTrueEquivalence()
|
|
|
|
{
|
|
|
|
$config = \Psalm\Config::getInstance();
|
|
|
|
$config->strict_binary_operands = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function returnsABool(): bool {
|
|
|
|
return rand(1, 2) === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (returnsABool() === true) {
|
|
|
|
echo "hi!";
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage('RedundantIdentityWithTrue');
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new \Psalm\Context());
|
|
|
|
}
|
|
|
|
|
2016-12-24 00:52:34 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2016-12-24 00:52:34 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
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;',
|
2018-11-07 14:45:26 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'differingNumericTypesAdditionInWeakMode' => [
|
|
|
|
'<?php
|
2017-05-27 02:05:57 +02:00
|
|
|
$a = 5 + 4.1;',
|
2018-11-07 14:45:26 +01:00
|
|
|
'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',
|
|
|
|
],
|
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-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
|
2018-09-17 18:15:45 +02:00
|
|
|
$name = rand(0, 1) ? "hello" : null;
|
2018-06-07 21:03:50 +02:00
|
|
|
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
|
|
|
],
|
|
|
|
],
|
2019-01-13 16:15:15 +01:00
|
|
|
'exponent' => [
|
|
|
|
'<?php
|
|
|
|
$a = "x" ^ "y";
|
|
|
|
$b = 4 ^ 5;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
2019-04-13 16:11:25 +02:00
|
|
|
'bitwiseNot' => [
|
|
|
|
'<?php
|
|
|
|
$a = ~4;
|
|
|
|
$b = ~4.0;
|
|
|
|
$c = ~4.4;
|
|
|
|
$d = ~"a";',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'int',
|
|
|
|
'$c' => 'int',
|
|
|
|
'$d' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2019-09-01 23:49:37 +02:00
|
|
|
'stringIncrementSuppressed' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
/** @psalm-suppress StringIncrement */
|
|
|
|
$a++;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2019-11-27 06:34:23 +01:00
|
|
|
'nullCoalescingAssignment' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $s): string {
|
|
|
|
$s ??= "Hello";
|
|
|
|
return $s;
|
2020-08-23 16:32:07 +02:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [],
|
|
|
|
'7.4',
|
2019-11-27 06:34:23 +01:00
|
|
|
],
|
2020-03-09 19:09:41 +01:00
|
|
|
'nullCoalescingArrayAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
foreach ($arr as $a) {
|
|
|
|
$b[0] ??= $a;
|
|
|
|
}
|
2020-08-23 16:32:07 +02:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [],
|
|
|
|
'7.4',
|
2020-03-09 19:09:41 +01:00
|
|
|
],
|
2020-05-20 02:10:01 +02:00
|
|
|
'addArrays' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{host?:string} $opts
|
|
|
|
* @return array{host:string|int}
|
|
|
|
*/
|
|
|
|
function a(array $opts): array {
|
|
|
|
return $opts + ["host" => 5];
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
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;',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'InvalidOperand - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:26 - Cannot add GMP to non-numeric type',
|
2018-05-05 18:59:30 +02:00
|
|
|
],
|
2018-06-18 19:16:51 +02:00
|
|
|
'stringIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
$a++;',
|
2019-09-01 23:49:37 +02:00
|
|
|
'error_message' => 'StringIncrement',
|
2018-06-18 19:16:51 +02:00
|
|
|
],
|
|
|
|
'falseIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
$a++;',
|
|
|
|
'error_message' => 'FalseOperand',
|
|
|
|
],
|
|
|
|
'trueIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = true;
|
|
|
|
$a++;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
2018-07-04 01:52:40 +02:00
|
|
|
'possiblyDivByZero' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5 / (rand(0, 1) ? 2 : null);',
|
|
|
|
'error_message' => 'PossiblyNullOperand',
|
|
|
|
],
|
2019-01-13 16:15:15 +01:00
|
|
|
'invalidExponent' => [
|
|
|
|
'<?php
|
|
|
|
$a = "x" ^ 1;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
|
|
|
'invalidBitwiseOr' => [
|
|
|
|
'<?php
|
|
|
|
$a = "x" | new stdClass;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
2019-04-13 16:11:25 +02:00
|
|
|
'invalidBitwiseNot' => [
|
|
|
|
'<?php
|
|
|
|
$a = ~new stdClass;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
|
|
|
'possiblyInvalidBitwiseNot' => [
|
|
|
|
'<?php
|
|
|
|
$a = ~(rand(0, 1) ? 2 : null);',
|
|
|
|
'error_message' => 'PossiblyInvalidOperand',
|
|
|
|
],
|
|
|
|
'invalidBooleanBitwiseNot' => [
|
|
|
|
'<?php
|
|
|
|
$a = ~true;',
|
|
|
|
'error_message' => 'InvalidOperand',
|
|
|
|
],
|
2020-07-24 16:08:57 +02:00
|
|
|
'substrImpossible' => [
|
|
|
|
'<?php
|
|
|
|
class HelloWorld
|
|
|
|
{
|
|
|
|
public function sayHello(string $s): void
|
|
|
|
{
|
|
|
|
if (substr($s, 0, 6) === "abc") {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
}
|