Skip to content

Commit

Permalink
Downgrade error when using unknown variable in mixed method
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 6, 2020
1 parent b2678d4 commit 75a3412
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -706,6 +707,19 @@ private static function evaluateAribitraryParam(
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;
Expand Down
4 changes: 2 additions & 2 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,14 @@ class Foo {
/**
* @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
Expand Down
1 change: 1 addition & 0 deletions tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ function callDangerous(): void {
'<?php
/** @psalm-suppress MixedMethodCall */
function passesByRef(object $a): void {
/** @psalm-suppress PossiblyUndefinedVariable */
$a->passedByRef($b);
}',
],
Expand Down

0 comments on commit 75a3412

Please sign in to comment.