2016-12-17 00:56:23 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class AssignmentTest extends TestCase
|
2016-12-17 00:56:23 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2016-12-17 00:56:23 +01:00
|
|
|
|
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
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2016-12-17 00:56:23 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
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;',
|
|
|
|
],
|
2019-02-18 18:53:55 +01:00
|
|
|
'bitwiseAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$x = 0;
|
|
|
|
$x |= (int) (rand(0, 1) !== 2);
|
|
|
|
$x |= 1;
|
|
|
|
if ($x) {
|
|
|
|
echo $x;
|
|
|
|
}',
|
|
|
|
],
|
2019-03-26 03:30:40 +01:00
|
|
|
'ifAssignment' => [
|
|
|
|
'<?php
|
|
|
|
if ($foo = rand(0, 1)) {
|
|
|
|
echo $foo;
|
|
|
|
}',
|
|
|
|
],
|
2019-08-17 17:22:43 +02:00
|
|
|
'explicitlyTypedMixedAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
/** @var mixed */
|
|
|
|
$b = $a;',
|
|
|
|
],
|
2019-08-28 05:19:10 +02:00
|
|
|
'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;',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2016-12-17 00:56:23 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2016-12-17 00:56:23 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'mixedAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/** @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
|
|
|
}
|