2017-02-23 06:25:28 +01:00
|
|
|
<?php
|
|
|
|
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 ReferenceConstraintTest extends TestCase
|
2017-02-23 06:25:28 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-02-23 06:25:28 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-02-23 06:25:28 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-02-23 06:25:28 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'functionParameterNoViolation' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function changeInt(int &$a) {
|
|
|
|
$a = 5;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-12-15 22:48:06 +01:00
|
|
|
'dontAllowByRefVarToBeAltered' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param ?string $str
|
|
|
|
* @psalm-suppress PossiblyNullArgument
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function nullable_ref_modifier(&$str): void {
|
2017-12-15 22:48:06 +01:00
|
|
|
if (strlen($str) > 5) {
|
|
|
|
$str = null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-12 05:18:13 +01:00
|
|
|
'trackFunctionReturnRefs' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "bar";
|
|
|
|
|
|
|
|
public function &getString() : string {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function useString(string &$s) : void {}
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
useString($a->getString());',
|
|
|
|
],
|
2018-01-12 18:33:26 +01:00
|
|
|
'makeByRefUseMixed' => [
|
|
|
|
'<?php
|
|
|
|
function s(?string $p): void {}
|
|
|
|
|
|
|
|
$var = 1;
|
|
|
|
$callback = function() use(&$var): void {
|
|
|
|
s($var);
|
|
|
|
};
|
|
|
|
$var = null;
|
|
|
|
$callback();',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedArgument'],
|
|
|
|
],
|
2018-02-11 16:39:21 +01:00
|
|
|
'assignByRefToMixed' => [
|
|
|
|
'<?php
|
|
|
|
function testRef() : array {
|
|
|
|
$result = [];
|
|
|
|
foreach ([1, 2, 1] as $v) {
|
|
|
|
$x = &$result;
|
|
|
|
if (!isset($x[$v])) {
|
|
|
|
$x[$v] = 0;
|
|
|
|
}
|
|
|
|
$x[$v] ++;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
|
|
|
'MixedArrayAccess',
|
|
|
|
'MixedReturnStatement',
|
|
|
|
'MixedInferredReturnType',
|
2018-06-18 19:16:51 +02:00
|
|
|
'MixedOperand',
|
2018-02-11 16:39:21 +01:00
|
|
|
],
|
|
|
|
],
|
2019-03-03 21:11:09 +01:00
|
|
|
'paramOutRefineType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param-out string $s
|
|
|
|
*/
|
|
|
|
function addFoo(?string &$s) : void {
|
|
|
|
if ($s === null) {
|
|
|
|
$s = "hello";
|
|
|
|
}
|
|
|
|
$s .= "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
addFoo($a);
|
|
|
|
|
|
|
|
echo strlen($a);',
|
|
|
|
],
|
|
|
|
'paramOutChangeType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param-out int $s
|
|
|
|
*/
|
|
|
|
function addFoo(?string &$s) : void {
|
|
|
|
if ($s === null) {
|
|
|
|
$s = 5;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$s = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
addFoo($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
],
|
|
|
|
],
|
2019-03-04 00:21:12 +01:00
|
|
|
'paramOutReturn' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param-out bool $s
|
|
|
|
*/
|
|
|
|
function foo(?bool &$s) : void {
|
|
|
|
$s = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = false;
|
|
|
|
foo($b);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
2019-03-04 02:16:11 +01:00
|
|
|
'dontChangeThis' => [
|
|
|
|
'<?php
|
|
|
|
interface I {}
|
|
|
|
class C implements I {
|
|
|
|
public function foo() : self {
|
|
|
|
bar($this);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(I &$i) : void {}',
|
|
|
|
],
|
2020-06-15 20:44:55 +02:00
|
|
|
'notEmptyArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
* @param-out int $value
|
|
|
|
*/
|
|
|
|
function addValue(&$value) : void {
|
|
|
|
$value = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo = [];
|
|
|
|
|
|
|
|
addValue($foo["a"]);'
|
|
|
|
],
|
2021-03-27 14:50:15 +01:00
|
|
|
'paramOutArrayDefaultNullWithThrow' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param-out array{errors: int}|null $info
|
|
|
|
*/
|
|
|
|
function idnToAsci(?array &$info = null): void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$info = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \UnexpectedValueException();
|
|
|
|
}'
|
|
|
|
],
|
2021-09-06 15:15:25 +02:00
|
|
|
'specificArrayWalkBehavior' => [
|
|
|
|
'<?php
|
|
|
|
function withArrayWalk(array &$val): void {
|
|
|
|
array_walk($val, /** @param mixed $arg */ function (&$arg): void {});
|
|
|
|
}
|
|
|
|
function withArrayWalkRecursive(array &$val): void {
|
|
|
|
array_walk_recursive($val, /** @param mixed $arg */ function (&$arg): void {});
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-20 02:44:44 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
|
2017-02-23 06:25:28 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-02-23 06:25:28 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'functionParameterViolation' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function changeInt(int &$a) {
|
|
|
|
$a = "hello";
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ReferenceConstraintViolation',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'classMethodParameterViolation' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $foo;
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct(int &$foo) {
|
|
|
|
$this->foo = &$foo;
|
|
|
|
$foo = "hello";
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bar = 5;
|
|
|
|
$a = new A($bar); // $bar is constrained to an int
|
|
|
|
$bar = null; // ReferenceConstraintViolation issue emitted',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ReferenceConstraintViolation',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'classMethodParameterViolationInPostAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $foo;
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct(int &$foo) {
|
|
|
|
$this->foo = &$foo;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bar = 5;
|
|
|
|
$a = new A($bar);
|
|
|
|
$bar = null;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ReferenceConstraintViolation',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'contradictoryReferenceConstraints' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $foo;
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct(int &$foo) {
|
|
|
|
$this->foo = &$foo;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
private $bar;
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct(string &$bar) {
|
|
|
|
$this->bar = &$bar;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$v = 5;
|
|
|
|
$c = (new A($v)); // $v is constrained to an int
|
|
|
|
} else {
|
|
|
|
$v = "hello";
|
|
|
|
$c = (new B($v)); // $v is constrained to a string
|
|
|
|
}
|
2017-06-20 20:38:13 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$v = 8;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ConflictingReferenceConstraint',
|
|
|
|
],
|
2020-01-11 16:47:31 +01:00
|
|
|
'invalidDocblockForBadAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param-out array<a(),bool> $ar
|
|
|
|
*/
|
|
|
|
function foo(array &$ar) : void {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2020-03-05 04:30:53 +01:00
|
|
|
'preventTernaryPassedByReference' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string $p
|
|
|
|
*/
|
|
|
|
function b(&$p): string {
|
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main(bool $a, string $b, string $c): void {
|
|
|
|
b($a ? $b : $c);
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPassByReference',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
}
|