2017-12-05 18:14:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
class EmptyTest extends TestCase
|
|
|
|
{
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'empty' => [
|
|
|
|
'<?php
|
|
|
|
$a = !empty($b) ? $b : null;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'mixed',
|
|
|
|
],
|
|
|
|
'error_levels' => ['MixedAssignment'],
|
|
|
|
],
|
|
|
|
'emptyArrayVar' => [
|
|
|
|
'<?php
|
|
|
|
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' => [],
|
2018-01-10 01:33:39 +01:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
|
2017-12-05 18:14:10 +01:00
|
|
|
],
|
|
|
|
'removeEmptyArray' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @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' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @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' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @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' => [
|
|
|
|
'<?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' => [
|
|
|
|
'<?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) {
|
|
|
|
if (!empty($item["a"]) && !empty($item["b"]) && !empty($item["b"]["c"])) {
|
|
|
|
echo "Found\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2018-01-10 01:33:39 +01:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
|
2017-12-05 18:14:10 +01:00
|
|
|
],
|
2017-12-10 20:56:51 +01:00
|
|
|
'dontBleedEmptyAfterExtract' => [
|
|
|
|
'<?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' => [],
|
|
|
|
'error_levels' => ['MixedAssignment'],
|
|
|
|
],
|
2018-03-18 00:28:01 +01:00
|
|
|
'emptyObjectLike' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [
|
|
|
|
"profile" => [
|
|
|
|
"foo" => "bar",
|
|
|
|
],
|
|
|
|
"groups" => [
|
|
|
|
"foo" => "bar",
|
|
|
|
"hide" => rand(0, 5),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($arr as $item) {
|
|
|
|
if (empty($item["hide"]) || $item["hide"] === 3) {}
|
|
|
|
}',
|
|
|
|
],
|
2017-12-05 18:14:10 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|