2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class FunctionCallTest extends TestCase
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-02-14 01:16:25 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-21 16:22:04 +01:00
|
|
|
* @return array
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
2018-01-21 16:22:04 +01:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
2018-01-21 16:22:04 +01:00
|
|
|
return [
|
|
|
|
'arrayFilter' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_filter(["a" => 5, "b" => 12, "c" => null]);
|
|
|
|
$e = array_filter(
|
|
|
|
["a" => 5, "b" => 12, "c" => null],
|
|
|
|
function(?int $i): bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$d' => 'array<string, int>',
|
|
|
|
'$e' => 'array<string, null|int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayFilterAdvanced' => [
|
2017-07-25 22:11:02 +02:00
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$f = array_filter(["a" => 5, "b" => 12, "c" => null], function(?int $val, string $key): bool {
|
2017-07-25 22:11:02 +02:00
|
|
|
return true;
|
|
|
|
}, ARRAY_FILTER_USE_BOTH);
|
2018-01-11 21:50:45 +01:00
|
|
|
$g = array_filter(["a" => 5, "b" => 12, "c" => null], function(string $val): bool {
|
2017-07-25 22:11:02 +02:00
|
|
|
return true;
|
|
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
|
|
|
|
|
|
$bar = "bar";
|
|
|
|
|
|
|
|
$foo = [
|
2018-01-11 21:50:45 +01:00
|
|
|
$bar => function (): string {
|
2017-07-25 22:11:02 +02:00
|
|
|
return "baz";
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
$foo = array_filter(
|
|
|
|
$foo,
|
2018-01-11 21:50:45 +01:00
|
|
|
function (string $key): bool {
|
2017-07-25 22:11:02 +02:00
|
|
|
return $key === "bar";
|
|
|
|
},
|
|
|
|
ARRAY_FILTER_USE_KEY
|
2018-01-21 16:22:04 +01:00
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$f' => 'array<string, null|int>',
|
|
|
|
'$g' => 'array<string, null|int>',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'typedArrayWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @param array<A> $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(array $a = []): void {
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-18 23:16:50 +01:00
|
|
|
'abs' => [
|
2017-07-15 00:09:25 +02:00
|
|
|
'<?php
|
|
|
|
$a = abs(-5);
|
|
|
|
$b = abs(-7.5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'validDocblockParamDefault' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int|false $p
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-05-27 02:05:57 +02:00
|
|
|
function f($p = false) {}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'byRef' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(string &$v): void {}
|
2017-05-27 02:05:57 +02:00
|
|
|
fooFoo($a);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'namespaced' => [
|
|
|
|
'<?php
|
|
|
|
namespace A;
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
function f(int $p) {}
|
2017-05-27 02:05:57 +02:00
|
|
|
f(5);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'namespacedRootFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace {
|
|
|
|
/** @return void */
|
|
|
|
function foo() { }
|
|
|
|
}
|
|
|
|
namespace A\B\C {
|
|
|
|
foo();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'namespacedAliasedFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace Aye {
|
|
|
|
/** @return void */
|
|
|
|
function foo() { }
|
|
|
|
}
|
|
|
|
namespace Bee {
|
|
|
|
use Aye as A;
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
A\foo();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayKeys' => [
|
|
|
|
'<?php
|
|
|
|
$a = array_keys(["a" => 1, "b" => 2]);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'array<int, string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayKeysMixed' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array */
|
|
|
|
$b = ["a" => 5];
|
|
|
|
$a = array_keys($b);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'array<int, mixed>',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MixedArgument'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayValues' => [
|
|
|
|
'<?php
|
|
|
|
$b = array_values(["a" => 1, "b" => 2]);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$b' => 'array<int, int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayCombine' => [
|
|
|
|
'<?php
|
|
|
|
$c = array_combine(["a", "b", "c"], [1, 2, 3]);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$c' => 'array<string, int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayMerge' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_merge(["a", "b", "c"], [1, 2, 3]);',
|
|
|
|
'assertions' => [
|
2017-12-19 00:47:17 +01:00
|
|
|
'$d' => 'array{0:string, 1:string, 2:string, 3:int, 4:int, 5:int}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-04 00:22:50 +01:00
|
|
|
'arrayReverse' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_reverse(["a", "b", 1]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$d' => 'array<int, int|string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayReversePreserveKey' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_reverse(["a", "b", 1], true);',
|
|
|
|
'assertions' => [
|
|
|
|
'$d' => 'array<int, int|string>',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'arrayDiff' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_diff(["a" => 5, "b" => 12], [5]);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$d' => 'array<string, int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-20 05:46:22 +01:00
|
|
|
'arrayPopMixed' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$b = ["a" => 5, "c" => 6];
|
|
|
|
$a = array_pop($b);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'mixed',
|
|
|
|
'$b' => 'mixed',
|
|
|
|
],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument'],
|
|
|
|
],
|
2017-12-05 07:47:49 +01:00
|
|
|
'uasort' => [
|
|
|
|
'<?php
|
|
|
|
uasort(
|
|
|
|
$manifest,
|
|
|
|
function ($a, $b) {
|
|
|
|
return strcmp($a["parent"],$b["parent"]);
|
|
|
|
}
|
|
|
|
);',
|
|
|
|
'assertions' => [],
|
2018-01-29 02:03:47 +01:00
|
|
|
'error_levels' => [
|
|
|
|
'MixedArrayAccess',
|
|
|
|
'MixedArgument',
|
|
|
|
'MissingClosureParamType',
|
|
|
|
'MissingClosureReturnType',
|
|
|
|
],
|
2017-12-05 07:47:49 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'byRefAfterCallable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable $callback
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function route($callback) {
|
|
|
|
if (!is_callable($callback)) { }
|
|
|
|
$a = preg_match("", "", $b);
|
|
|
|
if ($b[0]) {}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedArrayAccess',
|
2018-02-07 21:20:47 +01:00
|
|
|
'RedundantConditionGivenDocblockType',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-24 23:07:03 +01:00
|
|
|
'ignoreNullablePregReplace' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
$s = preg_replace("/hello/", "", $s);
|
|
|
|
if ($s === null) {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
function bar(string $s): string {
|
|
|
|
$s = preg_replace("/hello/", "", $s);
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
function bat(string $s): ?string {
|
|
|
|
$s = preg_replace("/hello/", "", $s);
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'extractVarCheck' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $str): void {}
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = null;
|
|
|
|
$a = ["$foo" => "bar"];
|
|
|
|
extract($a);
|
|
|
|
takesString($foo);',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedArrayAccess',
|
|
|
|
],
|
2017-05-06 00:53:45 +02:00
|
|
|
],
|
|
|
|
'arrayMergeObjectLike' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, int> $a
|
|
|
|
* @return array<string, int>
|
|
|
|
*/
|
|
|
|
function foo($a)
|
|
|
|
{
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a1 = ["hi" => 3];
|
|
|
|
$a2 = ["bye" => 5];
|
|
|
|
$a3 = array_merge($a1, $a2);
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
foo($a3);',
|
2017-11-11 20:19:45 +01:00
|
|
|
'assertions' => [
|
2017-12-19 00:47:17 +01:00
|
|
|
'$a3' => 'array{hi:int, bye:int}',
|
2017-11-11 20:19:45 +01:00
|
|
|
],
|
2017-05-06 00:53:45 +02:00
|
|
|
],
|
2017-09-16 19:16:21 +02:00
|
|
|
'goodByRef' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$b = "goodbye";
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(string &$v): void {}
|
2017-09-16 19:16:21 +02:00
|
|
|
|
|
|
|
fooFoo($a->foo);
|
|
|
|
fooFoo($b);',
|
|
|
|
],
|
2017-11-07 23:38:54 +01:00
|
|
|
'arrayRand' => [
|
|
|
|
'<?php
|
|
|
|
$vars = ["x" => "a", "y" => "b"];
|
|
|
|
$c = array_rand($vars);
|
2017-12-19 00:47:17 +01:00
|
|
|
$d = $vars[$c];
|
|
|
|
$more_vars = ["a", "b"];
|
|
|
|
$e = array_rand($more_vars);',
|
2017-11-07 23:38:54 +01:00
|
|
|
|
|
|
|
'assertions' => [
|
|
|
|
'$vars' => 'array{x:string, y:string}',
|
|
|
|
'$c' => 'string',
|
|
|
|
'$d' => 'string',
|
2017-12-19 00:47:17 +01:00
|
|
|
'$more_vars' => 'array{0:string, 1:string}',
|
|
|
|
'$e' => 'int',
|
2017-11-07 23:38:54 +01:00
|
|
|
],
|
|
|
|
],
|
2017-11-09 03:56:54 +01:00
|
|
|
'arrayRandMultiple' => [
|
|
|
|
'<?php
|
|
|
|
$vars = ["x" => "a", "y" => "b"];
|
|
|
|
$b = 3;
|
|
|
|
$c = array_rand($vars, 1);
|
|
|
|
$d = array_rand($vars, 2);
|
|
|
|
$e = array_rand($vars, 3);
|
|
|
|
$f = array_rand($vars, $b);',
|
|
|
|
|
|
|
|
'assertions' => [
|
|
|
|
'$vars' => 'array{x:string, y:string}',
|
|
|
|
'$c' => 'string',
|
|
|
|
'$e' => 'array<int, string>',
|
|
|
|
'$f' => 'array<int, string>|string',
|
|
|
|
],
|
|
|
|
],
|
2017-12-10 22:17:27 +01:00
|
|
|
'arrayKeysNoEmpty' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function expect_string(string $x): void {
|
2017-12-10 22:17:27 +01:00
|
|
|
echo $x;
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function test(): void {
|
2017-12-10 22:17:27 +01:00
|
|
|
foreach (array_keys([]) as $key) {
|
|
|
|
expect_string($key);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument'],
|
|
|
|
],
|
2017-11-08 15:37:58 +01:00
|
|
|
'compact' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function test(): array {
|
2017-11-08 15:37:58 +01:00
|
|
|
return compact(["val"]);
|
|
|
|
}',
|
|
|
|
],
|
2017-11-19 19:05:35 +01:00
|
|
|
'objectLikeKeyChecksAgainstGeneric' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, string> $b
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function a($b): string
|
2017-11-19 19:05:35 +01:00
|
|
|
{
|
|
|
|
return $b["a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
a(["a" => "hello"]);',
|
|
|
|
],
|
|
|
|
'objectLikeKeyChecksAgainstObjectLike' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{a: string} $b
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function a($b): string
|
2017-11-19 19:05:35 +01:00
|
|
|
{
|
|
|
|
return $b["a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
a(["a" => "hello"]);',
|
|
|
|
],
|
2018-01-08 06:09:22 +01:00
|
|
|
'getenv' => [
|
|
|
|
'<?php
|
|
|
|
$a = getenv();
|
|
|
|
$b = getenv("some_key");',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<mixed, string>',
|
|
|
|
'$b' => 'string|false',
|
|
|
|
],
|
|
|
|
],
|
2018-01-13 00:04:11 +01:00
|
|
|
'arrayPopIgnoreNullable' => [
|
|
|
|
'<?php
|
|
|
|
function expectsInt(int $a) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<mixed, array{item:int}> $list
|
|
|
|
*/
|
|
|
|
function test(array $list) : void
|
|
|
|
{
|
|
|
|
while (!empty($list)) {
|
|
|
|
$tmp = array_pop($list);
|
|
|
|
expectsInt($tmp["item"]);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-17 22:07:46 +01:00
|
|
|
'arrayFilterWithAssert' => [
|
|
|
|
'<?php
|
|
|
|
$a = array_filter(
|
|
|
|
[1, "hello", 6, "goodbye"],
|
|
|
|
function ($s): bool {
|
|
|
|
return is_string($s);
|
|
|
|
}
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<int, string>',
|
|
|
|
],
|
|
|
|
'error_levels' => [
|
2018-01-29 02:03:47 +01:00
|
|
|
'MissingClosureParamType',
|
2018-01-17 22:07:46 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayFilterUseKey' => [
|
|
|
|
'<?php
|
|
|
|
$bar = "bar";
|
|
|
|
|
|
|
|
$foo = [
|
|
|
|
$bar => function (): string {
|
|
|
|
return "baz";
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
$foo = array_filter(
|
|
|
|
$foo,
|
|
|
|
function (string $key): bool {
|
|
|
|
return $key === "bar";
|
|
|
|
},
|
|
|
|
ARRAY_FILTER_USE_KEY
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$foo' => 'array<string, Closure>',
|
|
|
|
],
|
|
|
|
],
|
2018-01-25 00:52:58 +01:00
|
|
|
'ignoreFalsableCurrent' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $arr */
|
|
|
|
function foo(array $arr): string {
|
|
|
|
return current($arr);
|
|
|
|
}
|
|
|
|
/** @param string[] $arr */
|
|
|
|
function bar(array $arr): string {
|
|
|
|
$a = current($arr);
|
|
|
|
if ($a === false) {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
function bat(array $arr) {
|
|
|
|
return current($arr);
|
|
|
|
}',
|
|
|
|
],
|
2018-01-25 19:07:36 +01:00
|
|
|
'ignoreFalsableFileGetContents' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
return file_get_contents($s);
|
|
|
|
}
|
|
|
|
function bar(string $s): string {
|
|
|
|
$a = file_get_contents($s);
|
|
|
|
if ($a === false) {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
function bat(string $s) {
|
|
|
|
return file_get_contents($s);
|
|
|
|
}',
|
|
|
|
],
|
2018-02-12 02:56:34 +01:00
|
|
|
'byRefString' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
function fooFoo(array &$v): void {}
|
|
|
|
$function = "fooFoo";
|
|
|
|
$function($arr);
|
|
|
|
if ($arr) {}',
|
|
|
|
],
|
2018-02-16 01:50:50 +01:00
|
|
|
'arraySumEmpty' => [
|
|
|
|
'<?php
|
|
|
|
$foo = array_sum([]) + 1;',
|
|
|
|
'assertions' => [
|
|
|
|
'$foo' => 'numeric',
|
|
|
|
],
|
|
|
|
],
|
2018-02-26 16:39:48 +01:00
|
|
|
'arrayMapObjectLikeAndCallable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-return array{key1:int,key2:int}
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
|
|
|
$v = ["key1"=> 1, "key2"=> "2"];
|
|
|
|
$r = array_map("intval", $v);
|
|
|
|
return $r;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'arrayMapObjectLikeAndClosure' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-return array{key1:int,key2:int}
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
|
|
|
$v = ["key1"=> 1, "key2"=> "2"];
|
|
|
|
$r = array_map(function($i) : int { return intval($i);}, $v);
|
|
|
|
return $r;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MissingClosureParamType',
|
|
|
|
],
|
|
|
|
],
|
2018-03-02 05:33:21 +01:00
|
|
|
'arrayFilterGoodArgs' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public static function bar(int $i) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array_filter([1, 2, 3], "foo");
|
|
|
|
array_filter([1, 2, 3], "A::bar");',
|
|
|
|
],
|
2018-03-02 05:43:52 +01:00
|
|
|
'arrayFilterIgnoreMissingClass' => [
|
|
|
|
'<?php
|
|
|
|
array_filter([1, 2, 3], "A::bar");',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['UndefinedClass'],
|
|
|
|
],
|
|
|
|
'arrayFilterIgnoreMissingMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function bar(int $i) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array_filter([1, 2, 3], "A::foo");',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['UndefinedMethod'],
|
|
|
|
],
|
2018-03-02 06:49:53 +01:00
|
|
|
'validCallables' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function b() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function c() : void {}
|
|
|
|
|
|
|
|
["a", "b"]();
|
|
|
|
"A::b"();
|
|
|
|
"c"();'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-17 02:51:53 +01:00
|
|
|
}
|
2017-02-27 16:52:43 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-27 16:52:43 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-02-27 16:52:43 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'invalidScalarArgument' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo("string");',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mixedArgument' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = "hello";
|
|
|
|
fooFoo($a);',
|
|
|
|
'error_message' => 'MixedArgument',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullArgument' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo(null);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'NullArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'tooFewArguments' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TooFewArguments',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'tooManyArguments' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(int $a): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo(5, "dfd");',
|
2018-02-02 17:26:55 +01:00
|
|
|
'error_message' => 'TooManyArguments - src/somefile.php:3 - Too many arguments for method fooFoo '
|
|
|
|
. '- expecting 1 but saw 2',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-05-25 05:11:40 +02:00
|
|
|
'tooManyArgumentsForConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A { }
|
|
|
|
new A("hello");',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TooManyArguments',
|
2017-05-25 05:11:40 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'typeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A{}
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(B $b): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo(new A());',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeCoercion',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A{}
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @param B[] $b
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function fooFoo(array $b) {}
|
|
|
|
fooFoo([new A()]);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeCoercion',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'duplicateParam' => [
|
|
|
|
'<?php
|
2017-09-02 17:18:56 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
function f($p, $p) {}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DuplicateParam',
|
2018-01-29 02:03:47 +01:00
|
|
|
'error_levels' => ['MissingParamType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidParamDefault' => [
|
|
|
|
'<?php
|
|
|
|
function f(int $p = false) {}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidParamDefault',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidDocblockParamDefault' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int $p
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function f($p = false) {}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidParamDefault',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-09-16 19:16:21 +02:00
|
|
|
'badByRef' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(string &$v): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo("a");',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidPassByReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-12-22 15:21:23 +01:00
|
|
|
'badArrayByRef' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(array &$a): void {}
|
2017-12-22 15:21:23 +01:00
|
|
|
fooFoo([1, 2, 3]);',
|
|
|
|
'error_message' => 'InvalidPassByReference',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'invalidArgAfterCallable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable $callback
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function route($callback) {
|
|
|
|
if (!is_callable($callback)) { }
|
|
|
|
takes_int("string");
|
|
|
|
}
|
2017-05-06 00:53:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function takes_int(int $i) {}',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedArrayAccess',
|
2018-02-07 21:20:47 +01:00
|
|
|
'RedundantConditionGivenDocblockType',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
|
|
|
],
|
2017-10-23 17:47:00 +02:00
|
|
|
'undefinedFunctionInArrayMap' => [
|
|
|
|
'<?php
|
|
|
|
array_map(
|
|
|
|
"undefined_function",
|
|
|
|
[1, 2, 3]
|
|
|
|
);',
|
|
|
|
'error_message' => 'UndefinedFunction',
|
|
|
|
],
|
2017-11-19 19:05:35 +01:00
|
|
|
'objectLikeKeyChecksAgainstDifferentGeneric' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, int> $b
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function a($b): int
|
2017-11-19 19:05:35 +01:00
|
|
|
{
|
|
|
|
return $b["a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
a(["a" => "hello"]);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'objectLikeKeyChecksAgainstDifferentObjectLike' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{a: int} $b
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function a($b): int
|
2017-11-19 19:05:35 +01:00
|
|
|
{
|
|
|
|
return $b["a"];
|
|
|
|
}
|
|
|
|
|
|
|
|
a(["a" => "hello"]);',
|
2018-02-11 05:30:40 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2017-11-19 19:05:35 +01:00
|
|
|
],
|
2018-01-22 06:17:16 +01:00
|
|
|
'possiblyNullFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? function(): void {} : null;
|
|
|
|
$a();',
|
|
|
|
'error_message' => 'PossiblyNullFunctionCall',
|
|
|
|
],
|
|
|
|
'possiblyInvalidFunctionCall' => [
|
|
|
|
'<?php
|
2018-02-12 02:56:34 +01:00
|
|
|
$a = rand(0, 1) ? function(): void {} : 23515;
|
2018-01-22 06:17:16 +01:00
|
|
|
$a();',
|
|
|
|
'error_message' => 'PossiblyInvalidFunctionCall',
|
|
|
|
],
|
2018-03-02 05:33:21 +01:00
|
|
|
'arrayFilterBadArgs' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_filter(["hello"], "foo");',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'arrayFilterTooFewArgs' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i, string $s) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_filter([1, 2, 3], "foo");',
|
|
|
|
'error_message' => 'TooFewArguments',
|
|
|
|
],
|
|
|
|
'arrayMapBadArgs' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_map("foo", ["hello"]);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'arrayMapTooFewArgs' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i, string $s) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_map("foo", [1, 2, 3]);',
|
|
|
|
'error_message' => 'TooFewArguments',
|
|
|
|
],
|
2018-03-02 06:12:41 +01:00
|
|
|
'arrayMapTooManyArgs' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_map("foo", [1, 2, 3]);',
|
|
|
|
'error_message' => 'TooManyArguments',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-27 16:52:43 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|