2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class FunctionCallTest extends TestCase
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-02-14 01:16:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
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>',
|
2018-05-18 17:02:50 +02:00
|
|
|
'$e' => 'array<string, int|null>',
|
2018-01-21 16:22:04 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'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' => [
|
2018-05-18 17:02:50 +02:00
|
|
|
'$f' => 'array<string, int|null>',
|
|
|
|
'$g' => 'array<string, int|null>',
|
2018-01-21 16:22:04 +01:00
|
|
|
],
|
|
|
|
],
|
2018-06-15 16:33:51 +02:00
|
|
|
'arrayFilterIgnoreNullable' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return array<int, self|null>
|
|
|
|
*/
|
|
|
|
public function getRows() : array {
|
|
|
|
return [new self, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filter() : void {
|
|
|
|
$arr = array_filter(
|
|
|
|
static::getRows(),
|
|
|
|
function (self $row) : bool {
|
|
|
|
return is_a($row, static::class);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['PossiblyInvalidArgument'],
|
|
|
|
],
|
2019-02-07 19:05:17 +01:00
|
|
|
'arrayFilterAllowTrim' => [
|
|
|
|
'<?php
|
|
|
|
$foo = array_filter(["hello ", " "], "trim");',
|
|
|
|
],
|
2019-04-09 23:22:30 +02:00
|
|
|
'arrayFilterAllowNull' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : array {
|
|
|
|
return array_filter(
|
|
|
|
array_map(
|
|
|
|
/** @return null */
|
|
|
|
function (int $arg) {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}',
|
|
|
|
],
|
2019-06-19 23:42:36 +02:00
|
|
|
'arrayFilterNamedFunction' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<int, DateTimeImmutable|null> $a
|
|
|
|
* @return array<int, DateTimeImmutable>
|
|
|
|
*/
|
|
|
|
function foo(array $a) : array {
|
|
|
|
return array_filter($a, "is_object");
|
|
|
|
}',
|
|
|
|
],
|
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);
|
2018-03-14 23:05:36 +01:00
|
|
|
$b = abs(-7.5);
|
|
|
|
$c = $_GET["c"];
|
|
|
|
$c = is_numeric($c) ? abs($c) : null;',
|
2017-07-15 00:09:25 +02:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
2018-03-14 23:05:36 +01:00
|
|
|
'$b' => 'float',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$c' => 'null|numeric',
|
2017-07-15 00:09:25 +02:00
|
|
|
],
|
2018-03-14 23:05:36 +01:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument'],
|
2017-07-15 00:09:25 +02:00
|
|
|
],
|
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
|
|
|
],
|
2018-11-21 18:38:43 +01:00
|
|
|
'byRefNewString' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
2019-02-20 23:43:12 +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
|
|
|
],
|
2018-11-21 18:38:43 +01:00
|
|
|
'byRefVariableFunctionExistingArray' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
function fooFoo(array &$v): void {}
|
|
|
|
$function = "fooFoo";
|
|
|
|
$function($arr);
|
|
|
|
if ($arr) {}',
|
|
|
|
],
|
|
|
|
'byRefProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
function fooFoo(string &$v): void {}
|
|
|
|
|
|
|
|
fooFoo($a->foo);',
|
|
|
|
],
|
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' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'list<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' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'list<array-key>',
|
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
|
2019-10-09 00:44:46 +02:00
|
|
|
$b = array_values(["a" => 1, "b" => 2]);
|
|
|
|
$c = array_values(["a" => "hello", "b" => "jello"]);',
|
2017-04-25 05:45:02 +02:00
|
|
|
'assertions' => [
|
2019-10-09 04:41:23 +02:00
|
|
|
'$b' => 'non-empty-list<int>',
|
|
|
|
'$c' => 'non-empty-list<string>',
|
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' => [
|
2019-05-05 08:11:52 +02:00
|
|
|
'$c' => 'array<string, int>|false',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayCombineFalse' => [
|
|
|
|
'<?php
|
|
|
|
$c = array_combine(["a", "b"], [1, 2, 3]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$c' => 'array<string, int>|false',
|
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' => [
|
2019-06-16 15:42:34 +02: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
|
|
|
],
|
2019-10-20 21:47:00 +02:00
|
|
|
'arrayMergeListResult' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param list<string> $list
|
|
|
|
* @return list<string>
|
|
|
|
*/
|
|
|
|
function foo(array $list) : array {
|
|
|
|
return array_merge($list, ["test"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $list
|
|
|
|
* @return list<string>
|
|
|
|
*/
|
|
|
|
function bar(array $list) : array {
|
|
|
|
return array_merge($list, ["test"]);
|
|
|
|
}',
|
|
|
|
],
|
2019-03-15 21:39:31 +01:00
|
|
|
'arrayReverseDontPreserveKey' => [
|
2018-01-04 00:22:50 +01:00
|
|
|
'<?php
|
2019-03-15 21:39:31 +01:00
|
|
|
$d = array_reverse(["a", "b", 1, "d" => 4]);',
|
2018-01-04 00:22:50 +01:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$d' => 'non-empty-array<int|string, int|string>',
|
2019-03-15 21:39:31 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayReverseDontPreserveKeyExplicitArg' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_reverse(["a", "b", 1, "d" => 4], false);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$d' => 'non-empty-array<int|string, int|string>',
|
2018-01-04 00:22:50 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayReversePreserveKey' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_reverse(["a", "b", 1], true);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$d' => 'non-empty-array<int, int|string>',
|
2018-01-04 00:22:50 +01:00
|
|
|
],
|
|
|
|
],
|
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
|
|
|
],
|
2019-10-06 21:28:41 +02:00
|
|
|
'arrayDiffIsVariadic' => [
|
|
|
|
'<?php
|
|
|
|
array_diff([], [], [], [], []);',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
|
|
|
'arrayDiffKeyIsVariadic' => [
|
|
|
|
'<?php
|
|
|
|
array_diff_key([], [], [], [], []);',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
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'],
|
|
|
|
],
|
2018-12-19 22:15:19 +01:00
|
|
|
'arrayPopNonEmpty' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if ($a) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}
|
|
|
|
$c = array_pop($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
'$c' => 'int|null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterIsset' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (isset($a["a"])) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCount' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (count($a)) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
2019-11-04 15:21:13 +01:00
|
|
|
'arrayShiftNonEmptyList' => [
|
|
|
|
'<?php
|
|
|
|
/** @param non-empty-list $arr */
|
|
|
|
function type_of_array_shift(array $arr) : int {
|
|
|
|
if (\is_int($arr[0])) {
|
|
|
|
return \array_shift($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}',
|
|
|
|
],
|
2018-12-19 22:15:19 +01:00
|
|
|
'noRedundantConditionAfterArrayObjectCountCheck' => [
|
|
|
|
'<?php
|
|
|
|
/** @var ArrayObject<int, int> */
|
|
|
|
$a = [];
|
|
|
|
$b = 5;
|
|
|
|
if (count($a)) {}',
|
|
|
|
],
|
|
|
|
'noRedundantConditionAfterMixedOrEmptyArrayCountCheck' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : void {
|
|
|
|
$a = json_decode($s) ?: [];
|
|
|
|
if (count($a)) {}
|
|
|
|
if (!count($a)) {}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument'],
|
2018-12-19 22:15:19 +01:00
|
|
|
],
|
|
|
|
'objectLikeArrayAssignmentInConditional' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a["a"] = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($a)) {}
|
|
|
|
if (!count($a)) {}',
|
|
|
|
],
|
|
|
|
'noRedundantConditionAfterCheckingExplodeLength' => [
|
|
|
|
'<?php
|
|
|
|
/** @var string */
|
|
|
|
$s = "hello";
|
|
|
|
$segments = explode(".", $s);
|
|
|
|
if (count($segments) === 1) {}',
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountEqualsOne' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (count($a) === 1) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountSoftEqualsOne' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (count($a) == 1) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountGreaterThanOne' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (count($a) > 0) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountGreaterOrEqualsOne' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (count($a) >= 1) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountEqualsOneReversed' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (1 === count($a)) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountSoftEqualsOneReversed' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (1 == count($a)) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountGreaterThanOneReversed' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (0 < count($a)) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterCountGreatorOrEqualToOneReversed' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$b = 5;
|
|
|
|
if (1 <= count($a)) {
|
|
|
|
$b = array_pop($a);
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterThreeAssertions' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
/** @var array<int, string> */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var array<A> */
|
|
|
|
$replacement_stmts = [];
|
|
|
|
|
|
|
|
if (!$replacement_stmts
|
|
|
|
|| !$replacement_stmts[0] instanceof B
|
|
|
|
|| count($replacement_stmts[0]->arr) > 1
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = $replacement_stmts[0]->arr;',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'array<int, string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterArrayAddition' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, int> */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$a["foo"] = 10;
|
|
|
|
$b = array_pop($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayPopNonEmptyAfterMixedArrayAddition' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array */
|
|
|
|
$a = ["a" => 5, "b" => 6, "c" => 7];
|
|
|
|
$a[] = "hello";
|
|
|
|
$b = array_pop($a);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'mixed|string',
|
2018-12-19 22:15:19 +01:00
|
|
|
],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
2019-02-21 23:17:10 +01:00
|
|
|
'countMoreThan0CanBeInverted' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a[] = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($a) > 0) {
|
|
|
|
exit;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<empty, empty>',
|
|
|
|
],
|
|
|
|
],
|
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',
|
2018-04-07 21:16:46 +02:00
|
|
|
'MixedArgument',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
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' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a3' => 'array{bye: int, hi: int}',
|
2017-11-11 20:19:45 +01:00
|
|
|
],
|
2017-05-06 00:53:45 +02:00
|
|
|
],
|
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' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$vars' => 'array{x: string, y: string}',
|
2017-11-07 23:38:54 +01:00
|
|
|
'$c' => 'string',
|
|
|
|
'$d' => 'string',
|
2019-06-16 15:42:34 +02:00
|
|
|
'$more_vars' => 'array{0: string, 1: string}',
|
2017-12-19 00:47:17 +01:00
|
|
|
'$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' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$vars' => 'array{x: string, y: string}',
|
2017-11-09 03:56:54 +01:00
|
|
|
'$c' => 'string',
|
2019-10-09 19:51:25 +02:00
|
|
|
'$e' => 'list<string>',
|
|
|
|
'$f' => 'list<string>|string',
|
2017-11-09 03:56:54 +01:00
|
|
|
],
|
|
|
|
],
|
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' => [],
|
2019-05-28 17:54:07 +02:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument', 'MixedArgumentTypeCoercion'],
|
2017-12-10 22:17:27 +01:00
|
|
|
],
|
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' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'$a' => 'array<array-key, string>',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'false|string',
|
2018-01-08 06:09:22 +01:00
|
|
|
],
|
|
|
|
],
|
2018-12-19 22:15:19 +01:00
|
|
|
'arrayPopNotNullable' => [
|
2018-01-13 00:04:11 +01:00
|
|
|
'<?php
|
|
|
|
function expectsInt(int $a) : void {}
|
|
|
|
|
|
|
|
/**
|
2019-01-05 06:15:53 +01:00
|
|
|
* @param array<array-key, array{item:int}> $list
|
2018-01-13 00:04:11 +01:00
|
|
|
*/
|
|
|
|
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' => [
|
2018-12-08 20:10:06 +01:00
|
|
|
'$foo' => 'array<string, Closure():string(baz)>',
|
2018-01-17 22:07:46 +01:00
|
|
|
],
|
|
|
|
],
|
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
|
2019-10-17 07:14:33 +02:00
|
|
|
* @return false|string
|
2018-01-25 00:52:58 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/**
|
2019-10-17 07:14:33 +02:00
|
|
|
* @return false|string
|
2018-01-25 19:07:36 +01:00
|
|
|
*/
|
|
|
|
function bat(string $s) {
|
|
|
|
return file_get_contents($s);
|
|
|
|
}',
|
|
|
|
],
|
2018-02-16 01:50:50 +01:00
|
|
|
'arraySumEmpty' => [
|
|
|
|
'<?php
|
|
|
|
$foo = array_sum([]) + 1;',
|
|
|
|
'assertions' => [
|
2018-03-27 07:05:37 +02:00
|
|
|
'$foo' => 'float|int',
|
2018-02-16 01:50:50 +01:00
|
|
|
],
|
|
|
|
],
|
2018-02-26 16:39:48 +01:00
|
|
|
'arrayMapObjectLikeAndCallable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-return array{key1:int,key2:int}
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
2019-01-28 23:54:50 +01:00
|
|
|
$v = ["key1"=> 1, "key2"=> "2"];
|
|
|
|
$r = array_map("intval", $v);
|
|
|
|
return $r;
|
2018-02-26 16:39:48 +01:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'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-04-10 21:47:04 +02:00
|
|
|
'MixedTypeCoercion',
|
2018-02-26 16:39:48 +01:00
|
|
|
],
|
|
|
|
],
|
2018-03-02 05:33:21 +01:00
|
|
|
'arrayFilterGoodArgs' => [
|
|
|
|
'<?php
|
2018-03-05 15:01:24 +01:00
|
|
|
function fooFoo(int $i) : bool {
|
2018-03-02 05:33:21 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
2018-03-05 15:01:24 +01:00
|
|
|
public static function barBar(int $i) : bool {
|
2018-03-02 05:33:21 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 15:01:24 +01:00
|
|
|
array_filter([1, 2, 3], "fooFoo");
|
|
|
|
array_filter([1, 2, 3], "foofoo");
|
|
|
|
array_filter([1, 2, 3], "FOOFOO");
|
|
|
|
array_filter([1, 2, 3], "A::barBar");
|
|
|
|
array_filter([1, 2, 3], "A::BARBAR");
|
|
|
|
array_filter([1, 2, 3], "A::barbar");',
|
2018-03-02 05:33:21 +01:00
|
|
|
],
|
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"();
|
2019-03-23 19:27:54 +01:00
|
|
|
"c"();',
|
2018-03-02 06:49:53 +01:00
|
|
|
],
|
2018-03-05 16:29:19 +01:00
|
|
|
'arrayMapParamDefault' => [
|
|
|
|
'<?php
|
|
|
|
$arr = ["a", "b"];
|
|
|
|
array_map("mapdef", $arr, array_fill(0, count($arr), 1));
|
|
|
|
function mapdef(string $_a, int $_b = 0): string {
|
|
|
|
return "a";
|
|
|
|
}',
|
|
|
|
],
|
2018-03-08 20:04:00 +01:00
|
|
|
'noInvalidOperandForCoreFunctions' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a, string $b) : int {
|
|
|
|
$aTime = strtotime($a);
|
|
|
|
$bTime = strtotime($b);
|
|
|
|
|
|
|
|
return $aTime - $bTime;
|
|
|
|
}',
|
|
|
|
],
|
2018-03-15 22:32:03 +01:00
|
|
|
'strposIntSecondParam' => [
|
|
|
|
'<?php
|
|
|
|
function hasZeroByteOffset(string $s) : bool {
|
|
|
|
return strpos($s, 0) !== false;
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-03-15 22:32:03 +01:00
|
|
|
],
|
2018-03-18 22:11:14 +01:00
|
|
|
'functionCallInGlobalScope' => [
|
|
|
|
'<?php
|
|
|
|
$a = function() use ($argv) : void {};',
|
|
|
|
],
|
2018-03-31 00:51:59 +02:00
|
|
|
'implodeMultiDimensionalArray' => [
|
2018-03-27 04:13:10 +02:00
|
|
|
'<?php
|
|
|
|
$urls = array_map("implode", [["a", "b"]]);',
|
|
|
|
],
|
2018-04-03 04:19:58 +02:00
|
|
|
'varExport' => [
|
|
|
|
'<?php
|
|
|
|
$a = var_export(["a"], true);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2019-01-21 22:55:50 +01:00
|
|
|
'varExportConstFetch' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
const BOOL_VAR_EXPORT_RETURN = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $mixed
|
|
|
|
*/
|
|
|
|
public static function Baz($mixed) : string {
|
|
|
|
return var_export($mixed, self::BOOL_VAR_EXPORT_RETURN);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-04-05 06:21:14 +02:00
|
|
|
'key' => [
|
|
|
|
'<?php
|
2018-04-05 19:57:01 +02:00
|
|
|
$a = ["one" => 1, "two" => 3];
|
|
|
|
$b = key($a);
|
|
|
|
$c = $a[$b];',
|
2018-04-05 06:21:14 +02:00
|
|
|
'assertions' => [
|
2018-07-23 01:15:40 +02:00
|
|
|
'$b' => 'null|string',
|
2018-04-05 19:57:01 +02:00
|
|
|
'$c' => 'int',
|
2018-04-05 06:21:14 +02:00
|
|
|
],
|
|
|
|
],
|
2019-11-02 10:10:38 +01:00
|
|
|
'explode' => [
|
2018-04-16 22:03:04 +02:00
|
|
|
'<?php
|
2019-11-02 10:10:38 +01:00
|
|
|
/** @var string $string */
|
|
|
|
$elements = explode(" ", $string);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'non-empty-list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithPositiveLimit' => [
|
|
|
|
'<?php
|
|
|
|
/** @var string $string */
|
|
|
|
$elements = explode(" ", $string, 5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'non-empty-list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithNegativeLimit' => [
|
|
|
|
'<?php
|
|
|
|
/** @var string $string */
|
|
|
|
$elements = explode(" ", $string, -5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithDynamicLimit' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var string $string
|
|
|
|
* @var int $limit
|
|
|
|
*/
|
|
|
|
$elements = explode(" ", $string, $limit);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithDynamicDelimiter' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var string $delim
|
|
|
|
* @var string $string
|
|
|
|
*/
|
|
|
|
$elements = explode($delim, $string);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'false|non-empty-list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithDynamicDelimiterAndPositiveLimit' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var string $delim
|
|
|
|
* @var string $string
|
|
|
|
*/
|
|
|
|
$elements = explode($delim, $string, 5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'false|non-empty-list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithDynamicDelimiterAndNegativeLimit' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var string $delim
|
|
|
|
* @var string $string
|
|
|
|
*/
|
|
|
|
$elements = explode($delim, $string, -5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'false|list<string>',
|
|
|
|
],
|
2018-04-16 22:03:04 +02:00
|
|
|
],
|
2019-11-02 10:10:38 +01:00
|
|
|
'explodeWithDynamicDelimiterAndLimit' => [
|
2019-07-17 20:13:28 +02:00
|
|
|
'<?php
|
2019-11-02 10:10:38 +01:00
|
|
|
/**
|
|
|
|
* @var string $delim
|
|
|
|
* @var string $string
|
|
|
|
* @var int $limit
|
|
|
|
*/
|
|
|
|
$elements = explode($delim, $string, $limit);',
|
|
|
|
'assertions' => [
|
|
|
|
'$elements' => 'false|list<string>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'explodeWithPossiblyFalse' => [
|
|
|
|
'<?php
|
|
|
|
/** @return non-empty-list<string> */
|
|
|
|
function exploder(string $d, string $s) : array {
|
|
|
|
return explode($d, $s);
|
|
|
|
}',
|
2019-07-17 20:13:28 +02:00
|
|
|
],
|
2018-04-19 18:16:00 +02:00
|
|
|
'allowPossiblyUndefinedClassInClassExists' => [
|
|
|
|
'<?php
|
2019-03-23 19:27:54 +01:00
|
|
|
if (class_exists(Foo::class)) {}',
|
2018-04-19 18:16:00 +02:00
|
|
|
],
|
2018-11-12 18:03:55 +01:00
|
|
|
'allowConstructorAfterClassExists' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : void {
|
|
|
|
if (class_exists($s)) {
|
|
|
|
new $s();
|
|
|
|
}
|
2019-01-02 12:58:49 +01:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedMethodCall'],
|
2018-11-12 18:03:55 +01:00
|
|
|
],
|
2018-05-14 01:20:05 +02:00
|
|
|
'next' => [
|
|
|
|
'<?php
|
|
|
|
$arr = ["one", "two", "three"];
|
|
|
|
$n = next($arr);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$n' => 'false|string',
|
2018-05-14 01:20:05 +02:00
|
|
|
],
|
|
|
|
],
|
2018-05-19 00:10:10 +02:00
|
|
|
'iteratorToArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return Generator<stdClass>
|
|
|
|
*/
|
|
|
|
function generator(): Generator {
|
|
|
|
yield new stdClass;
|
|
|
|
}
|
|
|
|
|
2019-01-27 20:50:05 +01:00
|
|
|
$a = iterator_to_array(generator());',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<mixed, stdClass>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'iteratorToArrayWithGetIterator' => [
|
|
|
|
'<?php
|
|
|
|
class C implements IteratorAggregate {
|
|
|
|
/**
|
|
|
|
* @return Traversable<int,string>
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
|
|
|
yield 1 => "1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$a = iterator_to_array(new C);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<int, string>',
|
|
|
|
],
|
2018-05-19 00:10:10 +02:00
|
|
|
],
|
2019-10-11 05:47:11 +02:00
|
|
|
'iteratorToArrayWithGetIteratorReturningList' => [
|
|
|
|
'<?php
|
|
|
|
class C implements IteratorAggregate {
|
|
|
|
/**
|
|
|
|
* @return Traversable<int,string>
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
|
|
|
yield 1 => "1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$a = iterator_to_array(new C, false);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'list<string>',
|
|
|
|
],
|
|
|
|
],
|
2018-06-01 03:14:06 +02:00
|
|
|
'arrayColumnInference' => [
|
|
|
|
'<?php
|
|
|
|
function makeMixedArray(): array { return []; }
|
|
|
|
/** @return array<array<int,bool>> */
|
|
|
|
function makeGenericArray(): array { return []; }
|
|
|
|
/** @return array<array{0:string}> */
|
|
|
|
function makeShapeArray(): array { return []; }
|
|
|
|
/** @return array<array{0:string}|int> */
|
|
|
|
function makeUnionArray(): array { return []; }
|
|
|
|
$a = array_column([[1], [2], [3]], 0);
|
|
|
|
$b = array_column([["a" => 1], ["a" => 2], ["a" => 3]], "a");
|
|
|
|
$c = array_column([["k" => "a", "v" => 1], ["k" => "b", "v" => 2]], "v", "k");
|
|
|
|
$d = array_column([], 0);
|
|
|
|
$e = array_column(makeMixedArray(), 0);
|
|
|
|
$f = array_column(makeGenericArray(), 0);
|
|
|
|
$g = array_column(makeShapeArray(), 0);
|
|
|
|
$h = array_column(makeUnionArray(), 0);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2019-01-05 06:15:53 +01:00
|
|
|
'$a' => 'array<array-key, int>',
|
|
|
|
'$b' => 'array<array-key, int>',
|
2018-06-01 03:14:06 +02:00
|
|
|
'$c' => 'array<string, int>',
|
2019-01-05 06:15:53 +01:00
|
|
|
'$d' => 'array<array-key, mixed>',
|
|
|
|
'$e' => 'array<array-key, mixed>',
|
|
|
|
'$f' => 'array<array-key, mixed>',
|
|
|
|
'$g' => 'array<array-key, string>',
|
|
|
|
'$h' => 'array<array-key, mixed>',
|
2018-06-01 03:14:06 +02:00
|
|
|
],
|
|
|
|
],
|
2018-08-09 05:28:30 +02:00
|
|
|
'strtrWithPossiblyFalseFirstArg' => [
|
2018-06-20 15:09:03 +02:00
|
|
|
'<?php
|
2018-08-09 05:28:30 +02:00
|
|
|
/**
|
2019-10-17 07:14:33 +02:00
|
|
|
* @param false|string $str
|
2018-08-09 05:28:30 +02:00
|
|
|
* @param array<string, string> $replace_pairs
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function strtr_wrapper($str, array $replace_pairs) {
|
|
|
|
/** @psalm-suppress PossiblyFalseArgument */
|
|
|
|
return strtr($str, $replace_pairs);
|
|
|
|
}',
|
2018-06-20 15:09:03 +02:00
|
|
|
],
|
2018-06-20 16:40:50 +02:00
|
|
|
'splatArrayIntersect' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [
|
|
|
|
[1, 2, 3],
|
|
|
|
[1, 2],
|
|
|
|
];
|
|
|
|
|
|
|
|
$bar = array_intersect(... $foo);',
|
|
|
|
'assertions' => [
|
|
|
|
'$bar' => 'array<int, int>',
|
|
|
|
],
|
|
|
|
],
|
2019-10-06 21:28:41 +02:00
|
|
|
'arrayIntersectIsVariadic' => [
|
|
|
|
'<?php
|
|
|
|
array_intersect([], [], [], [], []);',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
|
|
|
'arrayIntersectKeyIsVariadic' => [
|
|
|
|
'<?php
|
|
|
|
array_intersect_key([], [], [], [], []);',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
2018-06-28 22:01:53 +02:00
|
|
|
'arrayReduce' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [2, 3, 4, 5];
|
|
|
|
|
|
|
|
function multiply (int $carry, int $item) : int {
|
|
|
|
return $carry * $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
$f2 = function (int $carry, int $item) : int {
|
|
|
|
return $carry * $item;
|
|
|
|
};
|
|
|
|
|
|
|
|
$direct_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
function (int $carry, int $item) : int {
|
|
|
|
return $carry * $item;
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
$passed_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
$f2,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
$function_call_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
"multiply",
|
|
|
|
1
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$direct_closure_result' => 'int',
|
|
|
|
'$passed_closure_result' => 'int',
|
|
|
|
'$function_call_result' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayReduceMixedReturn' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [2, 3, 4, 5];
|
|
|
|
|
|
|
|
$direct_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
function (int $carry, int $item) {
|
|
|
|
return $_GET["boo"];
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MissingClosureReturnType', 'MixedAssignment'],
|
|
|
|
],
|
2018-07-06 19:09:05 +02:00
|
|
|
'versionCompare' => [
|
|
|
|
'<?php
|
|
|
|
function getString() : string {
|
|
|
|
return rand(0, 1) ? "===" : "==";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = version_compare("5.0.0", "7.0.0");
|
|
|
|
$b = version_compare("5.0.0", "7.0.0", "==");
|
|
|
|
$c = version_compare("5.0.0", "7.0.0", getString());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'bool',
|
|
|
|
'$c' => 'bool|null',
|
|
|
|
],
|
|
|
|
],
|
2018-07-08 02:35:24 +02:00
|
|
|
'getTimeOfDay' => [
|
|
|
|
'<?php
|
|
|
|
$a = gettimeofday(true) - gettimeofday(true);
|
|
|
|
$b = gettimeofday();
|
|
|
|
$c = gettimeofday(false);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'float',
|
|
|
|
'$b' => 'array<string, int>',
|
|
|
|
'$c' => 'array<string, int>',
|
|
|
|
],
|
|
|
|
],
|
2018-07-09 20:37:14 +02:00
|
|
|
'parseUrlArray' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : string {
|
|
|
|
return parse_url($s)["host"] ?? "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $s) : string {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
return $parsed["host"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function baz(string $s) : string {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
return $parsed["host"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function bag(string $s) : string {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
if (is_string($parsed["host"] ?? false)) {
|
|
|
|
return $parsed["host"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function hereisanotherone(string $s) : string {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
if (isset($parsed["host"]) && is_string($parsed["host"])) {
|
|
|
|
return $parsed["host"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function hereisthelastone(string $s) : string {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
if (isset($parsed["host"]) && is_string($parsed["host"])) {
|
|
|
|
return $parsed["host"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2018-07-10 06:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function portisint(string $s) : int {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
if (isset($parsed["port"])) {
|
|
|
|
return $parsed["port"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 80;
|
|
|
|
}
|
|
|
|
|
|
|
|
function portismaybeint(string $s) : ? int {
|
|
|
|
$parsed = parse_url($s);
|
|
|
|
|
|
|
|
return $parsed["port"] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$porta = parse_url("", PHP_URL_PORT);
|
|
|
|
$porte = parse_url("localhost:443", PHP_URL_PORT);',
|
|
|
|
'assertions' => [
|
|
|
|
'$porta' => 'int|null',
|
|
|
|
'$porte' => 'int|null',
|
|
|
|
],
|
2018-07-09 20:37:14 +02:00
|
|
|
'error_levels' => ['MixedReturnStatement', 'MixedInferredReturnType'],
|
|
|
|
],
|
|
|
|
'parseUrlComponent' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : string {
|
|
|
|
return parse_url($s, PHP_URL_HOST) ?? "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $s) : string {
|
|
|
|
return parse_url($s, PHP_URL_HOST);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bag(string $s) : string {
|
|
|
|
$host = parse_url($s, PHP_URL_HOST);
|
|
|
|
|
|
|
|
if (is_string($host)) {
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}',
|
|
|
|
],
|
2018-07-14 00:36:25 +02:00
|
|
|
'triggerUserError' => [
|
|
|
|
'<?php
|
|
|
|
function mightLeave() : string {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
trigger_error("bad", E_USER_ERROR);
|
|
|
|
} else {
|
|
|
|
return "here";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-08-02 20:43:13 +02:00
|
|
|
'getParentClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = get_parent_class(new A());
|
|
|
|
if ($b === false) {}
|
|
|
|
$c = new $b();',
|
2019-01-02 12:58:49 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedMethodCall'],
|
2018-08-02 20:43:13 +02:00
|
|
|
],
|
2018-08-09 04:44:02 +02:00
|
|
|
'arraySplice' => [
|
|
|
|
'<?php
|
|
|
|
$a = [1, 2, 3];
|
|
|
|
$c = $a;
|
|
|
|
$b = ["a", "b", "c"];
|
2018-08-24 22:12:07 +02:00
|
|
|
array_splice($a, -1, 1, $b);
|
|
|
|
$d = [1, 2, 3];
|
|
|
|
array_splice($d, -1, 1);',
|
2018-08-09 04:44:02 +02:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'non-empty-array<int, int|string>',
|
2019-06-16 15:42:34 +02:00
|
|
|
'$b' => 'array{0: string, 1: string, 2: string}',
|
|
|
|
'$c' => 'array{0: int, 1: int, 2: int}',
|
2018-08-09 04:44:02 +02:00
|
|
|
],
|
|
|
|
],
|
2018-08-14 17:51:17 +02:00
|
|
|
'arraySpliceOtherType' => [
|
|
|
|
'<?php
|
|
|
|
$d = [["red"], ["green"], ["blue"]];
|
|
|
|
array_splice($d, -1, 1, "foo");',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$d' => 'array<int, array{0: string}|string>',
|
2018-08-14 17:51:17 +02:00
|
|
|
],
|
|
|
|
],
|
2018-09-04 19:14:44 +02:00
|
|
|
'ksortPreserveShape' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["a" => 3, "b" => 4];
|
|
|
|
ksort($a);
|
|
|
|
acceptsAShape($a);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array{a:int,b:int} $a
|
|
|
|
*/
|
|
|
|
function acceptsAShape(array $a): void {}',
|
|
|
|
],
|
2018-09-04 19:57:05 +02:00
|
|
|
'suppressError' => [
|
|
|
|
'<?php
|
|
|
|
$a = @file_get_contents("foo");',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'false|string',
|
2018-09-04 19:57:05 +02:00
|
|
|
],
|
|
|
|
],
|
2018-09-07 16:53:56 +02:00
|
|
|
'arraySlicePreserveKeys' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["a" => 1, "b" => 2, "c" => 3];
|
|
|
|
$b = array_slice($a, 1, 2, true);
|
|
|
|
$c = array_slice($a, 1, 2, false);
|
|
|
|
$d = array_slice($a, 1, 2);',
|
|
|
|
'assertions' => [
|
2019-10-20 22:19:01 +02:00
|
|
|
'$b' => 'array<string, int>',
|
|
|
|
'$c' => 'array<string, int>',
|
|
|
|
'$d' => 'array<string, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'arraySliceDontPreserveIntKeys' => [
|
|
|
|
'<?php
|
|
|
|
$a = [1 => "a", 4 => "b", 3 => "c"];
|
|
|
|
$b = array_slice($a, 1, 2, true);
|
|
|
|
$c = array_slice($a, 1, 2, false);
|
|
|
|
$d = array_slice($a, 1, 2);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'array<int, string>',
|
|
|
|
'$c' => 'list<string>',
|
|
|
|
'$d' => 'list<string>',
|
2018-09-07 16:53:56 +02:00
|
|
|
],
|
|
|
|
],
|
2018-10-10 16:58:47 +02:00
|
|
|
'printrOutput' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : void {
|
|
|
|
echo $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(print_r(1, true));',
|
|
|
|
],
|
2018-10-10 22:03:00 +02:00
|
|
|
'microtime' => [
|
|
|
|
'<?php
|
|
|
|
$a = microtime(true);
|
|
|
|
$b = microtime();
|
2019-06-30 17:14:05 +02:00
|
|
|
/** @psalm-suppress InvalidScalarArgument */
|
2018-10-10 22:03:00 +02:00
|
|
|
$c = microtime(1);
|
|
|
|
$d = microtime(false);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'float',
|
|
|
|
'$b' => 'string',
|
|
|
|
'$c' => 'float|string',
|
|
|
|
'$d' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2018-10-23 20:38:36 +02:00
|
|
|
'filterVar' => [
|
|
|
|
'<?php
|
|
|
|
function filterInt(string $s) : int {
|
|
|
|
$filtered = filter_var($s, FILTER_VALIDATE_INT);
|
|
|
|
if ($filtered === false) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return $filtered;
|
|
|
|
}
|
|
|
|
function filterNullableInt(string $s) : ?int {
|
2019-01-02 16:04:01 +01:00
|
|
|
return filter_var($s, FILTER_VALIDATE_INT, ["options" => ["default" => null]]);
|
2018-10-23 20:38:36 +02:00
|
|
|
}
|
|
|
|
function filterIntWithDefault(string $s) : int {
|
2019-01-02 16:04:01 +01:00
|
|
|
return filter_var($s, FILTER_VALIDATE_INT, ["options" => ["default" => 5]]);
|
2018-10-23 20:38:36 +02:00
|
|
|
}
|
|
|
|
function filterBool(string $s) : bool {
|
|
|
|
return filter_var($s, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
}
|
|
|
|
function filterNullableBool(string $s) : ?bool {
|
|
|
|
return filter_var($s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
|
|
|
}
|
|
|
|
function filterNullableBoolWithFlagsArray(string $s) : ?bool {
|
|
|
|
return filter_var($s, FILTER_VALIDATE_BOOLEAN, ["flags" => FILTER_NULL_ON_FAILURE]);
|
|
|
|
}
|
|
|
|
function filterFloat(string $s) : float {
|
|
|
|
$filtered = filter_var($s, FILTER_VALIDATE_FLOAT);
|
|
|
|
if ($filtered === false) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
return $filtered;
|
|
|
|
}
|
|
|
|
function filterFloatWithDefault(string $s) : float {
|
2019-01-02 16:04:01 +01:00
|
|
|
return filter_var($s, FILTER_VALIDATE_FLOAT, ["options" => ["default" => 5.0]]);
|
2018-10-23 20:38:36 +02:00
|
|
|
}',
|
|
|
|
],
|
2019-01-02 15:00:45 +01:00
|
|
|
'callVariableVar' => [
|
|
|
|
'<?php
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public static function someInt(): int
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function makeInt()
|
|
|
|
{
|
|
|
|
$fooClass = Foo::class;
|
|
|
|
return $fooClass::someInt();
|
|
|
|
}',
|
|
|
|
],
|
2019-01-04 20:54:40 +01:00
|
|
|
'expectsIterable' => [
|
|
|
|
'<?php
|
|
|
|
function foo(iterable $i) : void {}
|
|
|
|
function bar(array $a) : void {
|
|
|
|
foo($a);
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 22:23:18 +01:00
|
|
|
'getTypeHasValues' => [
|
2019-01-05 20:50:11 +01:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $maybe
|
|
|
|
*/
|
|
|
|
function matchesTypes($maybe) : void {
|
|
|
|
$t = gettype($maybe);
|
|
|
|
if ($t === "object") {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-05 20:50:11 +01:00
|
|
|
],
|
|
|
|
'functionResolutionInNamespace' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
function sort(int $_) : void {}
|
2019-03-23 19:27:54 +01:00
|
|
|
sort(5);',
|
2019-01-05 20:50:11 +01:00
|
|
|
],
|
2019-01-06 19:02:46 +01:00
|
|
|
'rangeWithIntStep' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(int $bar) : string {
|
|
|
|
return (string) $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (range(1, 10, 1) as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'rangeWithNoStep' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(int $bar) : string {
|
|
|
|
return (string) $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (range(1, 10) as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
],
|
2019-02-07 16:50:42 +01:00
|
|
|
'rangeWithNoStepAndString' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(string $bar) : void {}
|
|
|
|
|
|
|
|
foreach (range("a", "z") as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
],
|
2019-01-06 19:02:46 +01:00
|
|
|
'rangeWithFloatStep' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(float $bar) : string {
|
|
|
|
return (string) $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (range(1, 10, .3) as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'rangeWithFloatStart' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(float $bar) : string {
|
|
|
|
return (string) $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (range(1.5, 10) as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
],
|
2019-01-06 22:40:44 +01:00
|
|
|
'duplicateNamespacedFunction' => [
|
|
|
|
'<?php
|
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
function sort() : void {}',
|
|
|
|
],
|
2019-01-15 23:53:23 +01:00
|
|
|
'arrayMapAfterFunctionMissingFile' => [
|
|
|
|
'<?php
|
|
|
|
require_once(FOO);
|
|
|
|
$urls = array_map("strval", [1, 2, 3]);',
|
|
|
|
[],
|
|
|
|
'error_levels' => ['UndefinedConstant', 'UnresolvableInclude'],
|
|
|
|
],
|
2019-01-24 16:41:06 +01:00
|
|
|
'noNamespaceClash' => [
|
|
|
|
'<?php
|
|
|
|
namespace FunctionNamespace {
|
|
|
|
function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ClassNamespace {
|
|
|
|
class Foo {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
use ClassNamespace\Foo;
|
|
|
|
use function FunctionNamespace\foo;
|
|
|
|
|
|
|
|
new Foo();
|
|
|
|
|
|
|
|
foo();
|
|
|
|
}',
|
|
|
|
],
|
2019-01-28 17:57:49 +01:00
|
|
|
'round' => [
|
|
|
|
'<?php
|
|
|
|
$a = round(4.6);
|
|
|
|
$b = round(3.6, 0);
|
|
|
|
$c = round(3.0, 1);
|
|
|
|
$d = round(3.1, 2);
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
$sig = 1;
|
|
|
|
$e = round(3.1, $sig);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'int',
|
|
|
|
'$c' => 'float',
|
|
|
|
'$d' => 'float',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$e' => 'float|int',
|
2019-01-28 17:57:49 +01:00
|
|
|
],
|
2019-02-07 18:25:57 +01:00
|
|
|
],
|
|
|
|
'hashInit70' => [
|
|
|
|
'<?php
|
|
|
|
$h = hash_init("sha256");',
|
|
|
|
[
|
|
|
|
'$h' => 'resource',
|
|
|
|
],
|
|
|
|
[],
|
2019-03-23 19:27:54 +01:00
|
|
|
'7.1',
|
2019-02-07 18:25:57 +01:00
|
|
|
],
|
|
|
|
'hashInit71' => [
|
|
|
|
'<?php
|
|
|
|
$h = hash_init("sha256");',
|
|
|
|
[
|
|
|
|
'$h' => 'resource',
|
|
|
|
],
|
|
|
|
[],
|
2019-03-23 19:27:54 +01:00
|
|
|
'7.1',
|
2019-02-07 18:25:57 +01:00
|
|
|
],
|
|
|
|
'hashInit72' => [
|
|
|
|
'<?php
|
|
|
|
$h = hash_init("sha256");',
|
|
|
|
[
|
|
|
|
'$h' => 'HashContext',
|
|
|
|
],
|
|
|
|
[],
|
2019-03-23 19:27:54 +01:00
|
|
|
'7.2',
|
2019-02-07 18:25:57 +01:00
|
|
|
],
|
|
|
|
'hashInit73' => [
|
|
|
|
'<?php
|
|
|
|
$h = hash_init("sha256");',
|
|
|
|
[
|
|
|
|
'$h' => 'HashContext',
|
|
|
|
],
|
|
|
|
[],
|
2019-03-23 19:27:54 +01:00
|
|
|
'7.3',
|
2019-02-20 23:43:12 +01:00
|
|
|
],
|
|
|
|
'nullableByRef' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string &$s) : void {}
|
|
|
|
|
|
|
|
function bar() : void {
|
|
|
|
foo($bar);
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-20 23:43:12 +01:00
|
|
|
],
|
2019-03-03 00:02:11 +01:00
|
|
|
'getClassNewInstance' => [
|
|
|
|
'<?php
|
|
|
|
interface I {}
|
|
|
|
class C implements I {}
|
|
|
|
|
|
|
|
class Props {
|
|
|
|
/** @var class-string<I>[] */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
(new Props)->arr[] = get_class(new C);',
|
2019-03-03 00:02:11 +01:00
|
|
|
],
|
|
|
|
'getClassVariable' => [
|
|
|
|
'<?php
|
|
|
|
interface I {}
|
|
|
|
class C implements I {}
|
|
|
|
$c_instance = new C;
|
|
|
|
|
|
|
|
class Props {
|
|
|
|
/** @var class-string<I>[] */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
(new Props)->arr[] = get_class($c_instance);',
|
2019-03-03 00:02:11 +01:00
|
|
|
],
|
|
|
|
'getClassAnonymousNewInstance' => [
|
|
|
|
'<?php
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
class Props {
|
|
|
|
/** @var class-string<I>[] */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
(new Props)->arr[] = get_class(new class implements I{});',
|
2019-03-03 00:02:11 +01:00
|
|
|
],
|
|
|
|
'getClassAnonymousVariable' => [
|
|
|
|
'<?php
|
|
|
|
interface I {}
|
|
|
|
$anon_instance = new class implements I {};
|
|
|
|
|
|
|
|
class Props {
|
|
|
|
/** @var class-string<I>[] */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
(new Props)->arr[] = get_class($anon_instance);',
|
2019-03-03 00:02:11 +01:00
|
|
|
],
|
2019-03-15 21:39:31 +01:00
|
|
|
'arrayReversePreserveNonEmptiness' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $arr */
|
|
|
|
function getOrderings(array $arr): int {
|
|
|
|
if ($arr) {
|
|
|
|
$next = null;
|
|
|
|
foreach (array_reverse($arr) as $v) {
|
|
|
|
$next = 1;
|
|
|
|
}
|
|
|
|
return $next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}',
|
|
|
|
],
|
2019-03-29 15:46:40 +01:00
|
|
|
'mktime' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress InvalidScalarArgument */
|
|
|
|
$a = mktime("foo");
|
|
|
|
/** @psalm-suppress MixedArgument */
|
|
|
|
$b = mktime($_GET["foo"]);
|
|
|
|
$c = mktime(1, 2, 3);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'false|int',
|
|
|
|
'$b' => 'false|int',
|
2019-03-29 15:46:40 +01:00
|
|
|
'$c' => 'int',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-03-29 15:46:40 +01:00
|
|
|
],
|
2019-04-03 23:08:37 +02:00
|
|
|
'PHP73-hrtime' => [
|
|
|
|
'<?php
|
|
|
|
$a = hrtime(true);
|
|
|
|
$b = hrtime();
|
2019-09-08 20:44:59 +02:00
|
|
|
/** @psalm-suppress InvalidScalarArgument */
|
2019-04-03 23:08:37 +02:00
|
|
|
$c = hrtime(1);
|
|
|
|
$d = hrtime(false);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
2019-06-16 15:42:34 +02:00
|
|
|
'$b' => 'array{0: int, 1: int}',
|
|
|
|
'$c' => 'array{0: int, 1: int}|int',
|
|
|
|
'$d' => 'array{0: int, 1: int}',
|
2019-04-03 23:08:37 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'PHP73-hrtimeCanBeFloat' => [
|
|
|
|
'<?php
|
|
|
|
$a = hrtime(true);
|
|
|
|
|
|
|
|
if (is_int($a)) {}
|
|
|
|
if (is_float($a)) {}',
|
|
|
|
],
|
2019-04-09 20:48:51 +02:00
|
|
|
'min' => [
|
|
|
|
'<?php
|
|
|
|
$a = min(0, 1);
|
|
|
|
$b = min([0, 1]);
|
|
|
|
$c = min("a", "b");
|
|
|
|
$d = min(1, 2, 3, 4);
|
|
|
|
$e = min(1, 2, 3, 4, 5);
|
|
|
|
$f = min(...[1, 2, 3]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
'$b' => 'int',
|
|
|
|
'$c' => 'string',
|
|
|
|
'$d' => 'int',
|
|
|
|
'$e' => 'int',
|
|
|
|
'$f' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'minUnpackedArg' => [
|
|
|
|
'<?php
|
|
|
|
$f = min(...[1, 2, 3]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$f' => 'int',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'sscanf' => [
|
|
|
|
'<?php
|
|
|
|
sscanf("10:05:03", "%d:%d:%d", $hours, $minutes, $seconds);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$hours' => 'float|int|string',
|
|
|
|
'$minutes' => 'float|int|string',
|
|
|
|
'$seconds' => 'float|int|string',
|
2019-04-09 20:48:51 +02:00
|
|
|
],
|
|
|
|
],
|
2019-05-07 22:27:25 +02:00
|
|
|
'inferArrayMapReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/** @return array<string> */
|
|
|
|
function Foo(DateTime ...$dateTimes) : array {
|
|
|
|
return array_map(
|
|
|
|
function ($dateTime) {
|
|
|
|
return (string) ($dateTime->format("c"));
|
|
|
|
},
|
|
|
|
$dateTimes
|
|
|
|
);
|
|
|
|
}',
|
|
|
|
],
|
2019-05-21 17:51:41 +02:00
|
|
|
'noImplicitAssignmentToStringFromMixedWithDocblockTypes' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string $s */
|
|
|
|
function takesString($s) : void {}
|
|
|
|
function takesInt(int $i) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $s
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function bar($s) : void {
|
|
|
|
takesString($s);
|
|
|
|
takesInt($s);
|
|
|
|
}',
|
|
|
|
],
|
2019-05-21 18:59:06 +02:00
|
|
|
'ignoreNullableIssuesAfterMixedCoercion' => [
|
|
|
|
'<?php
|
|
|
|
function takesNullableString(?string $s) : void {}
|
|
|
|
function takesString(string $s) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $s
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function bar($s) : void {
|
|
|
|
takesNullableString($s);
|
|
|
|
takesString($s);
|
|
|
|
}',
|
|
|
|
],
|
2019-05-29 13:56:06 +02:00
|
|
|
'countableSimpleXmlElement' => [
|
|
|
|
'<?php
|
|
|
|
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><a><b></b><b></b></a>");
|
2019-07-05 22:24:00 +02:00
|
|
|
echo count($xml);',
|
2019-05-29 13:56:06 +02:00
|
|
|
],
|
2019-08-12 22:17:55 +02:00
|
|
|
'countableCallableArray' => [
|
|
|
|
'<?php
|
|
|
|
/** @param callable|false $x */
|
|
|
|
function example($x) : void {
|
|
|
|
if (is_array($x)) {
|
|
|
|
echo "Count is: " . count($x);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-05-31 15:43:46 +02:00
|
|
|
'refineWithTraitExists' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : void {
|
|
|
|
if (trait_exists($s)) {
|
|
|
|
new ReflectionClass($s);
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-05-31 15:43:46 +02:00
|
|
|
],
|
|
|
|
'refineWithClassExistsOrTraitExists' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : void {
|
|
|
|
if (trait_exists($s) || class_exists($s)) {
|
|
|
|
new ReflectionClass($s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(string $s) : void {
|
|
|
|
if (class_exists($s) || trait_exists($s)) {
|
|
|
|
new ReflectionClass($s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function baz(string $s) : void {
|
|
|
|
if (class_exists($s) || interface_exists($s) || trait_exists($s)) {
|
|
|
|
new ReflectionClass($s);
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-05-31 15:43:46 +02:00
|
|
|
],
|
2019-06-03 16:33:54 +02:00
|
|
|
'minSingleArg' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress TooFewArguments */
|
|
|
|
min(0);',
|
|
|
|
],
|
2019-06-07 21:49:10 +02:00
|
|
|
'PHP73-allowIsCountableToInformType' => [
|
|
|
|
'<?php
|
|
|
|
function getObject() : iterable{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterableObject = getObject();
|
|
|
|
|
|
|
|
if (is_countable($iterableObject)) {
|
|
|
|
if (count($iterableObject) === 0) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-06-15 22:10:48 +02:00
|
|
|
'versionCompareAsCallable' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["1.0", "2.0"];
|
2019-07-05 22:24:00 +02:00
|
|
|
uksort($a, "version_compare");',
|
2019-06-15 22:10:48 +02:00
|
|
|
],
|
2019-06-26 06:14:06 +02:00
|
|
|
'coerceToObjectAfterBeingCalled' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesFoo(Foo $foo) : void {}
|
|
|
|
|
|
|
|
/** @param mixed $f */
|
|
|
|
function takesMixed($f) : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$f = new Foo();
|
|
|
|
}
|
|
|
|
/** @psalm-suppress MixedArgument */
|
|
|
|
takesFoo($f);
|
|
|
|
$f->bar();
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-26 06:14:06 +02:00
|
|
|
],
|
2019-06-28 16:48:30 +02:00
|
|
|
'functionExists' => [
|
|
|
|
'<?php
|
|
|
|
if (!function_exists("in_array")) {
|
|
|
|
function in_array($a, $b) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-28 16:48:30 +02:00
|
|
|
],
|
2019-08-15 16:28:03 +02:00
|
|
|
'pregMatch' => [
|
|
|
|
'<?php
|
|
|
|
function takesInt(int $i) : void {}
|
2019-08-26 05:51:36 +02:00
|
|
|
|
2019-08-15 16:28:03 +02:00
|
|
|
takesInt(preg_match("{foo}", "foo"));',
|
|
|
|
],
|
|
|
|
'pregMatchWithMatches' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $matches */
|
|
|
|
function takesMatches(array $matches) : void {}
|
2019-08-26 05:51:36 +02:00
|
|
|
|
2019-08-15 16:28:03 +02:00
|
|
|
preg_match("{foo}", "foo", $matches);
|
2019-08-26 05:51:36 +02:00
|
|
|
|
2019-08-15 16:28:03 +02:00
|
|
|
takesMatches($matches);',
|
|
|
|
],
|
|
|
|
'pregMatchWithOffset' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $matches */
|
|
|
|
function takesMatches(array $matches) : void {}
|
2019-08-26 05:51:36 +02:00
|
|
|
|
2019-08-15 16:28:03 +02:00
|
|
|
preg_match("{foo}", "foo", $matches, 0, 10);
|
2019-08-26 05:51:36 +02:00
|
|
|
|
2019-08-15 16:28:03 +02:00
|
|
|
takesMatches($matches);',
|
|
|
|
],
|
|
|
|
'pregMatchWithFlags' => [
|
|
|
|
'<?php
|
|
|
|
function takesInt(int $i) : void {}
|
2019-08-26 05:51:36 +02:00
|
|
|
|
2019-08-15 16:28:03 +02:00
|
|
|
if (preg_match("{foo}", "this is foo", $matches, PREG_OFFSET_CAPTURE)) {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
takesInt($matches[0][1]);
|
|
|
|
}',
|
|
|
|
],
|
2019-07-05 16:40:05 +02:00
|
|
|
'pregReplaceCallback' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : string {
|
|
|
|
return preg_replace_callback(
|
|
|
|
\'/<files (psalm-version="[^"]+") (?:php-version="(.+)">\n)/\',
|
|
|
|
/** @param array<int, string> $matches */
|
|
|
|
function (array $matches) : string {
|
|
|
|
return $matches[1];
|
|
|
|
},
|
|
|
|
$s
|
|
|
|
);
|
|
|
|
}',
|
|
|
|
],
|
2019-07-24 23:24:23 +02:00
|
|
|
'compactDefinedVariable' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $a, string $b, bool $c) : array {
|
|
|
|
return compact("a", "b", "c");
|
|
|
|
}',
|
|
|
|
],
|
2019-07-28 22:40:35 +02:00
|
|
|
'PHP73-setCookiePhp73' => [
|
|
|
|
'<?php
|
|
|
|
setcookie(
|
|
|
|
"name",
|
|
|
|
"value",
|
|
|
|
[
|
|
|
|
"path" => "/",
|
|
|
|
"expires" => 0,
|
|
|
|
"httponly" => true,
|
|
|
|
"secure" => true,
|
|
|
|
"samesite" => "Lax"
|
|
|
|
]
|
|
|
|
);',
|
|
|
|
],
|
2019-09-08 20:44:59 +02:00
|
|
|
'printrBadArg' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress InvalidScalarArgument */
|
|
|
|
$a = print_r([], 1);
|
|
|
|
echo $a;',
|
|
|
|
],
|
2019-09-11 19:52:37 +02:00
|
|
|
'dontCoerceCallMapArgs' => [
|
|
|
|
'<?php
|
|
|
|
function getStr() : ?string {
|
|
|
|
return rand(0,1) ? "test" : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function test() : void {
|
|
|
|
$g = getStr();
|
|
|
|
/** @psalm-suppress PossiblyNullArgument */
|
|
|
|
$x = strtoupper($g);
|
|
|
|
$c = "prefix " . (strtoupper($g ?? "") === "x" ? "xa" : "ya");
|
|
|
|
echo "$x, $c\n";
|
|
|
|
}'
|
|
|
|
],
|
2019-10-04 19:07:28 +02:00
|
|
|
'mysqliRealConnectFunctionAllowsNullParameters' => [
|
|
|
|
'<?php
|
|
|
|
$mysqli = mysqli_init();
|
|
|
|
mysqli_real_connect($mysqli, null, \'test\', null);',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-17 02:51:53 +01:00
|
|
|
}
|
2017-02-27 16:52:43 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-02-27 16:52:43 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-02-27 16:52:43 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2018-04-08 18:57:56 +02:00
|
|
|
'arrayFilterWithoutTypes' => [
|
|
|
|
'<?php
|
|
|
|
$e = array_filter(
|
|
|
|
["a" => 5, "b" => 12, "c" => null],
|
|
|
|
function(?int $i) {
|
|
|
|
return $_GET["a"];
|
|
|
|
}
|
|
|
|
);',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'MixedArgumentTypeCoercion',
|
2018-04-08 18:57:56 +02:00
|
|
|
'error_levels' => ['MissingClosureParamType', 'MissingClosureReturnType'],
|
|
|
|
],
|
2019-05-07 22:27:25 +02:00
|
|
|
'arrayFilterUseMethodOnInferrableInt' => [
|
|
|
|
'<?php
|
|
|
|
$a = array_filter([1, 2, 3, 4], function ($i) { return $i->foo(); });',
|
|
|
|
'error_message' => 'InvalidMethodCall',
|
|
|
|
],
|
|
|
|
'arrayMapUseMethodOnInferrableInt' => [
|
|
|
|
'<?php
|
|
|
|
$a = array_map(function ($i) { return $i->foo(); }, [1, 2, 3, 4]);',
|
|
|
|
'error_message' => 'InvalidMethodCall',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'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
|
|
|
],
|
2018-08-28 18:37:25 +02:00
|
|
|
'invalidArgumentWithDeclareStrictTypes' => [
|
|
|
|
'<?php declare(strict_types=1);
|
|
|
|
function fooFoo(int $a): void {}
|
|
|
|
fooFoo("string");',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-08-28 23:42:39 +02:00
|
|
|
'builtinFunctioninvalidArgumentWithWeakTypes' => [
|
|
|
|
'<?php
|
|
|
|
$s = substr(5, 4);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'builtinFunctioninvalidArgumentWithDeclareStrictTypes' => [
|
|
|
|
'<?php declare(strict_types=1);
|
|
|
|
$s = substr(5, 4);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'builtinFunctioninvalidArgumentWithDeclareStrictTypesInClass' => [
|
|
|
|
'<?php declare(strict_types=1);
|
|
|
|
class A {
|
|
|
|
public function foo() : void {
|
|
|
|
$s = substr(5, 4);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
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");',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'TooManyArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:21 - Too many arguments for method fooFoo '
|
2018-02-02 17:26:55 +01:00
|
|
|
. '- 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());',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
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()]);',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
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',
|
|
|
|
],
|
2019-02-22 06:50:41 +01:00
|
|
|
'arrayMapWithNonCallableStringArray' => [
|
|
|
|
'<?php
|
|
|
|
$foo = ["one", "two"];
|
|
|
|
array_map($foo, ["hello"]);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'arrayMapWithNonCallableIntArray' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [1, 2];
|
|
|
|
array_map($foo, ["hello"]);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
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',
|
|
|
|
],
|
2018-04-03 04:19:58 +02:00
|
|
|
'varExportAssignmentToVoid' => [
|
|
|
|
'<?php
|
|
|
|
$a = var_export(["a"]);',
|
|
|
|
'error_message' => 'AssignmentToVoid',
|
|
|
|
],
|
2018-04-16 22:03:04 +02:00
|
|
|
'explodeWithEmptyString' => [
|
|
|
|
'<?php
|
|
|
|
function exploder(string $s) : array {
|
|
|
|
return explode("", $s);
|
|
|
|
}',
|
|
|
|
'error_message' => 'FalsableReturnStatement',
|
|
|
|
],
|
2018-04-30 19:17:09 +02:00
|
|
|
'complainAboutArrayToIterable' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
/**
|
|
|
|
* @param iterable<mixed,A> $p
|
|
|
|
*/
|
|
|
|
function takesIterableOfA(iterable $p): void {}
|
|
|
|
|
2018-05-01 04:18:41 +02:00
|
|
|
takesIterableOfA([new B]); // should complain',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'complainAboutArrayToIterableSingleParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
/**
|
|
|
|
* @param iterable<A> $p
|
|
|
|
*/
|
|
|
|
function takesIterableOfA(iterable $p): void {}
|
|
|
|
|
2018-04-30 19:17:09 +02:00
|
|
|
takesIterableOfA([new B]); // should complain',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-05-20 19:14:31 +02:00
|
|
|
'putInvalidTypeMessagesFirst' => [
|
|
|
|
'<?php
|
|
|
|
$q = rand(0,1) ? new stdClass : false;
|
|
|
|
strlen($q);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-06-28 22:01:53 +02:00
|
|
|
'arrayReduceInvalidClosureTooFewArgs' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [2, 3, 4, 5];
|
|
|
|
|
|
|
|
$direct_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
function (int $carry) : int {
|
|
|
|
return 5;
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
'error_levels' => ['MixedTypeCoercion'],
|
|
|
|
],
|
|
|
|
'arrayReduceInvalidItemType' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [2, 3, 4, 5];
|
|
|
|
|
|
|
|
$direct_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
function (int $carry, stdClass $item) {
|
|
|
|
return $_GET["boo"];
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
'error_levels' => ['MissingClosureReturnType'],
|
|
|
|
],
|
|
|
|
'arrayReduceInvalidCarryType' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [2, 3, 4, 5];
|
|
|
|
|
|
|
|
$direct_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
function (stdClass $carry, int $item) {
|
|
|
|
return $_GET["boo"];
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
'error_levels' => ['MissingClosureReturnType'],
|
|
|
|
],
|
|
|
|
'arrayReduceInvalidCarryOutputType' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [2, 3, 4, 5];
|
|
|
|
|
|
|
|
$direct_closure_result = array_reduce(
|
|
|
|
$arr,
|
|
|
|
function (int $carry, int $item) : stdClass {
|
|
|
|
return new stdClass;
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-12-19 22:15:19 +01:00
|
|
|
'arrayPopNotNull' => [
|
|
|
|
'<?php
|
|
|
|
function expectsInt(int $a) : void {}
|
|
|
|
|
|
|
|
/**
|
2019-01-05 06:15:53 +01:00
|
|
|
* @param array<array-key, array{item:int}> $list
|
2018-12-19 22:15:19 +01:00
|
|
|
*/
|
|
|
|
function test(array $list) : void
|
|
|
|
{
|
|
|
|
while (!empty($list)) {
|
|
|
|
$tmp = array_pop($list);
|
|
|
|
if ($tmp === null) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction',
|
|
|
|
],
|
2019-01-05 22:23:18 +01:00
|
|
|
'getTypeInvalidValue' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $maybe
|
|
|
|
*/
|
|
|
|
function matchesTypes($maybe) : void {
|
|
|
|
$t = gettype($maybe);
|
|
|
|
if ($t === "bool") {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2019-01-06 19:02:46 +01:00
|
|
|
'rangeWithFloatStep' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(int $bar) : string {
|
|
|
|
return (string) $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (range(1, 10, .3) as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'rangeWithFloatStart' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
function foo(int $bar) : string {
|
|
|
|
return (string) $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (range(1.4, 10) as $x) {
|
|
|
|
foo($x);
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2019-01-06 22:40:44 +01:00
|
|
|
'duplicateFunction' => [
|
|
|
|
'<?php
|
|
|
|
function f() : void {}
|
|
|
|
function f() : void {}',
|
|
|
|
'error_message' => 'DuplicateFunction',
|
|
|
|
],
|
|
|
|
'duplicateCoreFunction' => [
|
|
|
|
'<?php
|
|
|
|
function sort() : void {}',
|
|
|
|
'error_message' => 'DuplicateFunction',
|
|
|
|
],
|
2019-01-20 00:11:39 +01:00
|
|
|
'usortInvalidComparison' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [["one"], ["two"], ["three"]];
|
|
|
|
|
|
|
|
usort(
|
|
|
|
$arr,
|
|
|
|
function (string $a, string $b): int {
|
|
|
|
return strcmp($a, $b);
|
|
|
|
}
|
|
|
|
);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-01-20 02:18:45 +01:00
|
|
|
'usortInvalidCallableString' => [
|
|
|
|
'<?php
|
|
|
|
$a = [[1], [2], [3]];
|
|
|
|
usort($a, "strcmp");',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-02-10 22:15:52 +01:00
|
|
|
'functionCallOnMixed' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var mixed $s
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
*/
|
|
|
|
$s = 1;
|
|
|
|
$s();',
|
|
|
|
'error_message' => 'MixedFunctionCall',
|
|
|
|
],
|
2019-02-18 22:35:23 +01:00
|
|
|
'iterableOfObjectCannotAcceptIterableOfInt' => [
|
|
|
|
'<?php
|
|
|
|
/** @param iterable<string,object> $_p */
|
|
|
|
function accepts(iterable $_p): void {}
|
|
|
|
|
|
|
|
/** @return iterable<int,int> */
|
|
|
|
function iterable() { yield 1; }
|
|
|
|
|
|
|
|
accepts(iterable());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'iterableOfObjectCannotAcceptTraversableOfInt' => [
|
|
|
|
'<?php
|
|
|
|
/** @param iterable<string,object> $_p */
|
|
|
|
function accepts(iterable $_p): void {}
|
|
|
|
|
|
|
|
/** @return Traversable<int,int> */
|
|
|
|
function traversable() { yield 1; }
|
|
|
|
|
|
|
|
accepts(traversable());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'iterableOfObjectCannotAcceptGeneratorOfInt' => [
|
|
|
|
'<?php
|
|
|
|
/** @param iterable<string,object> $_p */
|
|
|
|
function accepts(iterable $_p): void {}
|
|
|
|
|
|
|
|
/** @return Generator<int,int,mixed,void> */
|
|
|
|
function generator() { yield 1; }
|
|
|
|
|
|
|
|
accepts(generator());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'iterableOfObjectCannotAcceptArrayOfInt' => [
|
|
|
|
'<?php
|
|
|
|
/** @param iterable<string,object> $_p */
|
|
|
|
function accepts(iterable $_p): void {}
|
|
|
|
|
|
|
|
/** @return array<int,int> */
|
|
|
|
function arr() { return [1]; }
|
|
|
|
|
|
|
|
accepts(arr());',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-02-20 23:43:12 +01:00
|
|
|
'nonNullableByRef' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string &$s) : void {}
|
|
|
|
|
|
|
|
function bar() : void {
|
|
|
|
foo($bar);
|
|
|
|
}',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'NullReference',
|
2019-02-20 23:43:12 +01:00
|
|
|
],
|
2019-02-21 19:26:37 +01:00
|
|
|
'intCastByRef' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int &$i) : void {}
|
|
|
|
|
|
|
|
$a = rand(0, 1) ? null : 5;
|
|
|
|
/** @psalm-suppress MixedArgument */
|
|
|
|
foo((int) $a);',
|
2019-05-21 17:51:41 +02:00
|
|
|
'error_message' => 'InvalidPassByReference',
|
|
|
|
],
|
|
|
|
'implicitAssignmentToStringFromMixed' => [
|
|
|
|
'<?php
|
|
|
|
/** @param "a"|"b" $s */
|
|
|
|
function takesString(string $s) : void {}
|
|
|
|
function takesInt(int $i) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $s
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function bar($s) : void {
|
|
|
|
takesString($s);
|
|
|
|
takesInt($s);
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2019-02-21 19:26:37 +01:00
|
|
|
],
|
2019-06-19 15:04:01 +02:00
|
|
|
'tooFewArgsAccurateCount' => [
|
|
|
|
'<?php
|
|
|
|
preg_match(\'/adsf/\');',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'TooFewArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:21 - Too few arguments for method preg_match - expecting 2 but saw 1',
|
|
|
|
],
|
2019-07-24 23:24:23 +02:00
|
|
|
'compactUndefinedVariable' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : array {
|
|
|
|
return compact("a", "b", "c");
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedVariable',
|
|
|
|
],
|
2019-08-12 22:17:55 +02:00
|
|
|
'countCallableArrayShouldBeTwo' => [
|
|
|
|
'<?php
|
|
|
|
/** @param callable|false $x */
|
|
|
|
function example($x) : void {
|
|
|
|
if (is_array($x)) {
|
|
|
|
$c = count($x);
|
|
|
|
if ($c !== 2) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2019-08-26 05:51:36 +02:00
|
|
|
'arrayShiftUndefinedVariable' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress MissingParamType */
|
|
|
|
function foo($data): void {
|
|
|
|
/** @psalm-suppress MixedArgument */
|
|
|
|
array_unshift($data, $a);
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedVariable',
|
|
|
|
],
|
2019-09-11 19:52:37 +02:00
|
|
|
'coerceCallMapArgsInStrictMode' => [
|
|
|
|
'<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
function getStr() : ?string {
|
|
|
|
return rand(0,1) ? "test" : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function test() : void {
|
|
|
|
$g = getStr();
|
|
|
|
/** @psalm-suppress PossiblyNullArgument */
|
|
|
|
$x = strtoupper($g);
|
|
|
|
$c = "prefix " . (strtoupper($g ?? "") === "x" ? "xa" : "ya");
|
|
|
|
echo "$x, $c\n";
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2019-10-01 14:45:36 +02:00
|
|
|
'noCrashOnEmptyArrayPush' => [
|
|
|
|
'<?php
|
|
|
|
array_push();',
|
|
|
|
'error_message' => 'TooFewArguments',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-27 16:52:43 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|