Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if argument passed to isset() is not a variable #10068

Merged
merged 1 commit into from Aug 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,9 +3,12 @@
namespace Psalm\Internal\Analyzer\Statements\Expression;

use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Issue\InvalidArgument;
use Psalm\IssueBuffer;
use Psalm\Type;

/**
Expand All @@ -30,6 +33,15 @@ public static function analyze(
$context->vars_in_scope[$var_id] = Type::getMixed();
$context->vars_possibly_in_scope[$var_id] = true;
}
} elseif (!self::isValidStatement($isset_var)) {
IssueBuffer::maybeAdd(
new InvalidArgument(
'Isset only works with variables and array elements',
new CodeLocation($statements_analyzer->getSource(), $isset_var),
'empty',
),
$statements_analyzer->getSuppressedIssues(),
);
}

self::analyzeIssetVar($statements_analyzer, $isset_var, $context);
Expand All @@ -49,4 +61,15 @@ public static function analyzeIssetVar(

$context->inside_isset = false;
}

private static function isValidStatement(PhpParser\Node\Expr $stmt): bool
{
return $stmt instanceof PhpParser\Node\Expr\Variable
|| $stmt instanceof PhpParser\Node\Expr\ArrayDimFetch
|| $stmt instanceof PhpParser\Node\Expr\PropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\ClassConstFetch
|| $stmt instanceof PhpParser\Node\Expr\AssignRef;
}
}