1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/FunctionCallTest.php

733 lines
24 KiB
PHP
Raw Normal View History

2016-12-12 05:41:11 +01:00
<?php
namespace Psalm\Tests;
class FunctionCallTest extends TestCase
2016-12-12 05:41:11 +01:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
*/
public function providerFileCheckerValidCodeParse()
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02: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' => [
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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 {
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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 {
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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 {
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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 {
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
return $key === "bar";
},
ARRAY_FILTER_USE_KEY
);',
'assertions' => [
'$f' => 'array<string, null|int>',
'$g' => 'array<string, null|int>',
],
],
'typedArrayWithDefault' => [
'<?php
class A {}
/** @param array<A> $a */
2018-01-11 21:50:45 +01:00
function fooFoo(array $a = []): void {
2017-05-27 02:05:57 +02:00
}',
],
'abs' => [
'<?php
$a = abs(-5);
$b = abs(-7.5);',
'assertions' => [
'$a' => 'int',
'$b' => 'int',
],
],
'validDocblockParamDefault' => [
'<?php
/**
* @param int|false $p
* @return void
*/
2017-05-27 02:05:57 +02:00
function f($p = false) {}',
],
'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);',
],
'namespaced' => [
'<?php
namespace A;
/** @return void */
function f(int $p) {}
2017-05-27 02:05:57 +02:00
f(5);',
],
'namespacedRootFunctionCall' => [
'<?php
namespace {
/** @return void */
function foo() { }
}
namespace A\B\C {
foo();
2017-05-27 02:05:57 +02:00
}',
],
'namespacedAliasedFunctionCall' => [
'<?php
namespace Aye {
/** @return void */
function foo() { }
}
namespace Bee {
use Aye as A;
A\foo();
2017-05-27 02:05:57 +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
],
],
'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-05-27 02:05:57 +02:00
'error_levels' => ['MixedArgument'],
],
'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
],
],
'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
],
],
'arrayMerge' => [
'<?php
$d = array_merge(["a", "b", "c"], [1, 2, 3]);',
'assertions' => [
'$d' => 'array{0:string, 1:string, 2:string, 3:int, 4:int, 5:int}',
2017-05-27 02:05:57 +02: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>',
],
],
'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
],
],
'arrayPopMixed' => [
'<?php
/** @var mixed */
$b = ["a" => 5, "c" => 6];
$a = array_pop($b);',
'assertions' => [
'$a' => 'mixed',
'$b' => 'mixed',
],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
],
'uasort' => [
'<?php
uasort(
$manifest,
function ($a, $b) {
return strcmp($a["parent"],$b["parent"]);
}
);',
'assertions' => [],
'error_levels' => [
'MixedArrayAccess',
'MixedArgument',
'MissingClosureParamType',
'MissingClosureReturnType',
],
],
'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',
'RedundantConditionGivenDocblockType',
2017-05-27 02:05:57 +02: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;
}',
],
'extractVarCheck' => [
'<?php
2018-01-11 21:50:45 +01:00
function takesString(string $str): void {}
$foo = null;
$a = ["$foo" => "bar"];
extract($a);
takesString($foo);',
'assertions' => [],
'error_levels' => [
'MixedAssignment',
2017-05-27 02:05:57 +02:00
'MixedArrayAccess',
],
],
'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);',
'assertions' => [
'$a3' => 'array{hi:int, bye:int}',
],
],
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);',
],
'arrayRand' => [
'<?php
$vars = ["x" => "a", "y" => "b"];
$c = array_rand($vars);
$d = $vars[$c];
$more_vars = ["a", "b"];
$e = array_rand($more_vars);',
'assertions' => [
'$vars' => 'array{x:string, y:string}',
'$c' => 'string',
'$d' => 'string',
'$more_vars' => 'array{0:string, 1:string}',
'$e' => 'int',
],
],
'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'],
],
'compact' => [
'<?php
2018-01-11 21:50:45 +01:00
function test(): array {
return compact(["val"]);
}',
],
'objectLikeKeyChecksAgainstGeneric' => [
'<?php
/**
* @param array<string, string> $b
*/
2018-01-11 21:50:45 +01:00
function a($b): string
{
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
{
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',
],
],
'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"]);
}
}',
],
'arrayFilterWithAssert' => [
'<?php
$a = array_filter(
[1, "hello", 6, "goodbye"],
function ($s): bool {
return is_string($s);
}
);',
'assertions' => [
'$a' => 'array<int, string>',
],
'error_levels' => [
'MissingClosureParamType',
],
],
'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>',
],
],
'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);
}',
],
'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);
}',
],
'byRefString' => [
'<?php
$arr = [];
function fooFoo(array &$v): void {}
$function = "fooFoo";
$function($arr);
if ($arr) {}',
],
'arraySumEmpty' => [
'<?php
$foo = array_sum([]) + 1;',
'assertions' => [
'$foo' => 'numeric',
],
],
'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',
],
],
'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");',
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'invalidScalarArgument' => [
'<?php
2018-01-11 21:50:45 +01:00
function fooFoo(int $a): void {}
fooFoo("string");',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScalarArgument',
],
'mixedArgument' => [
'<?php
2018-01-11 21:50:45 +01:00
function fooFoo(int $a): void {}
/** @var mixed */
$a = "hello";
fooFoo($a);',
'error_message' => 'MixedArgument',
2017-05-27 02:05:57 +02:00
'error_levels' => ['MixedAssignment'],
],
'nullArgument' => [
'<?php
2018-01-11 21:50:45 +01:00
function fooFoo(int $a): void {}
fooFoo(null);',
2017-05-27 02:05:57 +02:00
'error_message' => 'NullArgument',
],
'tooFewArguments' => [
'<?php
2018-01-11 21:50:45 +01:00
function fooFoo(int $a): void {}
fooFoo();',
2017-05-27 02:05:57 +02:00
'error_message' => 'TooFewArguments',
],
'tooManyArguments' => [
'<?php
2018-01-11 21:50:45 +01:00
function fooFoo(int $a): void {}
fooFoo(5, "dfd");',
'error_message' => 'TooManyArguments - src/somefile.php:3 - Too many arguments for method fooFoo '
. '- expecting 1 but saw 2',
],
'tooManyArgumentsForConstructor' => [
'<?php
class A { }
new A("hello");',
2017-05-27 02:05:57 +02:00
'error_message' => 'TooManyArguments',
],
'typeCoercion' => [
'<?php
class A {}
class B extends A{}
2018-01-11 21:50:45 +01:00
function fooFoo(B $b): void {}
fooFoo(new A());',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeCoercion',
],
'arrayTypeCoercion' => [
'<?php
class A {}
class B extends A{}
/**
* @param B[] $b
* @return void
*/
function fooFoo(array $b) {}
fooFoo([new A()]);',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeCoercion',
],
'duplicateParam' => [
'<?php
/**
* @return void
*/
function f($p, $p) {}',
2017-05-27 02:05:57 +02:00
'error_message' => 'DuplicateParam',
'error_levels' => ['MissingParamType'],
],
'invalidParamDefault' => [
'<?php
function f(int $p = false) {}',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidParamDefault',
],
'invalidDocblockParamDefault' => [
'<?php
/**
* @param int $p
* @return void
*/
function f($p = false) {}',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidParamDefault',
],
2017-09-16 19:16:21 +02:00
'badByRef' => [
'<?php
2018-01-11 21:50:45 +01:00
function fooFoo(string &$v): void {}
fooFoo("a");',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidPassByReference',
],
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',
],
'invalidArgAfterCallable' => [
'<?php
/**
* @param callable $callback
* @return void
*/
function route($callback) {
if (!is_callable($callback)) { }
takes_int("string");
}
function takes_int(int $i) {}',
'error_message' => 'InvalidScalarArgument',
'error_levels' => [
'MixedAssignment',
2017-05-27 02:05:57 +02:00
'MixedArrayAccess',
'RedundantConditionGivenDocblockType',
2017-05-27 02:05:57 +02:00
],
],
'undefinedFunctionInArrayMap' => [
'<?php
array_map(
"undefined_function",
[1, 2, 3]
);',
'error_message' => 'UndefinedFunction',
],
'objectLikeKeyChecksAgainstDifferentGeneric' => [
'<?php
/**
* @param array<string, int> $b
*/
2018-01-11 21:50:45 +01:00
function a($b): int
{
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
{
return $b["a"];
}
a(["a" => "hello"]);',
'error_message' => 'InvalidArgument',
],
2018-01-22 06:17:16 +01:00
'possiblyNullFunctionCall' => [
'<?php
$a = rand(0, 1) ? function(): void {} : null;
$a();',
'error_message' => 'PossiblyNullFunctionCall',
],
'possiblyInvalidFunctionCall' => [
'<?php
$a = rand(0, 1) ? function(): void {} : 23515;
2018-01-22 06:17:16 +01:00
$a();',
'error_message' => 'PossiblyInvalidFunctionCall',
],
'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',
],
];
}
2016-12-12 05:41:11 +01:00
}