1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/EmptyTest.php

269 lines
9.0 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class EmptyTest extends TestCase
{
2018-11-06 03:57:36 +01:00
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return array
*/
2018-11-06 03:57:36 +01:00
public function providerValidCodeParse()
{
return [
'ifNotUndefinedAndEmpty' => [
'<?php
$a = !empty($b) ? $b : null;',
'assertions' => [
'$a' => 'mixed|null',
],
'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' => [],
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
],
'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 {
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 {
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 {
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 {
if (empty($s) || $s === "hello") {}
}',
],
'noRedundantConditionOnMixed' => [
'<?php
2018-01-11 21:50:45 +01:00
function testarray(array $data): void {
foreach ($data as $item) {
if (!empty($item["a"]) && !empty($item["b"]["c"])) {
echo "Found\n";
}
}
}',
'assertions' => [],
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
],
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', 'MixedArgument'],
2017-12-10 20:56:51 +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) {}
}',
],
2018-04-09 16:19:23 +02:00
'alwaysBoolResult' => [
'<?php
function takesBool(bool $p): void {}
takesBool(empty($q));'
],
2018-05-08 22:34:08 +02:00
'noRedundantConditionAfterFalsyIntChecks' => [
'<?php
function foo(int $t) : void {
if (!$t) {
foreach ([0, 1, 2] as $a) {
if (!$t) {
$t = $a;
}
}
}
}',
],
'noRedundantConditionAfterEmptyMixedChecks' => [
2018-05-08 06:57:18 +02:00
'<?php
function foo($t) : void {
if (empty($t)) {
foreach ($_GET["u"] as $a) {
if (empty($t)) {
$t = $a;
}
}
}
}',
'assertions' => [],
'error_levels' => ['MixedAssignment', 'MissingParamType'],
],
'canBeNonEmptyArray' => [
'<?php
function _processScopes($scopes) : void {
if (!is_array($scopes) && !empty($scopes)) {
$scopes = explode(" ", trim($scopes));
} else {
// false is allowed here
}
if (empty($scopes)){}
}',
'assertions' => [],
'error_levels' => ['MixedAssignment', 'MissingParamType', 'MixedArgument'],
],
2018-12-11 19:50:26 +01:00
'multipleEmptiesInCondition' => [
'<?php
/** @param array<int, int> $o */
function foo(array $o) : void {
if (empty($o[0]) && empty($o[1])) {}
}',
],
'multipleEmptiesInConditionWithMixedOffset' => [
'<?php
/** @param array $o */
function foo(array $o) : void {
if (empty($o[0]) && empty($o[1])) {}
}',
],
];
}
2018-05-01 04:13:13 +02:00
/**
* @return array
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
2018-05-01 04:13:13 +02:00
{
return [
'preventImpossibleEmpty' => [
'<?php
function foo(array $arr) : void {
if (empty($ar)) {
// do something
}
}',
'error_message' => 'UndefinedVariable',
],
'reconciliationForMixed' => [
'<?php
function foo(array $arr) : void {
$a = empty($arr["a"]) ? "" : $arr["a"];
if ($a) {
if ($a) {}
}
}',
'error_message' => 'RedundantCondition',
'error_levels' => ['MixedAssignment', 'MissingParamType'],
],
2018-05-01 04:13:13 +02:00
];
}
}