1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Warn about impossible empty

This commit is contained in:
Matthew Brown 2018-04-30 22:13:13 -04:00
parent 6e67030925
commit 15e1003c37
3 changed files with 24 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace Psalm\Checker\Statements\Expression\Fetch;
use PhpParser;
use Psalm\Checker\FunctionLikeChecker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\StatementsChecker;
use Psalm\CodeLocation;
@ -157,7 +158,9 @@ class VariableFetchChecker
$context->branch_point
);
}
} elseif (!$context->inside_isset) {
} elseif (!$context->inside_isset
|| $statements_checker->getSource() instanceof FunctionLikeChecker
) {
if ($context->is_global) {
if (IssueBuffer::accepts(
new UndefinedGlobalVariable(

View File

@ -1150,6 +1150,7 @@ class ExpressionChecker
PhpParser\Node\Expr $stmt,
Context $context
) {
$context->inside_isset = true;
if (self::analyze($statements_checker, $stmt, $context) === false) {

View File

@ -3,6 +3,7 @@ namespace Psalm\Tests;
class EmptyTest extends TestCase
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
@ -180,4 +181,22 @@ class EmptyTest extends TestCase
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'preventImpossibleEmpty' => [
'<?php
function foo(array $arr) : void {
if (empty($ar)) {
// do something
}
}',
'error_message' => 'UndefinedVariable',
],
];
}
}