2019-06-25 19:00:06 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-06-25 19:00:06 +02:00
|
|
|
namespace Psalm\Tests\Template;
|
|
|
|
|
|
|
|
use Psalm\Tests\TestCase;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2019-06-25 19:00:06 +02:00
|
|
|
|
|
|
|
class FunctionTemplateTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2019-06-25 19:00:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2019-06-25 19:00:06 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'validTemplatedType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar(foo("string"));',
|
|
|
|
],
|
|
|
|
'validPsalmTemplatedFunctionType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template T
|
|
|
|
* @psalm-param T $x
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar(foo("string"));',
|
|
|
|
],
|
|
|
|
'validTemplatedStaticMethodType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public static function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar(A::foo("string"));',
|
|
|
|
],
|
|
|
|
'validTemplatedInstanceMethodType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar((new A())->foo("string"));',
|
|
|
|
],
|
|
|
|
'genericArrayKeys' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as array-key
|
|
|
|
*
|
|
|
|
* @param array<T, mixed> $arr
|
2020-04-17 21:07:41 +02:00
|
|
|
* @return list<T>
|
2019-06-25 19:00:06 +02:00
|
|
|
*/
|
|
|
|
function my_array_keys($arr) {
|
|
|
|
return array_keys($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);',
|
|
|
|
'assertions' => [
|
2020-04-17 21:07:41 +02:00
|
|
|
'$a' => 'list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'genericNonEmptyArrayKeys' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as array-key
|
|
|
|
*
|
|
|
|
* @param non-empty-array<T, mixed> $arr
|
|
|
|
* @return non-empty-list<T>
|
|
|
|
*/
|
|
|
|
function my_array_keys($arr) {
|
|
|
|
return array_keys($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'non-empty-list<string>',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'genericArrayFlip' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue as array-key
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $arr
|
|
|
|
* @return array<TValue, TKey>
|
|
|
|
*/
|
|
|
|
function my_array_flip($arr) {
|
|
|
|
return array_flip($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = my_array_flip(["hello" => 5, "goodbye" => 6]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'array<int, string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'byRefKeyValueArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
* @template TKey as array-key
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $arr
|
|
|
|
*/
|
|
|
|
function byRef(array &$arr) : void {}
|
|
|
|
|
|
|
|
$b = ["a" => 5, "c" => 6];
|
|
|
|
byRef($b);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'array<string, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'byRefMixedKeyArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
*
|
2020-02-21 07:26:51 +01:00
|
|
|
* @param array<TValue> $arr
|
2019-06-25 19:00:06 +02:00
|
|
|
*/
|
|
|
|
function byRef(array &$arr) : void {}
|
|
|
|
|
|
|
|
$b = ["a" => 5, "c" => 6];
|
|
|
|
byRef($b);',
|
|
|
|
'assertions' => [
|
2020-02-21 07:26:51 +01:00
|
|
|
'$b' => 'array<array-key, int>',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'mixedArrayPop' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
*
|
|
|
|
* @param array<array-key, TValue> $arr
|
|
|
|
* @return TValue|null
|
|
|
|
*/
|
|
|
|
function my_array_pop(array &$arr) {
|
|
|
|
return array_pop($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
$b = ["a" => 5, "c" => 6];
|
|
|
|
$a = my_array_pop($b);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'mixed',
|
|
|
|
'$b' => 'array<array-key, mixed>',
|
|
|
|
],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument'],
|
|
|
|
],
|
|
|
|
'genericArrayPop' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
* @template TKey as array-key
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $arr
|
|
|
|
* @return TValue|null
|
|
|
|
*/
|
|
|
|
function my_array_pop(array &$arr) {
|
|
|
|
return array_pop($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = ["a" => 5, "c" => 6];
|
|
|
|
$a = my_array_pop($b);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'int|null',
|
2019-06-25 19:00:06 +02:00
|
|
|
'$b' => 'array<string, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'templateCallableReturnType' => [
|
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @psalm-param callable():T $action
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
function retry(int $maxRetries, callable $action) {
|
|
|
|
return $action();
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInt(int $p): void{};
|
|
|
|
|
|
|
|
takesInt(retry(1, function(): int { return 1; }));',
|
|
|
|
],
|
|
|
|
'templateClosureReturnType' => [
|
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @psalm-param \Closure():T $action
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
function retry(int $maxRetries, callable $action) {
|
|
|
|
return $action();
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInt(int $p): void{};
|
|
|
|
|
|
|
|
takesInt(retry(1, function(): int { return 1; }));',
|
|
|
|
],
|
|
|
|
'replaceChildTypeWithGenerator' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue
|
|
|
|
* @param Traversable<TKey, TValue> $t
|
|
|
|
* @return array<TKey, TValue>
|
|
|
|
*/
|
|
|
|
function f(Traversable $t): array {
|
|
|
|
$ret = [];
|
|
|
|
foreach ($t as $k => $v) $ret[$k] = $v;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return Generator<int, stdClass> */
|
|
|
|
function g():Generator { yield new stdClass; }
|
|
|
|
|
|
|
|
takesArrayOfStdClass(f(g()));
|
|
|
|
|
|
|
|
/** @param array<stdClass> $p */
|
|
|
|
function takesArrayOfStdClass(array $p): void {}',
|
|
|
|
],
|
|
|
|
|
|
|
|
'splatTemplateParam' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $arr
|
|
|
|
* @param array $arr2
|
|
|
|
* @return array<TKey, TValue>
|
|
|
|
*/
|
|
|
|
function splat_proof(array $arr, array $arr2) {
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo = [
|
|
|
|
[1, 2, 3],
|
|
|
|
[1, 2],
|
|
|
|
];
|
|
|
|
|
|
|
|
$a = splat_proof(...$foo);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<int, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'passArrayByRef' => [
|
|
|
|
'<?php
|
|
|
|
function acceptsStdClass(stdClass $_p): void {}
|
|
|
|
|
|
|
|
$q = [new stdClass];
|
|
|
|
acceptsStdClass(fNoRef($q));
|
|
|
|
acceptsStdClass(fRef($q));
|
|
|
|
acceptsStdClass(fNoRef($q));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $_arr
|
|
|
|
* @return null|TValue
|
|
|
|
* @psalm-ignore-nullable-return
|
|
|
|
*/
|
|
|
|
function fRef(array &$_arr) {
|
|
|
|
return array_shift($_arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $_arr
|
|
|
|
* @return null|TValue
|
|
|
|
* @psalm-ignore-nullable-return
|
|
|
|
*/
|
|
|
|
function fNoRef(array $_arr) {
|
|
|
|
return array_shift($_arr);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
|
|
|
|
'classTemplateAsCorrect' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class FooChild extends Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function bar($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
bar(new Foo());
|
|
|
|
bar(new FooChild());',
|
|
|
|
],
|
|
|
|
'classTemplateOfCorrect' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class FooChild extends Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of Foo
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function bar($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
bar(new Foo());
|
|
|
|
bar(new FooChild());',
|
|
|
|
],
|
|
|
|
'classTemplateAsInterface' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {}
|
|
|
|
interface FooChild extends Foo {}
|
|
|
|
class FooImplementer implements Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function bar($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesFoo(Foo $f) : void {
|
|
|
|
bar($f);
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesFooChild(FooChild $f) : void {
|
|
|
|
bar($f);
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesFooImplementer(FooImplementer $f) : void {
|
|
|
|
bar($f);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'templateFunctionVar' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B;
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface D {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as C
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo($some_t) : C {
|
|
|
|
/** @var T */
|
|
|
|
$a = $some_t;
|
|
|
|
$a->bar();
|
|
|
|
|
|
|
|
/** @var T&D */
|
|
|
|
$b = $some_t;
|
|
|
|
$b->bar();
|
|
|
|
|
|
|
|
/** @var D&T */
|
|
|
|
$b = $some_t;
|
|
|
|
$b->bar();
|
|
|
|
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MissingParamType'],
|
|
|
|
],
|
2019-11-04 03:27:40 +01:00
|
|
|
'bindFirstTemplatedClosureParameterValid' => [
|
2019-06-25 19:00:06 +02:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @param Closure(T):void $t1
|
|
|
|
* @param T $t2
|
|
|
|
*/
|
2019-11-04 03:27:40 +01:00
|
|
|
function apply(Closure $t1, $t2) : void {}
|
2019-06-25 19:00:06 +02:00
|
|
|
|
|
|
|
apply(function(int $_i) : void {}, 5);
|
|
|
|
apply(function(string $_i) : void {}, "hello");
|
|
|
|
apply(function(stdClass $_i) : void {}, new stdClass);
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
apply(function(A $_i) : void {}, new AChild());',
|
|
|
|
],
|
|
|
|
'callableReturnsItself' => [
|
|
|
|
'<?php
|
|
|
|
$a =
|
|
|
|
/**
|
|
|
|
* @param callable():string $s
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function(callable $s) {
|
|
|
|
return $s();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T1
|
|
|
|
* @param callable(callable():T1):T1 $s
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function takesReturnTCallable(callable $s) {}
|
|
|
|
|
|
|
|
takesReturnTCallable($a);',
|
|
|
|
],
|
|
|
|
'nonBindingParamReturn' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @param Closure():T $t1
|
|
|
|
* @param T $t2
|
|
|
|
*/
|
|
|
|
function foo(Closure $t1, $t2) : void {}
|
|
|
|
foo(
|
|
|
|
function () : int {
|
|
|
|
return 5;
|
|
|
|
},
|
|
|
|
"hello"
|
|
|
|
);',
|
|
|
|
],
|
|
|
|
'templatedInterfaceMethodInheritReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
class SomeIterator implements IteratorAggregate
|
|
|
|
{
|
|
|
|
public function getIterator() {
|
|
|
|
yield new Foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$i = (new SomeIterator())->getIterator();',
|
|
|
|
[
|
|
|
|
'$i' => 'Traversable<mixed, mixed>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'upcastArrayToIterable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template K
|
|
|
|
* @template V
|
|
|
|
* @param iterable<K,V> $collection
|
|
|
|
* @return V
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
function first($collection) {}
|
|
|
|
|
|
|
|
$one = first([1,2,3]);',
|
|
|
|
[
|
|
|
|
'$one' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'templateIntersectionLeft' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
|
|
|
interface I2 {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as I1&I2
|
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function templatedBar(I1 $a) : void {}',
|
|
|
|
],
|
|
|
|
'templateIntersectionRight' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
|
|
|
interface I2 {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as I1&I2
|
|
|
|
* @param T $b
|
|
|
|
*/
|
|
|
|
function templatedBar(I2 $b) : void {}',
|
|
|
|
],
|
|
|
|
'matchMostSpecificTemplate' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TReturn
|
|
|
|
* @param callable():(\Generator<mixed, mixed, mixed, TReturn>|TReturn) $gen
|
|
|
|
* @return array<int, TReturn>
|
|
|
|
*/
|
|
|
|
function call(callable $gen) : array {
|
|
|
|
$return = $gen();
|
|
|
|
if ($return instanceof Generator) {
|
2020-03-11 18:44:08 +01:00
|
|
|
return [$return->getReturn()];
|
2019-06-25 19:00:06 +02:00
|
|
|
}
|
2019-06-25 21:06:02 +02:00
|
|
|
/** @var array<int, TReturn> */
|
|
|
|
$wrapped_gen = [$gen];
|
|
|
|
return $wrapped_gen;
|
2019-06-25 19:00:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$arr = call(
|
|
|
|
function() {
|
|
|
|
yield 1;
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
);',
|
|
|
|
[
|
|
|
|
'$arr' => 'array<int, string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'templateOfWithSpace' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of array<int, mixed>
|
|
|
|
*/
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Foo<array<int, DateTime>> $a
|
|
|
|
*/
|
|
|
|
function bar(Foo $a) : void {}',
|
|
|
|
],
|
|
|
|
'allowUnionTypeParam' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param callable(T) $x
|
|
|
|
* @param array<T> $y
|
|
|
|
*/
|
|
|
|
function example($x, $y): void {}
|
|
|
|
|
|
|
|
example(
|
|
|
|
/**
|
|
|
|
* @param int|false $x
|
|
|
|
*/
|
|
|
|
function($x): void {},
|
2020-09-11 01:05:47 +02:00
|
|
|
[rand(0, 1) ? 5 : false]
|
2019-06-25 19:00:06 +02:00
|
|
|
);',
|
|
|
|
],
|
|
|
|
'functionTemplateUnionType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T0 as int|string
|
|
|
|
* @param T0 $t
|
|
|
|
* @return T0
|
|
|
|
*/
|
|
|
|
function foo($t) {
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
$s = foo("hello");
|
|
|
|
$i = foo(5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$s' => 'string',
|
|
|
|
'$i' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'reconcileTraversableTemplatedAndNormal' => [
|
|
|
|
'<?php
|
|
|
|
function foo(Traversable $t): void {
|
|
|
|
if ($t instanceof IteratorAggregate) {
|
|
|
|
$a = $t->getIterator();
|
|
|
|
$t = $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$t instanceof Iterator) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1) && rand(0, 1)) {
|
|
|
|
$t->next();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'keyOfTemplate' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as array
|
|
|
|
* @template K as key-of<T>
|
|
|
|
*
|
|
|
|
* @param T $o
|
|
|
|
* @param K $name
|
|
|
|
*
|
|
|
|
* @return T[K]
|
|
|
|
*/
|
|
|
|
function getOffset(array $o, $name) {
|
|
|
|
return $o[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = ["foo" => "hello", "bar" => 2];
|
|
|
|
|
|
|
|
$b = getOffset($a, "foo");
|
|
|
|
$c = getOffset($a, "bar");',
|
|
|
|
[
|
|
|
|
'$b' => 'string',
|
|
|
|
'$c' => 'int',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'dontGeneraliseBoundParamWithWiderCallable' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template T
|
|
|
|
* @psalm-param T $t
|
|
|
|
* @psalm-param callable(?T):void $callable
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function makeConcrete($t, callable $callable) {
|
|
|
|
$callable(rand(0, 1) ? $t : null);
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = makeConcrete(new C(), function (?C $c) : void {});',
|
|
|
|
[
|
|
|
|
'$c' => 'C',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'allowTemplateTypeBeingUsedInsideFunction' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of DateTime
|
|
|
|
* @param callable(T) $callable
|
|
|
|
* @param T $value
|
|
|
|
*/
|
|
|
|
function foo(callable $callable, DateTime $value) : void {
|
|
|
|
$callable($value);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'callFindAnother' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param T $foo
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function loader($foo) {
|
|
|
|
return $foo::getAnother();
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-25 19:00:06 +02:00
|
|
|
class Foo {
|
|
|
|
/** @return static */
|
|
|
|
public static function getAnother() {
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-06-26 22:26:14 +02:00
|
|
|
'templatedVarOnReturn' => [
|
|
|
|
'<?php
|
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $t
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function getAOrB($t) {
|
|
|
|
if ($t instanceof A) {
|
|
|
|
/** @var T */
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var T */
|
|
|
|
return new B();
|
|
|
|
}',
|
|
|
|
],
|
2019-07-15 07:26:08 +02:00
|
|
|
'assertOnTemplatedValue' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @param I $foo
|
|
|
|
*/
|
|
|
|
function bar($foo): void {
|
|
|
|
if (is_string($foo)) {}
|
|
|
|
if (!is_string($foo)) {}
|
|
|
|
if (is_int($foo)) {}
|
|
|
|
if (!is_int($foo)) {}
|
|
|
|
if (is_numeric($foo)) {}
|
|
|
|
if (!is_numeric($foo)) {}
|
|
|
|
if (is_scalar($foo)) {}
|
|
|
|
if (!is_scalar($foo)) {}
|
|
|
|
if (is_bool($foo)) {}
|
|
|
|
if (!is_bool($foo)) {}
|
|
|
|
if (is_object($foo)) {}
|
|
|
|
if (!is_object($foo)) {}
|
|
|
|
if (is_callable($foo)) {}
|
|
|
|
if (!is_callable($foo)) {}
|
|
|
|
}'
|
|
|
|
],
|
2019-10-09 13:40:42 +02:00
|
|
|
'interpretFunctionCallableReturnValue' => [
|
|
|
|
'<?php
|
|
|
|
final class Id
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
private function __construct(string $id)
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromString(string $id): self
|
|
|
|
{
|
|
|
|
return new self($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @psalm-param callable(string): T $generator
|
|
|
|
* @psalm-return callable(): T
|
|
|
|
*/
|
|
|
|
function idGenerator(callable $generator)
|
|
|
|
{
|
|
|
|
return static function () use ($generator) {
|
|
|
|
return $generator("random id");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function client(Id $id): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
$staticIdGenerator = idGenerator([Id::class, "fromString"]);
|
|
|
|
client($staticIdGenerator());'
|
|
|
|
],
|
2019-10-11 18:02:41 +02:00
|
|
|
'noCrashWhenTemplatedClassIsStatic' => [
|
|
|
|
'<?php
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-10-11 18:02:41 +02:00
|
|
|
abstract class Model {
|
|
|
|
/** @return static */
|
|
|
|
public function newInstance() {
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of Model
|
|
|
|
* @param T $m
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo(Model $m) : Model {
|
|
|
|
return $m->newInstance();
|
|
|
|
}'
|
|
|
|
],
|
2019-11-07 17:03:41 +01:00
|
|
|
'unboundVariableIsEmpty' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TE
|
|
|
|
* @template TR
|
|
|
|
*
|
|
|
|
* @param TE $elt
|
|
|
|
* @param TR ...$elts
|
|
|
|
*
|
|
|
|
* @return TE|TR
|
|
|
|
*/
|
|
|
|
function collect($elt, ...$elts) {
|
|
|
|
$ret = $elt;
|
|
|
|
foreach ($elts as $item) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$ret = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo collect("a");'
|
|
|
|
],
|
2019-12-01 16:40:53 +01:00
|
|
|
'paramOutDontLeak' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue
|
|
|
|
*
|
|
|
|
* @param array<TKey, TValue> $arr
|
|
|
|
* @param-out list<TValue> $arr
|
|
|
|
*/
|
|
|
|
function example_sort_by_ref(array &$arr): bool {
|
|
|
|
$arr = array_values($arr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, array{0: int, 1: string}> $array
|
2019-12-01 16:43:51 +01:00
|
|
|
* @return list<array{0: int, 1: string}>
|
2019-12-01 16:40:53 +01:00
|
|
|
*/
|
|
|
|
function example(array $array): array {
|
|
|
|
example_sort_by_ref($array);
|
|
|
|
return $array;
|
|
|
|
}',
|
|
|
|
],
|
2019-12-09 03:40:15 +01:00
|
|
|
'narrowTemplateTypeWithIsObject' => [
|
|
|
|
'<?php
|
|
|
|
function takesObject(object $object): void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as mixed
|
|
|
|
* @param T $value
|
|
|
|
*/
|
|
|
|
function example($value): void {
|
|
|
|
if (is_object($value)) {
|
|
|
|
takesObject($value);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-12-14 03:40:09 +01:00
|
|
|
'falseDefault' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $v
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function exampleWithNullDefault($v = false) {
|
|
|
|
return $v;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'nullDefault' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $v
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function exampleWithNullDefault($v = null) {
|
|
|
|
return $v;
|
|
|
|
}'
|
|
|
|
],
|
2019-12-29 00:37:55 +01:00
|
|
|
'uasortCallable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of object
|
|
|
|
* @psalm-param array<T> $collection
|
|
|
|
* @psalm-param callable(T, T): int $sorter
|
|
|
|
* @psalm-return array<T>
|
|
|
|
*/
|
|
|
|
function order(array $collection, callable $sorter): array {
|
|
|
|
usort($collection, $sorter);
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}'
|
|
|
|
],
|
2020-01-30 23:13:46 +01:00
|
|
|
'callableInference' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class FooChild extends Foo {}
|
|
|
|
class Bar {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template ThingType of Foo
|
|
|
|
* @psalm-param ThingType $t
|
|
|
|
* @return ThingType|Bar
|
|
|
|
*/
|
|
|
|
function from_other(Foo $t) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return new Bar();
|
|
|
|
}
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param list<FooChild> $a
|
|
|
|
* @return list<Bar|FooChild>
|
|
|
|
*/
|
|
|
|
function baz(array $a) {
|
|
|
|
return array_map("from_other", $a);
|
|
|
|
}'
|
|
|
|
],
|
2020-02-02 18:25:24 +01:00
|
|
|
'templateFlipIntersection' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @template S as object
|
|
|
|
* @param S&T $item
|
|
|
|
* @return T&S
|
|
|
|
*/
|
|
|
|
function filter(object $item) {
|
|
|
|
return $item;
|
|
|
|
}',
|
|
|
|
],
|
2020-02-18 01:54:11 +01:00
|
|
|
'splatIntoTemplatedArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param array<T> ...$iterators
|
|
|
|
* @return Generator<T>
|
|
|
|
*/
|
|
|
|
function joinBySplat(array ...$iterators): iterable {
|
|
|
|
foreach ($iterators as $iter) {
|
|
|
|
foreach ($iter as $value) {
|
|
|
|
yield $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-23 00:58:22 +01:00
|
|
|
* @return Generator<int, array<int>>
|
2020-02-18 01:54:11 +01:00
|
|
|
*/
|
|
|
|
function genIters(): Generator {
|
|
|
|
yield [1,2,3];
|
|
|
|
yield [4,5,6];
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = joinBySplat(...genIters());
|
|
|
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
echo $value;
|
|
|
|
}'
|
|
|
|
],
|
2020-02-21 21:17:35 +01:00
|
|
|
'allowTemplatedCast' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-template Tk of array-key
|
|
|
|
* @psalm-param Tk $key
|
|
|
|
*/
|
|
|
|
function at($key) : void {
|
|
|
|
echo (string) $key;
|
|
|
|
}'
|
|
|
|
],
|
2020-02-21 22:44:31 +01:00
|
|
|
'uksortNoNamespace' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template Tk of array-key
|
|
|
|
* @template Tv
|
|
|
|
*
|
|
|
|
* @param array<Tk, Tv> $result
|
|
|
|
* @param (callable(Tk, Tk): int) $comparator
|
|
|
|
*
|
|
|
|
* @preturn array<Tk, Tv>
|
|
|
|
*/
|
|
|
|
function sort_by_key(array $result, callable $comparator): array
|
|
|
|
{
|
|
|
|
\uksort($result, $comparator);
|
|
|
|
return $result;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'uksortNamespaced' => [
|
|
|
|
'<?php
|
|
|
|
namespace Psl\Arr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Tk of array-key
|
|
|
|
* @template Tv
|
|
|
|
*
|
|
|
|
* @param array<Tk, Tv> $result
|
|
|
|
* @param (callable(Tk, Tk): int) $comparator
|
|
|
|
*
|
|
|
|
* @preturn array<Tk, Tv>
|
|
|
|
*/
|
|
|
|
function sort_by_key(array $result, callable $comparator): array
|
|
|
|
{
|
|
|
|
\uksort($result, $comparator);
|
|
|
|
return $result;
|
|
|
|
}'
|
|
|
|
],
|
2020-02-28 03:36:03 +01:00
|
|
|
'mockObject' => [
|
|
|
|
'<?php
|
|
|
|
class MockObject {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template T1 of object
|
|
|
|
* @psalm-param class-string<T1> $originalClassName
|
|
|
|
* @psalm-return MockObject&T1
|
|
|
|
*/
|
|
|
|
function foo(string $originalClassName): MockObject {
|
|
|
|
return createMock($originalClassName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
* @psalm-suppress InvalidReturnStatement
|
|
|
|
*
|
|
|
|
* @psalm-template T2 of object
|
|
|
|
* @psalm-param class-string<T2> $originalClassName
|
|
|
|
* @psalm-return MockObject&T2
|
|
|
|
*/
|
|
|
|
function createMock(string $originalClassName): MockObject {
|
|
|
|
return new MockObject;
|
|
|
|
}'
|
|
|
|
],
|
2020-02-29 21:43:35 +01:00
|
|
|
'testStringCallableInference' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function dup(string $a): string {
|
|
|
|
return $a . $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param iterable<T> $iter
|
|
|
|
* @return list<T>
|
|
|
|
*/
|
|
|
|
function toArray(iterable $iter): array {
|
|
|
|
$data = [];
|
2020-09-30 18:28:13 +02:00
|
|
|
foreach ($iter as $val) {
|
2020-02-29 21:43:35 +01:00
|
|
|
$data[] = $val;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template U
|
|
|
|
* @param callable(T): U $predicate
|
2020-09-30 18:28:13 +02:00
|
|
|
* @return callable(iterable<int, T>): iterable<int, U>
|
2020-02-29 21:43:35 +01:00
|
|
|
*/
|
|
|
|
function map(callable $predicate): callable {
|
|
|
|
return
|
2020-09-30 18:28:13 +02:00
|
|
|
/** @param iterable<int, T> $iter */
|
2020-02-29 21:43:35 +01:00
|
|
|
function(iterable $iter) use ($predicate): iterable {
|
|
|
|
foreach ($iter as $key => $value) {
|
|
|
|
yield $key => $predicate($value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param list<string> $strings */
|
|
|
|
function _test(array $strings): void {}
|
|
|
|
$a = map([A::class, "dup"])(["a", "b", "c"]);',
|
|
|
|
[
|
2020-09-30 18:28:13 +02:00
|
|
|
'$a' => 'iterable<int, string>'
|
2020-02-29 21:43:35 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'testClosureCallableInference' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param iterable<T> $iter
|
|
|
|
* @return list<T>
|
|
|
|
*/
|
|
|
|
function toArray(iterable $iter): array {
|
|
|
|
$data = [];
|
2020-09-30 18:28:13 +02:00
|
|
|
foreach ($iter as $val) {
|
2020-02-29 21:43:35 +01:00
|
|
|
$data[] = $val;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template U
|
|
|
|
* @param callable(T): U $predicate
|
2020-09-30 18:28:13 +02:00
|
|
|
* @return callable(iterable<int, T>): iterable<int, U>
|
2020-02-29 21:43:35 +01:00
|
|
|
*/
|
|
|
|
function map(callable $predicate): callable {
|
|
|
|
return
|
2020-09-30 18:28:13 +02:00
|
|
|
/** @param iterable<int, T> $iter */
|
2020-02-29 21:43:35 +01:00
|
|
|
function(iterable $iter) use ($predicate): iterable {
|
|
|
|
foreach ($iter as $key => $value) {
|
|
|
|
yield $key => $predicate($value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param list<string> $strings */
|
|
|
|
function _test(array $strings): void {}
|
|
|
|
|
|
|
|
$a = map(
|
|
|
|
function (string $a) {
|
|
|
|
return $a . $a;
|
|
|
|
}
|
|
|
|
)(["a", "b", "c"]);',
|
|
|
|
[
|
2020-09-30 18:28:13 +02:00
|
|
|
'$a' => 'iterable<int, string>'
|
2020-02-29 21:43:35 +01:00
|
|
|
]
|
|
|
|
],
|
2020-04-04 23:14:33 +02:00
|
|
|
'possiblyNullMatchesTemplateType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @param T $o
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function takesObject(object $o) : object {
|
|
|
|
return $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/** @psalm-suppress PossiblyNullArgument */
|
|
|
|
$a = takesObject(rand(0, 1) ? new A() : null);',
|
|
|
|
[
|
|
|
|
'$a' => 'A',
|
|
|
|
]
|
|
|
|
],
|
2020-04-06 00:40:25 +02:00
|
|
|
'possiblyNullMatchesAnotherTemplateType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-template RealObjectType of object
|
|
|
|
*
|
|
|
|
* @psalm-param class-string<RealObjectType> $className
|
|
|
|
* @psalm-param Closure(
|
|
|
|
* RealObjectType|null
|
|
|
|
* ) : void $initializer
|
|
|
|
*/
|
|
|
|
function createProxy(
|
|
|
|
string $className,
|
|
|
|
Closure $initializer
|
|
|
|
) : void {}
|
|
|
|
|
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
createProxy(Foo::class, function (?Foo $f) : void {});'
|
|
|
|
],
|
2020-04-06 16:19:30 +02:00
|
|
|
'assertIntersectionsOnTemplatedTypes' => [
|
|
|
|
'<?php
|
|
|
|
interface Input {}
|
|
|
|
interface HasFoo {}
|
|
|
|
interface HasBar {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template InputType of Input
|
|
|
|
* @psalm-param InputType $input
|
|
|
|
* @psalm-return InputType&HasFoo
|
|
|
|
*/
|
|
|
|
function decorateWithFoo(Input $input): Input
|
|
|
|
{
|
|
|
|
assert($input instanceof HasFoo);
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template InputType of Input
|
|
|
|
* @psalm-param InputType $input
|
|
|
|
* @psalm-return InputType&HasBar
|
|
|
|
*/
|
|
|
|
function decorateWithBar(Input $input): Input
|
|
|
|
{
|
|
|
|
assert($input instanceof HasBar);
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param HasFoo&HasBar $input */
|
|
|
|
function useFooAndBar(object $input): void {}
|
|
|
|
|
|
|
|
function consume(Input $input): void {
|
|
|
|
useFooAndBar(decorateWithFoo(decorateWithBar($input)));
|
|
|
|
}'
|
|
|
|
],
|
2020-04-07 06:13:56 +02:00
|
|
|
'bottomTypeInClosureShouldNotBind' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $className
|
|
|
|
* @param Closure(T):void $outmaker
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function createProxy(
|
|
|
|
string $className,
|
|
|
|
Closure $outmaker
|
|
|
|
) : object {
|
2020-08-06 16:18:55 +02:00
|
|
|
/** @psalm-suppress MixedMethodCall */
|
2020-04-07 06:13:56 +02:00
|
|
|
$t = new $className();
|
|
|
|
$outmaker($t);
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
createProxy(A::class, function(object $o):void {})->bar();'
|
|
|
|
],
|
2020-04-21 03:36:44 +02:00
|
|
|
'bottomTypeInNamespacedCallableShouldMatch' => [
|
|
|
|
'<?php
|
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $className
|
|
|
|
* @param callable(T):void $outmaker
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function createProxy(
|
|
|
|
string $className,
|
|
|
|
callable $outmaker
|
|
|
|
) : object {
|
2020-08-06 16:18:55 +02:00
|
|
|
/** @psalm-suppress MixedMethodCall */
|
2020-04-21 03:36:44 +02:00
|
|
|
$t = new $className();
|
|
|
|
$outmaker($t);
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(A $o):void {}
|
|
|
|
|
|
|
|
createProxy(A::class, \'Ns\foo\')->bar();',
|
|
|
|
],
|
2020-05-16 22:41:07 +02:00
|
|
|
'compareToEmptyArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @param T $a
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function ex($a) {
|
|
|
|
if($a === []) {}
|
|
|
|
return $a;
|
|
|
|
}'
|
|
|
|
],
|
2020-07-03 07:07:50 +02:00
|
|
|
'compareToFalse' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as int|false
|
|
|
|
* @param T $value
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function foo($value) {
|
|
|
|
if ($value === false) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}'
|
|
|
|
],
|
2020-05-22 19:16:48 +02:00
|
|
|
'refineTemplateTypeNotEmpty' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of Iterator|null
|
|
|
|
* @param T $iterator
|
|
|
|
*/
|
|
|
|
function toArray($iterator): array
|
|
|
|
{
|
|
|
|
if ($iterator) {
|
|
|
|
return iterator_to_array($iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}'
|
|
|
|
],
|
2020-05-26 23:55:54 +02:00
|
|
|
'manyGenericParams' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TArg1
|
|
|
|
* @template TArg2
|
|
|
|
* @template TRes
|
|
|
|
*
|
|
|
|
* @psalm-param Closure(TArg1, TArg2): TRes $func
|
|
|
|
* @psalm-param TArg1 $arg1
|
|
|
|
*
|
|
|
|
* @psalm-return Closure(TArg2): TRes
|
|
|
|
*/
|
|
|
|
function partial(Closure $func, $arg1): Closure {
|
|
|
|
return fn($arg2) => $func($arg1, $arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TArg1
|
|
|
|
* @template TArg2
|
|
|
|
* @template TRes
|
|
|
|
*
|
|
|
|
* @template T as (Closure(): TRes | Closure(TArg1): TRes | Closure(TArg1, TArg2): TRes)
|
|
|
|
*
|
|
|
|
* @psalm-param T $fn
|
|
|
|
* @psalm-param TArg1 $arg
|
|
|
|
*/
|
|
|
|
function foo(Closure $fn, $arg): void {
|
|
|
|
$a = partial($fn, $arg);
|
2020-08-10 15:58:43 +02:00
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'7.4'
|
2020-05-26 23:55:54 +02:00
|
|
|
],
|
2020-08-25 21:50:33 +02:00
|
|
|
'mixedDoesntSwallowNull' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template E
|
|
|
|
* @param E $e
|
|
|
|
* @param mixed $d
|
|
|
|
* @return ?E
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
*/
|
|
|
|
function reduce_values($e, $d) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$c = $e;
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
/** @psalm-suppress MixedAssignment */
|
|
|
|
$c = $d;
|
|
|
|
} else {
|
|
|
|
$c = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress MixedReturnStatement */
|
|
|
|
return $c;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'mixedDoesntSwallowNullProgressive' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template E
|
|
|
|
* @param E $e
|
|
|
|
* @param mixed $d
|
|
|
|
* @return ?E
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
*/
|
|
|
|
function reduce_values($e, $d)
|
|
|
|
{
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$d = $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
/** @psalm-suppress MixedReturnStatement */
|
|
|
|
return $d;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}'
|
|
|
|
],
|
2020-08-26 17:00:38 +02:00
|
|
|
'inferIterableArrayKeyAfterIsArrayCheck' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template Key
|
|
|
|
* @template Element
|
|
|
|
* @psalm-param iterable<Key, Element> $input
|
|
|
|
* @psalm-return Iterator<Key, Element>
|
|
|
|
*/
|
|
|
|
function to_iterator(iterable $input): Iterator
|
|
|
|
{
|
|
|
|
if (\is_array($input)) {
|
|
|
|
return new \ArrayIterator($input);
|
|
|
|
} elseif ($input instanceof Iterator) {
|
|
|
|
return $input;
|
|
|
|
} else {
|
|
|
|
return new \IteratorIterator($input);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-10-27 15:01:17 +01:00
|
|
|
'doublyNestedFunctionTemplates' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-template Tk
|
|
|
|
* @psalm-template Tv
|
|
|
|
*
|
|
|
|
* @psalm-param iterable<Tk, Tv> $iterable
|
|
|
|
* @psalm-param (callable(Tk, Tv): bool)|null $predicate
|
|
|
|
*
|
|
|
|
* @psalm-return iterable<Tk, Tv>
|
|
|
|
*/
|
|
|
|
function filter_with_key(iterable $iterable, ?callable $predicate = null): iterable
|
|
|
|
{
|
|
|
|
return (static function () use ($iterable, $predicate): Generator {
|
|
|
|
$predicate = $predicate ??
|
|
|
|
/**
|
|
|
|
* @psalm-param Tk $_k
|
|
|
|
* @psalm-param Tv $v
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function($_k, $v) { return (bool) $v; };
|
|
|
|
|
|
|
|
foreach ($iterable as $k => $v) {
|
|
|
|
if ($predicate($k, $v)) {
|
|
|
|
yield $k => $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}'
|
|
|
|
],
|
2020-11-11 23:46:09 +01:00
|
|
|
'allowClosureParamLowerBoundAndUpperBound' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TParam as Foo
|
|
|
|
* @psalm-param Closure(TParam): void $func
|
|
|
|
* @psalm-return Closure(TParam): TParam
|
|
|
|
*/
|
|
|
|
function takesClosure(callable $func): callable {
|
|
|
|
return
|
|
|
|
/**
|
|
|
|
* @psalm-param TParam $value
|
|
|
|
* @psalm-return TParam
|
|
|
|
*/
|
|
|
|
function ($value) use ($func) {
|
|
|
|
$func($value);
|
|
|
|
return $value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = takesClosure(function(Foo $foo) : void {})(new Foo());'
|
|
|
|
],
|
2021-01-10 00:08:02 +01:00
|
|
|
'subtractTemplatedNull' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T|null $var
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function notNull($var) {
|
|
|
|
if ($var === null) {
|
|
|
|
throw new \InvalidArgumentException("");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesNullableString(?string $s) : string {
|
|
|
|
return notNull($s);
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'subtractTemplatedInt' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T|int $var
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function notNull($var) {
|
|
|
|
if (\is_int($var)) {
|
|
|
|
throw new \InvalidArgumentException("");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesNullableString(string|int $s) : string {
|
|
|
|
return notNull($s);
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
'8.0'
|
|
|
|
],
|
2021-01-10 00:58:29 +01:00
|
|
|
'templateChildClass' => [
|
|
|
|
'<?php
|
|
|
|
/** @template T */
|
|
|
|
class Collection {
|
|
|
|
/**
|
|
|
|
* @param T $t
|
|
|
|
*/
|
|
|
|
private function add($t) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TChild as T
|
|
|
|
* @param TChild $default
|
|
|
|
*
|
|
|
|
* @return TChild
|
|
|
|
*/
|
|
|
|
public function get($default)
|
|
|
|
{
|
|
|
|
$this->add($default);
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2021-02-12 18:32:24 +01:00
|
|
|
'isArrayCheckOnTemplated' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TIterable of iterable
|
|
|
|
*/
|
2021-02-14 18:07:09 +01:00
|
|
|
function toList(iterable $iterable): void {
|
2021-02-12 18:32:24 +01:00
|
|
|
if (is_array($iterable)) {}
|
|
|
|
}'
|
|
|
|
],
|
2021-10-20 23:14:35 +02:00
|
|
|
'transformNestedTemplateWherePossible' => [
|
2021-02-14 18:07:09 +01:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
* @template TArray of non-empty-array<TValue>
|
|
|
|
* @param TArray $arr
|
|
|
|
* @return TValue
|
|
|
|
*/
|
|
|
|
function toList(array $arr): array {
|
|
|
|
return reset($arr);
|
|
|
|
}'
|
|
|
|
],
|
2021-02-26 00:43:04 +01:00
|
|
|
'callTemplatedFunctionWithTemplatedClassString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template Ta of object
|
|
|
|
* @psalm-param Ta $obj
|
|
|
|
* @return Ta
|
|
|
|
*/
|
|
|
|
function a(string $str, object $obj) {
|
|
|
|
$class = get_class($obj);
|
|
|
|
return deserialize_object($str, $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template Tb
|
|
|
|
* @psalm-param class-string<Tb> $type
|
|
|
|
* @psalm-return Tb
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
function deserialize_object(string $data, string $type) {}'
|
|
|
|
],
|
2021-10-09 20:03:52 +02:00
|
|
|
'arrayKeyInTemplateOfArrayKey' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TKey of array-key
|
|
|
|
* @template TValue
|
|
|
|
* @template TNewKey of array-key
|
|
|
|
* @template TNewValue
|
|
|
|
* @psalm-param iterable<TKey, TValue> $iterable
|
|
|
|
* @psalm-param callable(TKey, TValue): iterable<TNewKey, TNewValue> $mapper
|
|
|
|
* @psalm-return \Generator<TNewKey, TNewValue>
|
|
|
|
*/
|
|
|
|
function map(iterable $iterable, callable $mapper): Generator
|
|
|
|
{
|
|
|
|
foreach ($iterable as $key => $value) {
|
|
|
|
yield from $mapper($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-return iterable<array-key, \stdClass>
|
|
|
|
*/
|
|
|
|
function iter(): iterable
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TKey of array-key
|
|
|
|
* @psalm-param TKey $key
|
|
|
|
* @psalm-return Generator<TKey, string>
|
|
|
|
*/
|
|
|
|
function mapper($key): Generator
|
|
|
|
{
|
|
|
|
yield $key => "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
map(iter(), "mapper");'
|
|
|
|
],
|
2021-10-24 19:30:39 +02:00
|
|
|
'dontScreamForArithmeticsOnIntTemplates' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of int|string
|
|
|
|
* @param T $p
|
|
|
|
*/
|
|
|
|
function foo($p): void {
|
|
|
|
if (is_int($p)) {
|
|
|
|
$q = $p - 1;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-06-25 19:00:06 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-20 02:44:44 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
|
2019-06-25 19:00:06 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2019-06-25 19:00:06 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'invalidTemplatedType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar(foo(4));',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'invalidTemplatedStaticMethodType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public static function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar(A::foo(4));',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'invalidTemplatedInstanceMethodType' => [
|
|
|
|
'<?php
|
|
|
|
namespace FooFoo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public function foo($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $a): void { }
|
|
|
|
|
|
|
|
bar((new A())->foo(4));',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'replaceChildTypeNoHint' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TKey as array-key
|
|
|
|
* @template TValue
|
|
|
|
* @param Traversable<TKey, TValue> $t
|
|
|
|
* @return array<TKey, TValue>
|
|
|
|
*/
|
|
|
|
function f(Traversable $t): array {
|
|
|
|
$ret = [];
|
|
|
|
foreach ($t as $k => $v) $ret[$k] = $v;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function g():Generator { yield new stdClass; }
|
|
|
|
|
|
|
|
takesArrayOfStdClass(f(g()));
|
|
|
|
|
|
|
|
/** @param array<stdClass> $p */
|
|
|
|
function takesArrayOfStdClass(array $p): void {}',
|
|
|
|
'error_message' => 'MixedArgumentTypeCoercion',
|
|
|
|
],
|
|
|
|
'classTemplateAsIncorrectClass' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class NotFoo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function bar($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
bar(new NotFoo());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'classTemplateAsIncorrectInterface' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {}
|
|
|
|
interface NotFoo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function bar($x) {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesNotFoo(NotFoo $f) : void {
|
|
|
|
bar($f);
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'templateFunctionMethodCallWithoutMethod' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B;
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as C
|
|
|
|
* @param T $some_t
|
|
|
|
*/
|
|
|
|
function foo($some_t) : void {
|
|
|
|
$some_t->bar();
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyUndefinedMethod',
|
|
|
|
],
|
|
|
|
'templateFunctionMethodCallWithoutAsType' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $some_t
|
|
|
|
*/
|
|
|
|
function foo($some_t) : void {
|
|
|
|
$some_t->bar();
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedMethodCall',
|
|
|
|
],
|
2019-11-04 03:27:40 +01:00
|
|
|
'bindFirstTemplatedClosureParameterInvalidScalar' => [
|
2019-06-25 19:00:06 +02:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @param Closure(T):void $t1
|
|
|
|
* @param T $t2
|
|
|
|
*/
|
|
|
|
function apply(Closure $t1, $t2) : void
|
|
|
|
{
|
|
|
|
$t1($t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(function(int $_i) : void {}, "hello");',
|
2020-04-08 18:37:38 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'bindFirstTemplatedClosureParameterTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @param Closure(T):void $t1
|
|
|
|
* @param T $t2
|
|
|
|
*/
|
|
|
|
function apply(Closure $t1, $t2) : void
|
|
|
|
{
|
|
|
|
$t1($t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
apply(function(AChild $_i) : void {}, new A());',
|
2020-04-08 18:37:38 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
|
|
|
|
'callableDoesNotReturnItself' => [
|
|
|
|
'<?php
|
|
|
|
$b =
|
|
|
|
/**
|
|
|
|
* @param callable():int $s
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function(callable $s) {
|
|
|
|
return "#" . $s();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T1
|
|
|
|
* @param callable(callable():T1):T1 $s
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function takesReturnTCallable(callable $s) {}
|
|
|
|
|
|
|
|
takesReturnTCallable($b);',
|
2020-04-08 18:37:38 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'multipleArgConstraintWithMoreRestrictiveFirstArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param callable(T):void $c1
|
|
|
|
* @param callable(T):void $c2
|
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function foo(callable $c1, callable $c2, $a): void {
|
|
|
|
$c1($a);
|
|
|
|
$c2($a);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(
|
|
|
|
function(AChild $_a) : void {},
|
|
|
|
function(A $_a) : void {},
|
|
|
|
new A()
|
|
|
|
);',
|
2020-04-08 18:37:38 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'multipleArgConstraintWithMoreRestrictiveSecondArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param callable(T):void $c1
|
|
|
|
* @param callable(T):void $c2
|
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function foo(callable $c1, callable $c2, $a): void {
|
|
|
|
$c1($a);
|
|
|
|
$c2($a);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(
|
|
|
|
function(A $_a) : void {},
|
|
|
|
function(AChild $_a) : void {},
|
|
|
|
new A()
|
|
|
|
);',
|
2020-04-08 18:37:38 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'multipleArgConstraintWithLessRestrictiveThirdArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param callable(T):void $c1
|
|
|
|
* @param callable(T):void $c2
|
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function foo(callable $c1, callable $c2, $a): void {
|
|
|
|
$c1($a);
|
|
|
|
$c2($a);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(
|
|
|
|
function(AChild $_a) : void {},
|
|
|
|
function(AChild $_a) : void {},
|
|
|
|
new A()
|
|
|
|
);',
|
2020-04-08 18:37:38 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'possiblyInvalidArgumentWithUnionFirstArg' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $a
|
|
|
|
* @param T $b
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo($a, $b) {
|
|
|
|
return rand(0, 1) ? $a : $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo foo([], "hello");',
|
|
|
|
'error_message' => 'PossiblyInvalidArgument',
|
|
|
|
],
|
|
|
|
'possiblyInvalidArgumentWithUnionSecondArg' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $a
|
|
|
|
* @param T $b
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo($a, $b) {
|
|
|
|
return rand(0, 1) ? $a : $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo foo("hello", []);',
|
|
|
|
'error_message' => 'PossiblyInvalidArgument',
|
|
|
|
],
|
|
|
|
'preventTemplateTypeAsBeingUsedInsideFunction' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of DateTime
|
|
|
|
* @param callable(T) $callable
|
|
|
|
*/
|
|
|
|
function foo(callable $callable) : void {
|
|
|
|
$callable(new \DateTime());
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'preventWrongTemplateBeingPassed' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of DateTime
|
|
|
|
* @template T2 of DateTime
|
|
|
|
* @param callable(T): T $parameter
|
|
|
|
* @param T2 $value
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo(callable $parameter, $value)
|
|
|
|
{
|
|
|
|
return $parameter($value);
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
|
|
|
'preventTemplateTypeReturnMoreGeneral' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T of DateTimeInterface
|
|
|
|
* @param T $x
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo($x)
|
|
|
|
{
|
|
|
|
return new \DateTime();
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2019-06-25 19:00:06 +02:00
|
|
|
],
|
2019-06-25 21:06:02 +02:00
|
|
|
'preventReturningString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @psalm-param T $t
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function mirror($t) {
|
|
|
|
return "string";
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2019-06-25 21:06:02 +02:00
|
|
|
],
|
2019-06-26 22:26:14 +02:00
|
|
|
'unTemplatedVarOnReturn' => [
|
|
|
|
'<?php
|
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $t
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function getAOrB($t) {
|
|
|
|
if ($t instanceof A) {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new B();
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2019-06-26 22:26:14 +02:00
|
|
|
],
|
2019-08-13 19:15:23 +02:00
|
|
|
'templateReturnTypeOfCallableWithIncompatibleType' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public static function returnsObjectOrNull() : ?A {
|
|
|
|
return random_int(0, 1) ? new A() : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template T as object
|
|
|
|
* @psalm-param callable() : T $callback
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
function makeResultSet(callable $callback)
|
|
|
|
{
|
|
|
|
return $callback();
|
|
|
|
}
|
|
|
|
|
2019-11-12 06:52:10 +01:00
|
|
|
makeResultSet([B::class, "returnsObjectOrNull"]);',
|
2019-08-13 19:15:23 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-11-07 15:39:24 +01:00
|
|
|
'templateInvokeArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param callable(T):void $c
|
|
|
|
* @param T $param
|
|
|
|
*/
|
|
|
|
function apply(callable $c, $param):void{
|
|
|
|
call_user_func($c, $param);
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function __toString(){
|
|
|
|
return "a";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
class Printer{
|
|
|
|
public function __invoke(A $a) : void {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(new Printer(), new B());',
|
|
|
|
'error_message' => 'InvalidArgument'
|
|
|
|
],
|
2019-12-09 03:19:29 +01:00
|
|
|
'invalidTemplateDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/** @template */
|
|
|
|
function f():void {}',
|
|
|
|
'error_message' => 'MissingDocblockType'
|
|
|
|
],
|
2020-02-25 05:14:16 +01:00
|
|
|
'returnNamedObjectWhereTemplateIsExpected' => [
|
|
|
|
'<?php
|
|
|
|
class Bar {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @param T $t
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function shouldComplain(object $t) {
|
|
|
|
return new Bar();
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2020-02-28 00:42:15 +01:00
|
|
|
'returnIntersectionWhenTemplateIsExpectedForward' => [
|
|
|
|
'<?php
|
|
|
|
interface Baz {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @param T $t
|
|
|
|
* @return T&Baz
|
|
|
|
*/
|
|
|
|
function returnsTemplatedIntersection(object $t) {
|
|
|
|
return $t;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'returnIntersectionWhenTemplateIsExpectedBackward' => [
|
|
|
|
'<?php
|
|
|
|
interface Baz {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @param T $t
|
|
|
|
* @return Baz&T
|
|
|
|
*/
|
|
|
|
function returnsTemplatedIntersection(object $t) {
|
|
|
|
return $t;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2020-04-07 06:13:56 +02:00
|
|
|
'bottomTypeInClosureShouldClash' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $className
|
|
|
|
* @param Closure(T):void $outmaker
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function createProxy(
|
|
|
|
string $className,
|
|
|
|
Closure $outmaker
|
|
|
|
) : object {
|
2020-08-06 16:18:55 +02:00
|
|
|
/** @psalm-suppress MixedMethodCall */
|
2020-04-07 06:13:56 +02:00
|
|
|
$t = new $className();
|
|
|
|
$outmaker($t);
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
createProxy(A::class, function(B $o):void {})->bar();',
|
|
|
|
'error_message' => 'InvalidArgument'
|
|
|
|
],
|
2020-04-21 03:36:44 +02:00
|
|
|
'bottomTypeInNamespacedCallableShouldClash' => [
|
|
|
|
'<?php
|
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $className
|
|
|
|
* @param callable(T):void $outmaker
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function createProxy(
|
|
|
|
string $className,
|
|
|
|
callable $outmaker
|
|
|
|
) : object {
|
2020-08-06 16:18:55 +02:00
|
|
|
/** @psalm-suppress MixedMethodCall */
|
2020-04-21 03:36:44 +02:00
|
|
|
$t = new $className();
|
|
|
|
$outmaker($t);
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
function foo(B $o):void {}
|
|
|
|
|
|
|
|
createProxy(A::class, \'Ns\foo\')->bar();',
|
|
|
|
'error_message' => 'InvalidArgument'
|
|
|
|
],
|
2021-05-05 06:42:06 +02:00
|
|
|
'preventBadArraySubtyping' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as array{a: int}
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo() : array {
|
|
|
|
$b = ["a" => 123];
|
|
|
|
return $b;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
|
|
|
'modifyTemplatedShape' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as array{a: int}
|
|
|
|
* @param T $s
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo(array $s) : array {
|
|
|
|
$s["a"] = 123;
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
2021-05-05 07:06:53 +02:00
|
|
|
'preventArrayOverwriting' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function foo(array $b) : array {
|
|
|
|
return $b;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
2021-05-15 04:50:11 +02:00
|
|
|
'catchIssueInTemplatedFunctionInsideClass' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*/
|
|
|
|
interface Container {
|
|
|
|
/** @param T $value */
|
|
|
|
public function take($value): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param Container<T> $c
|
|
|
|
*/
|
|
|
|
function jsonFromEntityCollection(Container $c): void {
|
|
|
|
$c->take("foo");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-06-25 19:00:06 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|