Skip to content
Permalink
Browse files

Add support for compact

  • Loading branch information...
muglug committed Jul 24, 2019
1 parent ea2074b commit 5770a6c904c33be736d952c31a6d596b8e1c3299
Showing with 33 additions and 0 deletions.
  1. +20 −0 src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php
  2. +13 −0 tests/FunctionCallTest.php
@@ -28,6 +28,7 @@
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 @@ protected static function checkFunctionLikeArgumentsMatch(
}
}
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,
@@ -1787,6 +1787,12 @@ function (array $matches) : string {
);
}',
],
'compactDefinedVariable' => [
'<?php
function foo(int $a, string $b, bool $c) : array {
return compact("a", "b", "c");
}',
],
];
}
@@ -2331,6 +2337,13 @@ function bar($s) : void {
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',
],
];
}
}

0 comments on commit 5770a6c

Please sign in to comment.
You can’t perform that action at this time.