1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 02:07:37 +01:00
psalm/tests/AssignmentTest.php

116 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class AssignmentTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
2017-01-13 20:07:23 +01:00
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
2017-01-13 20:07:23 +01:00
*/
public function providerValidCodeParse(): iterable
{
return [
'nestedAssignment' => [
'<?php
$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' => [
'<?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, 2) !== 2);
$x |= 1;
/** @psalm-suppress RedundantCondition Psalm now knows this is always truthy */
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;',
],
'chainedAssignmentUncomplicated' => [
'<?php
$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;',
[
'$a' => 'int',
'$B' => 'int',
]
],
];
}
/**
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
*/
public function providerInvalidCodeParse(): iterable
{
return [
'mixedAssignment' => [
'<?php
/** @var mixed */
$a = 5;
$b = $a;',
2017-05-27 02:05:57 +02:00
'error_message' => 'MixedAssignment',
],
];
2017-02-13 00:06:18 +01:00
}
}