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

Add support for compact

This commit is contained in:
Brown 2019-07-24 17:24:23 -04:00
parent ea2074b19b
commit 5770a6c904
2 changed files with 33 additions and 0 deletions

View File

@ -28,6 +28,7 @@ use Psalm\Issue\TooFewArguments;
use Psalm\Issue\TooManyArguments;
use Psalm\Issue\ArgumentTypeCoercion;
use Psalm\Issue\UndefinedFunction;
use Psalm\Issue\UndefinedVariable;
use Psalm\IssueBuffer;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FunctionLikeParameter;
@ -1181,6 +1182,25 @@ class CallAnalyzer
}
}
if ($method_id === 'compact'
&& isset($arg->value->inferredType)
&& $arg->value->inferredType->isSingleStringLiteral()
) {
$literal = $arg->value->inferredType->getSingleStringLiteral();
if (!$context->hasVariable('$' . $literal->value)) {
if (IssueBuffer::accepts(
new UndefinedVariable(
'Cannot find referenced variable $' . $literal->value,
new CodeLocation($statements_analyzer->getSource(), $arg->value)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}
if (self::checkFunctionLikeArgumentMatches(
$statements_analyzer,
$cased_method_id,

View File

@ -1787,6 +1787,12 @@ class FunctionCallTest extends TestCase
);
}',
],
'compactDefinedVariable' => [
'<?php
function foo(int $a, string $b, bool $c) : array {
return compact("a", "b", "c");
}',
],
];
}
@ -2331,6 +2337,13 @@ class FunctionCallTest extends TestCase
preg_match(\'/adsf/\');',
'error_message' => 'TooFewArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:21 - Too few arguments for method preg_match - expecting 2 but saw 1',
],
'compactUndefinedVariable' => [
'<?php
function foo() : array {
return compact("a", "b", "c");
}',
'error_message' => 'UndefinedVariable',
],
];
}
}