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