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

Fix var_export return type

Fixes #636
This commit is contained in:
Matthew Brown 2018-04-02 22:19:58 -04:00
parent 734b6915db
commit 4ff7db09f9
2 changed files with 28 additions and 1 deletions

View File

@ -74,7 +74,7 @@ class FunctionChecker extends FunctionLikeChecker
}
}
if (in_array($call_map_key, ['pathinfo'], true)) {
if ($call_map_key === 'pathinfo') {
if (isset($call_args[1])) {
return Type::getString();
}
@ -82,6 +82,21 @@ class FunctionChecker extends FunctionLikeChecker
return Type::getArray();
}
if ($call_map_key === 'var_export') {
if (isset($call_args[1]->value->inferredType)) {
/** @var Type\Union */
$subject_type = $call_args[1]->value->inferredType;
if ((string) $subject_type === 'true') {
return Type::getString();
}
return new Type\Union([new Type\Atomic\TString, new Type\Atomic\TNull]);
}
return Type::getVoid();
}
if (substr($call_map_key, 0, 6) === 'array_') {
$array_return_type = self::getArrayReturnType(
$statements_checker,

View File

@ -589,6 +589,13 @@ class FunctionCallTest extends TestCase
'<?php
$urls = array_map("implode", [["a", "b"]]);',
],
'varExport' => [
'<?php
$a = var_export(["a"], true);',
'assertions' => [
'$a' => 'string',
],
],
];
}
@ -805,6 +812,11 @@ class FunctionCallTest extends TestCase
array_map("foo", [1, 2, 3]);',
'error_message' => 'TooManyArguments',
],
'varExportAssignmentToVoid' => [
'<?php
$a = var_export(["a"]);',
'error_message' => 'AssignmentToVoid',
],
];
}
}