2016-12-17 00:56:23 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class AssignmentTest extends TestCase
|
2016-12-17 00:56:23 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2016-12-17 00:56:23 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'nestedAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = $b = $c = 5;',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'int',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
|
|
|
],
|
2019-01-11 20:57:21 +01:00
|
|
|
'assignmentInByRefParams' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-11 20:57:21 +01:00
|
|
|
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;',
|
|
|
|
],
|
2019-02-18 18:53:55 +01:00
|
|
|
'bitwiseAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 18:53:55 +01:00
|
|
|
$x = 0;
|
2021-08-21 00:52:16 +02:00
|
|
|
$x |= (int) (rand(0, 2) !== 2);
|
2019-02-18 18:53:55 +01:00
|
|
|
$x |= 1;
|
2021-09-20 12:43:19 +02:00
|
|
|
/** @psalm-suppress RedundantCondition Psalm now knows this is always truthy */
|
2019-02-18 18:53:55 +01:00
|
|
|
if ($x) {
|
|
|
|
echo $x;
|
|
|
|
}',
|
|
|
|
],
|
2019-03-26 03:30:40 +01:00
|
|
|
'ifAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-26 03:30:40 +01:00
|
|
|
if ($foo = rand(0, 1)) {
|
|
|
|
echo $foo;
|
|
|
|
}',
|
|
|
|
],
|
2019-08-17 17:22:43 +02:00
|
|
|
'explicitlyTypedMixedAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-17 17:22:43 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
/** @var mixed */
|
|
|
|
$b = $a;',
|
|
|
|
],
|
2019-08-28 05:19:10 +02:00
|
|
|
'referenceAssignmentArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-28 05:19:10 +02:00
|
|
|
$matrix = [
|
|
|
|
[1, 0],
|
|
|
|
[0, 1],
|
|
|
|
];
|
|
|
|
$row =& $matrix[0];
|
|
|
|
echo $row[0];',
|
|
|
|
],
|
|
|
|
'referenceAssignmentLhs' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-28 05:19:10 +02:00
|
|
|
$a = 1;
|
|
|
|
$b =& $a;
|
|
|
|
echo $b;',
|
|
|
|
],
|
|
|
|
'referenceAssignmentRhs' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-28 05:19:10 +02:00
|
|
|
$a = 1;
|
|
|
|
$b =& $a;
|
|
|
|
echo $a;',
|
|
|
|
],
|
2020-07-10 22:49:45 +02:00
|
|
|
'chainedAssignmentUncomplicated' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-10 22:49:45 +02:00
|
|
|
$a = $b = $c = $d = $e = $f = $g = $h = $i = $j = $k = $l = $m
|
|
|
|
= $n = $o = $p = $q = $r = $s = $t = $u = $v = $w = $x = $y
|
|
|
|
= $z = $A = $B = 0;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-07-10 22:49:45 +02:00
|
|
|
'$a' => 'int',
|
|
|
|
'$B' => 'int',
|
|
|
|
]
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2016-12-17 00:56:23 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'mixedAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
$b = $a;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-13 00:06:18 +01:00
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|