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

337 lines
11 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 ReferenceConstraintTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
*
*/
public function providerValidCodeParse(): iterable
{
return [
'functionParameterNoViolation' => [
'code' => '<?php
/** @return void */
function changeInt(int &$a) {
$a = 5;
2017-05-27 02:05:57 +02:00
}',
],
'dontAllowByRefVarToBeAltered' => [
'code' => '<?php
/**
* @param ?string $str
* @psalm-suppress PossiblyNullArgument
*/
2018-01-11 21:50:45 +01:00
function nullable_ref_modifier(&$str): void {
if (strlen($str) > 5) {
$str = null;
}
}',
],
'trackFunctionReturnRefs' => [
'code' => '<?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());',
],
'makeByRefUseMixed' => [
'code' => '<?php
function s(?string $p): void {}
$var = 1;
$callback = function() use(&$var): void {
s($var);
};
$var = null;
$callback();',
'assertions' => [],
'ignored_issues' => ['MixedArgument'],
],
'assignByRefToMixed' => [
'code' => '<?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' => [],
'ignored_issues' => [
'MixedAssignment',
'MixedArrayAccess',
'MixedReturnStatement',
'MixedInferredReturnType',
'MixedOperand',
],
],
'paramOutRefineType' => [
'code' => '<?php
/**
* @param-out string $s
*/
function addFoo(?string &$s) : void {
if ($s === null) {
$s = "hello";
}
$s .= "foo";
}
addFoo($a);
echo strlen($a);',
],
'paramOutChangeType' => [
'code' => '<?php
/**
* @param-out int $s
*/
function addFoo(?string &$s) : void {
if ($s === null) {
$s = 5;
return;
}
$s = 4;
}
addFoo($a);',
'assertions' => [
'$a' => 'int',
],
],
'paramOutReturn' => [
'code' => '<?php
/**
* @param-out bool $s
*/
function foo(?bool &$s) : void {
$s = true;
}
$b = false;
foo($b);',
'assertions' => [
'$b' => 'bool',
],
],
'dontChangeThis' => [
'code' => '<?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' => [
'code' => '<?php
2020-06-15 20:44:55 +02:00
/**
* @param mixed $value
* @param-out int $value
*/
function addValue(&$value) : void {
$value = 5;
}
$foo = [];
addValue($foo["a"]);'
],
'paramOutArrayDefaultNullWithThrow' => [
'code' => '<?php
/**
* @param-out array{errors: int}|null $info
*/
function idnToAsci(?array &$info = null): void {
if (rand(0, 1)) {
$info = null;
}
throw new \UnexpectedValueException();
}'
],
'specificArrayWalkBehavior' => [
'code' => '<?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 {});
}'
],
'investigateByrefArg' => [
'code' => '<?php
class A {}
function takesNullableObj(?A &$a): bool { return true; }
$a = null;
if (takesNullableObj($a) === false) {
return;
} else {}
if ($a) {}',
],
];
}
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
*
*/
public function providerInvalidCodeParse(): iterable
{
return [
'functionParameterViolation' => [
'code' => '<?php
/** @return void */
function changeInt(int &$a) {
$a = "hello";
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'ReferenceConstraintViolation',
],
'classMethodParameterViolation' => [
'code' => '<?php
class A {
/** @var int */
private $foo;
public function __construct(int &$foo) {
$this->foo = &$foo;
$foo = "hello";
}
}
$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',
],
'classMethodParameterViolationInPostAssignment' => [
'code' => '<?php
class A {
/** @var int */
private $foo;
public function __construct(int &$foo) {
$this->foo = &$foo;
}
}
$bar = 5;
$a = new A($bar);
$bar = null;',
2017-05-27 02:05:57 +02:00
'error_message' => 'ReferenceConstraintViolation',
],
'contradictoryReferenceConstraints' => [
'code' => '<?php
class A {
/** @var int */
private $foo;
public function __construct(int &$foo) {
$this->foo = &$foo;
}
}
class B {
/** @var string */
private $bar;
public function __construct(string &$bar) {
$this->bar = &$bar;
}
}
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
}
$v = 8;',
2017-05-27 02:05:57 +02:00
'error_message' => 'ConflictingReferenceConstraint',
],
'invalidDocblockForBadAnnotation' => [
'code' => '<?php
/**
* @param-out array<a(),bool> $ar
*/
function foo(array &$ar) : void {}',
'error_message' => 'InvalidDocblock',
],
'preventTernaryPassedByReference' => [
'code' => '<?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',
],
'SKIPPED-referenceToTypedArrayConstrainsAssignment' => [
'code' => '<?php
class Foo
{
/** @var list<int> */
public array $arr = [];
public function __construct()
{
assert(isset($this->arr[0]));
$int = &$this->arr[0];
$int = (string) $int;
}
}
',
'error_message' => 'ReferenceConstraintViolation',
],
'SKIPPED-referenceToTypedArrayConstrainsAssignmentWithNullReferenceInitialization' => [
'code' => '<?php
class Foo
{
/** @var list<int> */
public array $arr = [];
public function __construct()
{
$int = &$this->arr[0]; // If $this->arr[0] isn\'t set, this will set it to null.
}
}
',
'error_message' => 'PossiblyInvalidPropertyAssignmentValue',
],
];
}
}