2017-12-05 18:14:10 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-08-08 05:22:30 +02:00
|
|
|
namespace Psalm\Tests\TypeReconciliation;
|
2017-12-05 18:14:10 +01:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class EmptyTest extends TestCase
|
2017-12-05 18:14:10 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-12-05 18:14:10 +01:00
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
|
2017-12-05 18:14:10 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-12-05 18:14:10 +01:00
|
|
|
{
|
|
|
|
return [
|
2018-12-08 19:18:55 +01:00
|
|
|
'ifNotUndefinedAndEmpty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-05 18:14:10 +01:00
|
|
|
$a = !empty($b) ? $b : null;',
|
|
|
|
'assertions' => [
|
2018-12-08 19:18:55 +01:00
|
|
|
'$a' => 'mixed|null',
|
2017-12-05 18:14:10 +01:00
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2017-12-05 18:14:10 +01:00
|
|
|
],
|
|
|
|
'emptyArrayVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-05 18:14:10 +01:00
|
|
|
function a(array $in): void
|
|
|
|
{
|
|
|
|
$r = [];
|
|
|
|
foreach ($in as $entry) {
|
|
|
|
if (!empty($entry["a"])) {
|
|
|
|
$r[] = [];
|
|
|
|
}
|
|
|
|
if (empty($entry["a"])) {
|
|
|
|
$r[] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function b(array $in): void
|
|
|
|
{
|
|
|
|
$i = 0;
|
|
|
|
foreach ($in as $entry) {
|
|
|
|
if (!empty($entry["a"])) {
|
|
|
|
$i--;
|
|
|
|
}
|
|
|
|
if (empty($entry["a"])) {
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function c(array $in): void
|
|
|
|
{
|
|
|
|
foreach ($in as $entry) {
|
|
|
|
if (!empty($entry["a"])) {}
|
|
|
|
}
|
|
|
|
foreach ($in as $entry) {
|
|
|
|
if (empty($entry["a"])) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess'],
|
2017-12-05 18:14:10 +01:00
|
|
|
],
|
|
|
|
'removeEmptyArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-05 18:14:10 +01:00
|
|
|
$arr_or_string = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$arr_or_string = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return void **/
|
|
|
|
function foo(string $s) {}
|
|
|
|
|
|
|
|
if (!empty($arr_or_string)) {
|
|
|
|
foo($arr_or_string);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'emptyArrayReconciliationThenIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-05 18:14:10 +01:00
|
|
|
/**
|
|
|
|
* @param string|string[] $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($a): string {
|
2017-12-05 18:14:10 +01:00
|
|
|
if (is_string($a)) {
|
|
|
|
return $a;
|
|
|
|
} elseif (empty($a)) {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($a[0])) {
|
|
|
|
return $a[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
return "not found";
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'emptyStringReconciliationThenIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-05 18:14:10 +01:00
|
|
|
/**
|
|
|
|
* @param Exception|string|string[] $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($a): string {
|
2017-12-05 18:14:10 +01:00
|
|
|
if (is_array($a)) {
|
|
|
|
return "hello";
|
|
|
|
} elseif (empty($a)) {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($a)) {
|
|
|
|
return $a;
|
|
|
|
};
|
|
|
|
|
|
|
|
return "an exception";
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'emptyExceptionReconciliationAfterIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-05 18:14:10 +01:00
|
|
|
/**
|
|
|
|
* @param Exception|null $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($a): string {
|
2017-12-05 18:14:10 +01:00
|
|
|
if ($a && $a->getMessage() === "hello") {
|
|
|
|
return "hello";
|
|
|
|
} elseif (empty($a)) {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a->getMessage();
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'noFalsyLeak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $s): void {
|
2017-12-05 18:14:10 +01:00
|
|
|
if (empty($s) || $s === "hello") {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'noRedundantConditionOnMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function testarray(array $data): void {
|
2017-12-05 18:14:10 +01:00
|
|
|
foreach ($data as $item) {
|
2018-12-19 22:15:19 +01:00
|
|
|
if (!empty($item["a"]) && !empty($item["b"]["c"])) {
|
2017-12-05 18:14:10 +01:00
|
|
|
echo "Found\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess'],
|
2017-12-05 18:14:10 +01:00
|
|
|
],
|
2017-12-10 20:56:51 +01:00
|
|
|
'dontBleedEmptyAfterExtract' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(array $args): void {
|
2017-12-10 20:56:51 +01:00
|
|
|
extract($args);
|
|
|
|
if ((empty($arr) && empty($a)) || $c === 0) {
|
|
|
|
} else {
|
|
|
|
foreach ($arr as $b) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MixedArgument'],
|
2017-12-10 20:56:51 +01:00
|
|
|
],
|
2020-08-30 17:44:14 +02:00
|
|
|
'emptyTKeyedArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-18 00:28:01 +01:00
|
|
|
$arr = [
|
|
|
|
"profile" => [
|
|
|
|
"foo" => "bar",
|
|
|
|
],
|
|
|
|
"groups" => [
|
|
|
|
"foo" => "bar",
|
|
|
|
"hide" => rand(0, 5),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($arr as $item) {
|
|
|
|
if (empty($item["hide"]) || $item["hide"] === 3) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-04-09 16:19:23 +02:00
|
|
|
'alwaysBoolResult' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-09 16:19:23 +02:00
|
|
|
function takesBool(bool $p): void {}
|
2019-03-23 19:27:54 +01:00
|
|
|
takesBool(empty($q));',
|
2018-04-09 16:19:23 +02:00
|
|
|
],
|
2018-05-08 22:34:08 +02:00
|
|
|
'noRedundantConditionAfterFalsyIntChecks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-08 22:34:08 +02:00
|
|
|
function foo(int $t) : void {
|
|
|
|
if (!$t) {
|
|
|
|
foreach ([0, 1, 2] as $a) {
|
|
|
|
if (!$t) {
|
|
|
|
$t = $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'noRedundantConditionAfterEmptyMixedChecks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-08 06:57:18 +02:00
|
|
|
function foo($t) : void {
|
|
|
|
if (empty($t)) {
|
|
|
|
foreach ($_GET["u"] as $a) {
|
|
|
|
if (empty($t)) {
|
|
|
|
$t = $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MissingParamType'],
|
2018-05-08 06:57:18 +02:00
|
|
|
],
|
2018-12-08 19:18:55 +01:00
|
|
|
'canBeNonEmptyArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-08 19:18:55 +01:00
|
|
|
function _processScopes($scopes) : void {
|
|
|
|
if (!is_array($scopes) && !empty($scopes)) {
|
|
|
|
$scopes = explode(" ", trim($scopes));
|
|
|
|
} else {
|
|
|
|
// false is allowed here
|
|
|
|
}
|
2018-05-31 21:07:03 +02:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if (empty($scopes)){}
|
2018-05-31 21:07:03 +02:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MissingParamType', 'MixedArgument'],
|
2018-05-31 21:07:03 +02:00
|
|
|
],
|
2018-12-11 19:50:26 +01:00
|
|
|
'multipleEmptiesInCondition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-11 19:50:26 +01:00
|
|
|
/** @param array<int, int> $o */
|
|
|
|
function foo(array $o) : void {
|
|
|
|
if (empty($o[0]) && empty($o[1])) {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'multipleEmptiesInConditionWithMixedOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-11 19:50:26 +01:00
|
|
|
/** @param array $o */
|
|
|
|
function foo(array $o) : void {
|
|
|
|
if (empty($o[0]) && empty($o[1])) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-12-20 22:03:21 +01:00
|
|
|
'unsetChangesArrayEmptiness' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-20 22:03:21 +01:00
|
|
|
function foo(array $n): void {
|
|
|
|
if (empty($n)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (!empty($n)) {
|
|
|
|
unset($n[rand(0, 10)]);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-07-15 15:49:30 +02:00
|
|
|
'unsetChangesComplicatedArrayEmptiness' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-20 22:03:21 +01:00
|
|
|
function contains(array $data, array $needle): bool {
|
|
|
|
if (empty($data) || empty($needle)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$stack = [];
|
|
|
|
|
|
|
|
while (!empty($needle)) {
|
|
|
|
$key = key($needle);
|
2020-07-20 10:49:36 +02:00
|
|
|
if ($key === null) continue;
|
2018-12-20 22:03:21 +01:00
|
|
|
$val = $needle[$key];
|
|
|
|
unset($needle[$key]);
|
|
|
|
|
|
|
|
if (array_key_exists($key, $data) && is_array($val)) {
|
|
|
|
$next = $data[$key];
|
|
|
|
unset($data[$key]);
|
|
|
|
|
|
|
|
if (!empty($val)) {
|
|
|
|
$stack[] = [$val, $next];
|
|
|
|
}
|
|
|
|
} elseif (!array_key_exists($key, $data) || $data[$key] != $val) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($needle) && !empty($stack)) {
|
|
|
|
list($needle, $data) = array_pop($stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MissingParamType', 'MixedArgument', 'MixedArrayOffset', 'MixedArrayAccess'],
|
2018-12-20 22:03:21 +01:00
|
|
|
],
|
2018-12-26 11:52:37 +01:00
|
|
|
'possiblyEmptyIterable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-26 11:52:37 +01:00
|
|
|
function foo(iterable $i) : void {
|
|
|
|
if (empty($i)) {}
|
|
|
|
if (!empty($i)) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-02-07 22:24:31 +01:00
|
|
|
'allowEmptyClassConstantOffsetCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-07 22:24:31 +01:00
|
|
|
class Foo {
|
|
|
|
const BAR = "bar";
|
|
|
|
const ONE = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string,string> $data
|
|
|
|
*/
|
|
|
|
function bat(array $data) : void {
|
|
|
|
if (!empty($data["foo"])) {
|
|
|
|
if (empty($data[Foo::BAR])) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int,string> $data
|
|
|
|
*/
|
|
|
|
function baz(array $data) : void {
|
|
|
|
if (!empty($data[0])) {
|
|
|
|
if (empty($data[Foo::ONE])) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-02-18 18:53:55 +01:00
|
|
|
'doubleEmptyCheckTwoArrays' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 18:53:55 +01:00
|
|
|
function foo(array $a, array $b) : void {
|
|
|
|
if (empty($a) && empty($b)) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-18 18:53:55 +01:00
|
|
|
],
|
2020-08-30 17:44:14 +02:00
|
|
|
'doubleEmptyCheckOnTKeyedArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 18:53:55 +01:00
|
|
|
/**
|
|
|
|
* @param array{a: array, b: array} $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (empty($arr["a"]) && empty($arr["b"])) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-18 18:53:55 +01:00
|
|
|
],
|
2020-08-30 17:44:14 +02:00
|
|
|
'doubleEmptyCheckOnTKeyedArrayVariableOffsets' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-26 07:03:33 +01:00
|
|
|
function foo(int $i, int $j) : void {
|
|
|
|
$arr = [];
|
|
|
|
$arr[0] = rand(0, 1);
|
|
|
|
$arr[1] = rand(0, 1);
|
|
|
|
|
|
|
|
if (empty($arr[$i]) && empty($arr[$j])) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-26 03:30:40 +01:00
|
|
|
],
|
|
|
|
'checkArrayEmptyUnknownRoot' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-26 03:30:40 +01:00
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (empty($arr[rand(0, 1)])) {
|
|
|
|
if ($arr) {}
|
|
|
|
}
|
|
|
|
}',
|
2019-02-26 07:03:33 +01:00
|
|
|
],
|
2019-11-06 22:14:46 +01:00
|
|
|
'allowEmptyCheckOnPossiblyNullPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-06 22:14:46 +01:00
|
|
|
class A {
|
|
|
|
public bool $b = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(?A $a) : void {
|
|
|
|
if (!empty($a->b)) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-11-14 19:31:50 +01:00
|
|
|
'allowNumericEmpty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-14 19:31:50 +01:00
|
|
|
/**
|
|
|
|
* @param numeric $p
|
|
|
|
*/
|
|
|
|
function f($p): bool {
|
|
|
|
if (empty($p)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}'
|
|
|
|
],
|
2020-01-24 04:26:49 +01:00
|
|
|
'possiblyUndefinedArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-24 04:26:49 +01:00
|
|
|
$d = [];
|
|
|
|
if (!rand(0,1)) {
|
|
|
|
$d[0] = "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($d[0])) {}'
|
|
|
|
],
|
2020-12-02 04:50:21 +01:00
|
|
|
'reconcileNonEmptyArrayKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-02 04:50:21 +01:00
|
|
|
/**
|
|
|
|
* @param array{a?: string, b: string} $arr
|
|
|
|
*/
|
|
|
|
function createFromString(array $arr): void
|
|
|
|
{
|
|
|
|
if (empty($arr["a"])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $arr["a"];
|
|
|
|
}'
|
|
|
|
],
|
2021-05-17 22:11:27 +02:00
|
|
|
'reconcileEmptyTwiceWithoutReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-17 22:11:27 +02:00
|
|
|
function foo(array $arr): void {
|
|
|
|
if (!empty($arr["a"])) {
|
|
|
|
} else {
|
|
|
|
if (empty($arr["dontcare"])) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($arr["a"])) {}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'reconcileEmptyTwiceWithReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-17 22:11:27 +02:00
|
|
|
function foo(array $arr): void {
|
|
|
|
if (!empty($arr["a"])) {
|
|
|
|
} else {
|
|
|
|
if (empty($arr["dontcare"])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($arr["a"])) {}
|
|
|
|
}'
|
|
|
|
],
|
2017-12-05 18:14:10 +01:00
|
|
|
];
|
|
|
|
}
|
2018-05-01 04:13:13 +02:00
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
2018-05-01 04:13:13 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2018-05-01 04:13:13 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'preventImpossibleEmpty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-01 04:13:13 +02:00
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (empty($ar)) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedVariable',
|
|
|
|
],
|
2018-12-08 19:18:55 +01:00
|
|
|
'reconciliationForMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-08 19:18:55 +01:00
|
|
|
function foo(array $arr) : void {
|
|
|
|
$a = empty($arr["a"]) ? "" : $arr["a"];
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
if ($a) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MissingParamType'],
|
2018-12-08 19:18:55 +01:00
|
|
|
],
|
2019-11-06 22:14:46 +01:00
|
|
|
'preventEmptyOnBool' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-06 22:14:46 +01:00
|
|
|
function foo(bool $b) : void {
|
|
|
|
if (!empty($b)) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-11-14 20:23:33 +01:00
|
|
|
'preventEmptyCreatingArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-14 20:23:33 +01:00
|
|
|
/** @return array{a:mixed} */
|
|
|
|
function foo(array $r) {
|
|
|
|
if (!empty($r["a"])) {}
|
|
|
|
return $r;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedReturnTypeCoercion'
|
|
|
|
],
|
|
|
|
'preventEmptyEquivalentCreatingArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-14 20:23:33 +01:00
|
|
|
/** @return array{a:mixed} */
|
|
|
|
function foo(array $r) {
|
|
|
|
if (isset($r["a"]) && $r["a"]) {}
|
|
|
|
return $r;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedReturnTypeCoercion'
|
|
|
|
],
|
2020-02-09 17:42:41 +01:00
|
|
|
'secondEmptyTwice' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-06 22:38:45 +01:00
|
|
|
/**
|
|
|
|
* @param array{a?:int,b?:string} $p
|
|
|
|
*/
|
2020-02-09 17:42:41 +01:00
|
|
|
function f(array $p) : void {
|
2020-02-06 22:38:45 +01:00
|
|
|
if (empty($p)) {
|
|
|
|
throw new RuntimeException("");
|
|
|
|
}
|
|
|
|
assert(!empty($p));
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition'
|
|
|
|
],
|
2018-05-01 04:13:13 +02:00
|
|
|
];
|
|
|
|
}
|
2017-12-05 18:14:10 +01:00
|
|
|
}
|