2017-02-23 06:25:28 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ReferenceConstraintTest extends TestCase
|
2017-02-23 06:25:28 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-02-23 06:25:28 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-23 06:25:28 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
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
|
|
|
|
*/
|
|
|
|
function nullable_ref_modifier(&$str) : void {
|
|
|
|
if (strlen($str) > 5) {
|
|
|
|
$str = null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-23 06:25:28 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
}
|