mirror of
https://github.com/danog/psalm.git
synced 2024-12-11 16:59:45 +01:00
1f0aca00b3
This change assigns the type of the rhs expression to the variable that will become a reference, as well as preventing clobbering of the rhs if it is an already-typed variable. Fixes: #2077
101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
class AssignmentTest extends TestCase
|
|
{
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
/**
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
*/
|
|
public function providerValidCodeParse()
|
|
{
|
|
return [
|
|
'nestedAssignment' => [
|
|
'<?php
|
|
$a = $b = $c = 5;',
|
|
'assertions' => [
|
|
'$a' => 'int',
|
|
],
|
|
],
|
|
'assignmentInByRefParams' => [
|
|
'<?php
|
|
function foo(?string $s, ?string $t): void {}
|
|
foo($s = null, $t = null);
|
|
echo $s;
|
|
echo $t;
|
|
|
|
function foo2(?string &$u, ?string &$v): void {}
|
|
foo2($u = null, $v = null);
|
|
echo $u;
|
|
echo $v;
|
|
|
|
$read = [fopen(\'php://stdin\', \'rb\')];
|
|
$return = stream_select($read, $w = null, $e = null, 0);
|
|
echo $w;
|
|
echo $e;',
|
|
],
|
|
'bitwiseAssignment' => [
|
|
'<?php
|
|
$x = 0;
|
|
$x |= (int) (rand(0, 1) !== 2);
|
|
$x |= 1;
|
|
if ($x) {
|
|
echo $x;
|
|
}',
|
|
],
|
|
'ifAssignment' => [
|
|
'<?php
|
|
if ($foo = rand(0, 1)) {
|
|
echo $foo;
|
|
}',
|
|
],
|
|
'explicitlyTypedMixedAssignment' => [
|
|
'<?php
|
|
/** @var mixed */
|
|
$a = 5;
|
|
/** @var mixed */
|
|
$b = $a;',
|
|
],
|
|
'referenceAssignmentArray' => [
|
|
'<?php
|
|
$matrix = [
|
|
[1, 0],
|
|
[0, 1],
|
|
];
|
|
$row =& $matrix[0];
|
|
echo $row[0];',
|
|
],
|
|
'referenceAssignmentLhs' => [
|
|
'<?php
|
|
$a = 1;
|
|
$b =& $a;
|
|
echo $b;',
|
|
],
|
|
'referenceAssignmentRhs' => [
|
|
'<?php
|
|
$a = 1;
|
|
$b =& $a;
|
|
echo $a;',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
|
*/
|
|
public function providerInvalidCodeParse()
|
|
{
|
|
return [
|
|
'mixedAssignment' => [
|
|
'<?php
|
|
/** @var mixed */
|
|
$a = 5;
|
|
$b = $a;',
|
|
'error_message' => 'MixedAssignment',
|
|
],
|
|
];
|
|
}
|
|
}
|