1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Only the binary op 'plus' works with two arrays

Treat the result of any other operation as int|float
Fixes #2123
This commit is contained in:
robchett 2023-09-16 15:26:52 +01:00
parent 13ea0c241d
commit 03e8d19eec
2 changed files with 134 additions and 82 deletions

View File

@ -542,11 +542,10 @@ class ArithmeticOpAnalyzer
$has_valid_right_operand = true; $has_valid_right_operand = true;
} }
$result_type = Type::getArray();
return null; return null;
} }
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
$has_valid_right_operand = true; $has_valid_right_operand = true;
$has_valid_left_operand = true; $has_valid_left_operand = true;
@ -634,6 +633,7 @@ class ArithmeticOpAnalyzer
return null; return null;
} }
}
if (($left_type_part instanceof TNamedObject && strtolower($left_type_part->value) === 'gmp') if (($left_type_part instanceof TNamedObject && strtolower($left_type_part->value) === 'gmp')
|| ($right_type_part instanceof TNamedObject && strtolower($right_type_part->value) === 'gmp') || ($right_type_part instanceof TNamedObject && strtolower($right_type_part->value) === 'gmp')

View File

@ -1022,6 +1022,58 @@ class BinaryOperationTest extends TestCase
'$a===' => 'float(9.2233720368548E+18)', '$a===' => 'float(9.2233720368548E+18)',
], ],
], ],
'invalidArrayOperations' => [
'code' => <<<'PHP'
<?php
$a1 = 1 + [];
$a2 = [] + 1;
// This is the one exception to this rule
$a3 = [] + [];
$b1 = 1 - [];
$b2 = [] - 1;
$b3 = [] - [];
$c1 = 1 * [];
$c2 = [] * 1;
$c3 = [] * [];
$d1 = 1 / [];
$d2 = [] / 1;
$d3 = [] / [];
$e1 = 1 ** [];
$e2 = [] ** 1;
$e3 = [] ** [];
$f1 = 1 % [];
$f2 = [] % 1;
$f3 = [] % [];
PHP,
'assertions' => [
'$a1' => 'float|int',
'$a2' => 'float|int',
'$a3' => 'array<never, never>',
'$b1' => 'float|int',
'$b2' => 'float|int',
'$b3' => 'float|int',
'$c1' => 'float|int',
'$c2' => 'float|int',
'$c3' => 'float|int',
'$d1' => 'float|int',
'$d2' => 'float|int',
'$d3' => 'float|int',
'$e1' => 'float|int',
'$e2' => 'float|int',
'$e3' => 'float|int',
'$f1' => 'float|int',
'$f2' => 'float|int',
'$f3' => 'float|int',
],
'ignored_issues' => ['InvalidOperand'],
],
]; ];
} }