1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Downgrade error when using unknown variable in mixed method

This commit is contained in:
Matthew Brown 2020-03-05 21:24:08 -05:00
parent b2678d40aa
commit 75a3412a42
3 changed files with 17 additions and 2 deletions

View File

@ -30,6 +30,7 @@ use Psalm\Issue\NullArgument;
use Psalm\Issue\PossiblyFalseArgument;
use Psalm\Issue\PossiblyInvalidArgument;
use Psalm\Issue\PossiblyNullArgument;
use Psalm\Issue\PossiblyUndefinedVariable;
use Psalm\Issue\TooFewArguments;
use Psalm\Issue\TooManyArguments;
use Psalm\Issue\ArgumentTypeCoercion;
@ -706,6 +707,19 @@ class CallAnalyzer
if (!$context->hasVariable($var_id, $statements_analyzer)
|| $context->vars_in_scope[$var_id]->isNull()
) {
if (!isset($context->vars_in_scope[$var_id])) {
if (IssueBuffer::accepts(
new PossiblyUndefinedVariable(
'Variable ' . $var_id
. ' must be defined prior to use within an unknown function or method',
new CodeLocation($statements_analyzer->getSource(), $arg->value)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
// we don't know if it exists, assume it's passed by reference
$context->vars_in_scope[$var_id] = Type::getMixed();
$context->vars_possibly_in_scope[$var_id] = true;

View File

@ -543,14 +543,14 @@ class UnusedCodeTest extends TestCase
/**
* @psalm-suppress MixedArgument
*/
public function bar(): void {
public function bar(string $request): void {
/** @var mixed $action */
$action = "";
$this->{"execute" . ucfirst($action)}($request);
}
}
(new Foo)->bar();'
(new Foo)->bar("request");'
],
'usedMethodCallForExternalMutationFreeClass' => [
'<?php

View File

@ -522,6 +522,7 @@ class UnusedVariableTest extends TestCase
'<?php
/** @psalm-suppress MixedMethodCall */
function passesByRef(object $a): void {
/** @psalm-suppress PossiblyUndefinedVariable */
$a->passedByRef($b);
}',
],