1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00
psalm/tests/BinaryOperationTest.php

102 lines
3.0 KiB
PHP
Raw Normal View History

2016-12-23 23:52:34 +00:00
<?php
namespace Psalm\Tests;
class BinaryOperationTest extends TestCase
2016-12-23 23:52:34 +00:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2016-12-23 23:52:34 +00:00
/**
* @return array
2016-12-23 23:52:34 +00:00
*/
public function providerFileCheckerValidCodeParse()
2016-12-23 23:52:34 +00:00
{
return [
'regularAddition' => [
'<?php
2017-05-26 20:05:57 -04:00
$a = 5 + 4;',
],
'differingNumericTypesAdditionInWeakMode' => [
'<?php
2017-05-26 20:05:57 -04:00
$a = 5 + 4.1;',
],
'numericAddition' => [
'<?php
$a = "5";
if (is_numeric($a)) {
$b = $a + 4;
2017-05-26 20:05:57 -04:00
}',
],
'concatenation' => [
'<?php
2017-05-26 20:05:57 -04:00
$a = "Hey " . "Jude,";',
],
'concatenationWithNumberInWeakMode' => [
'<?php
2017-05-26 20:05:57 -04:00
$a = "hi" . 5;',
],
'possiblyInvalidAdditionOnBothSides' => [
'<?php
function foo(string $s) : int {
return strpos($s, "a") + strpos($s, "b");
}',
'assertions' => [],
'error_levels' => ['PossiblyFalseOperand'],
],
];
2016-12-23 23:52:34 +00:00
}
2017-01-13 14:07:23 -05:00
/**
* @return array
2017-01-13 14:07:23 -05:00
*/
public function providerFileCheckerInvalidCodeParse()
2016-12-23 23:52:34 +00:00
{
return [
'badAddition' => [
'<?php
$a = "b" + 5;',
2017-05-26 20:05:57 -04:00
'error_message' => 'InvalidOperand',
],
'differingNumericTypesAdditionInStrictMode' => [
'<?php
$a = 5 + 4.1;',
'error_message' => 'InvalidOperand',
'error_levels' => [],
2017-05-26 20:05:57 -04:00
'strict_mode' => true,
],
'concatenationWithNumberInStrictMode' => [
'<?php
$a = "hi" . 5;',
'error_message' => 'InvalidOperand',
'error_levels' => [],
2017-05-26 20:05:57 -04:00
'strict_mode' => true,
],
'addArrayToNumber' => [
'<?php
$a = [1] + 1;',
'error_message' => 'InvalidOperand',
'error_levels' => [],
2017-05-26 20:05:57 -04:00
'strict_mode' => true,
],
'additionWithClassInWeakMode' => [
'<?php
$a = "hi" + (new stdClass);',
'error_message' => 'InvalidOperand',
],
'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',
],
];
2016-12-23 23:52:34 +00:00
}
}