2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
2016-08-14 01:44:24 +02:00
|
|
|
use Psalm\Config;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\EffectsAnalyser;
|
|
|
|
use Psalm\Exception\DocblockParseException;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\FunctionLikeParameter;
|
2016-10-28 19:24:06 +02:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-10-14 06:53:43 +02:00
|
|
|
use Psalm\Issue\InvalidReturnType;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\StatementsSource;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\Type;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
class FunctionChecker extends FunctionLikeChecker
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @var array<string,array<string,Type\Union|false>>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $function_return_types = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, string>>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $function_namespaces = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, string>>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $existing_functions = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $deprecated_functions = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $have_registered_function = [];
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-01 05:39:41 +01:00
|
|
|
* @var array<string, array<string, array<FunctionLikeParameter>>>
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected static $file_function_params = [];
|
2016-10-18 23:55:07 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-01 05:39:41 +01:00
|
|
|
* @var array<string, bool>
|
2016-10-18 23:55:07 +02:00
|
|
|
*/
|
|
|
|
protected static $variadic_functions = [];
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, array<int, FunctionLikeParameter>>
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected static $builtin_function_params = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected static $builtin_functions = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<array<string,string>>|null
|
|
|
|
*/
|
2016-08-22 21:00:12 +02:00
|
|
|
protected static $call_map = null;
|
2016-05-16 22:12:02 +02:00
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
/**
|
2016-11-05 02:14:04 +01:00
|
|
|
* @param mixed $function
|
2016-08-14 05:26:45 +02:00
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param string $base_file_name
|
2016-06-16 02:16:40 +02:00
|
|
|
*/
|
2016-10-23 07:57:11 +02:00
|
|
|
public function __construct($function, StatementsSource $source, $base_file_name)
|
2016-05-16 22:12:02 +02:00
|
|
|
{
|
2016-10-23 18:24:53 +02:00
|
|
|
if (!$function instanceof PhpParser\Node\Stmt\Function_) {
|
2016-10-23 07:57:11 +02:00
|
|
|
throw new \InvalidArgumentException('Bad');
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
parent::__construct($function, $source);
|
2016-05-16 22:12:02 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
$this->registerFunction($function, $base_file_name);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @param string $file_name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public static function functionExists($function_id, $file_name)
|
2016-02-27 01:11:11 +01:00
|
|
|
{
|
2016-08-14 05:26:45 +02:00
|
|
|
if (isset(self::$existing_functions[$file_name][$function_id])) {
|
|
|
|
return true;
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
if (strpos($function_id, '::') !== false) {
|
2016-08-15 19:37:21 +02:00
|
|
|
$function_id = strtolower(preg_replace('/^[^:]+::/', '', $function_id));
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
if (!isset(self::$builtin_functions[$function_id])) {
|
|
|
|
self::extractReflectionInfo($function_id);
|
|
|
|
}
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
return self::$builtin_functions[$function_id];
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @param string $file_name
|
|
|
|
* @return array<FunctionLikeParameter>
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public static function getParams($function_id, $file_name)
|
2016-08-11 01:21:03 +02:00
|
|
|
{
|
2016-08-14 05:26:45 +02:00
|
|
|
if (isset(self::$builtin_functions[$function_id]) && self::$builtin_functions[$function_id]) {
|
|
|
|
return self::$builtin_function_params[$function_id];
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
return self::$file_function_params[$file_name][$function_id];
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 23:55:07 +02:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @param string $file_name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function isVariadic($function_id, $file_name)
|
|
|
|
{
|
|
|
|
return isset(self::$variadic_functions[$file_name][$function_id]);
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected static function extractReflectionInfo($function_id)
|
2016-08-11 01:21:03 +02:00
|
|
|
{
|
2016-08-14 05:26:45 +02:00
|
|
|
try {
|
|
|
|
$reflection_function = new \ReflectionFunction($function_id);
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
$reflection_params = $reflection_function->getParameters();
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
self::$builtin_function_params[$function_id] = [];
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/** @var \ReflectionParameter $param */
|
2016-08-14 05:26:45 +02:00
|
|
|
foreach ($reflection_params as $param) {
|
|
|
|
self::$builtin_function_params[$function_id][] = self::getReflectionParamArray($param);
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
self::$builtin_functions[$function_id] = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (\ReflectionException $e) {
|
2016-08-14 05:26:45 +02:00
|
|
|
self::$builtin_functions[$function_id] = false;
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @param string $file_name
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
2016-08-14 19:13:53 +02:00
|
|
|
public static function getFunctionReturnTypes($function_id, $file_name)
|
|
|
|
{
|
|
|
|
if (!isset(self::$function_return_types[$file_name][$function_id])) {
|
2016-10-20 20:26:03 +02:00
|
|
|
throw new \InvalidArgumentException('Do not know function ' . $function_id . ' in file ' . $file_name);
|
2016-08-14 19:13:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
$function_return_types = self::$function_return_types[$file_name][$function_id];
|
|
|
|
|
|
|
|
return $function_return_types
|
|
|
|
? clone $function_return_types
|
2016-08-14 19:13:53 +02:00
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Function_ $function
|
|
|
|
* @param string $file_name
|
2016-10-30 17:46:18 +01:00
|
|
|
* @return null|false
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2016-08-14 03:14:32 +02:00
|
|
|
protected function registerFunction(PhpParser\Node\Stmt\Function_ $function, $file_name)
|
2016-08-11 01:21:03 +02:00
|
|
|
{
|
2016-11-21 03:49:06 +01:00
|
|
|
$function_id = ($this->namespace ? strtolower($this->namespace) . '\\' : '') . strtolower($function->name);
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
if (isset(self::$have_registered_function[$file_name][$function_id])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
self::$have_registered_function[$file_name][$function_id] = true;
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
self::$function_namespaces[$file_name][$function_id] = $this->namespace;
|
2016-08-14 05:26:45 +02:00
|
|
|
self::$existing_functions[$file_name][$function_id] = true;
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
self::$file_function_params[$file_name][$function_id] = [];
|
2016-08-11 01:21:03 +02:00
|
|
|
|
|
|
|
$function_param_names = [];
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/** @var PhpParser\Node\Param $param */
|
2016-08-11 01:21:03 +02:00
|
|
|
foreach ($function->getParams() as $param) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$param_array = self::getTranslatedParam(
|
|
|
|
$param,
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name,
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->namespace,
|
|
|
|
$this->getAliasedClasses()
|
|
|
|
);
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
self::$file_function_params[$file_name][$function_id][] = $param_array;
|
2016-10-09 23:54:58 +02:00
|
|
|
$function_param_names[$param->name] = $param_array->type;
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$config = Config::getInstance();
|
|
|
|
$return_type = null;
|
|
|
|
|
2016-10-28 16:54:20 +02:00
|
|
|
$docblock_info = null;
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-10-28 16:54:20 +02:00
|
|
|
$this->suppressed_issues = [];
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-10-28 16:54:20 +02:00
|
|
|
try {
|
|
|
|
$docblock_info = CommentChecker::extractDocblockInfo((string)$function->getDocComment());
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
2016-10-28 16:54:20 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
'Invalid type passed in docblock for ' . $this->getMethodId(),
|
|
|
|
$this->getCheckedFileName(),
|
|
|
|
$function->getLine()
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-19 03:54:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:54:20 +02:00
|
|
|
if ($docblock_info) {
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($docblock_info->deprecated) {
|
2016-10-28 16:54:20 +02:00
|
|
|
self::$deprecated_functions[$file_name][$function_id] = true;
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($docblock_info->variadic) {
|
2016-10-28 16:54:20 +02:00
|
|
|
self::$variadic_functions[$file_name][$function_id] = true;
|
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
$this->suppressed_issues = $docblock_info->suppress;
|
2016-10-28 16:54:20 +02:00
|
|
|
|
|
|
|
if ($function->returnType) {
|
|
|
|
$return_type = Type::parseString(
|
|
|
|
is_string($function->returnType)
|
|
|
|
? $function->returnType
|
2016-11-08 01:16:51 +01:00
|
|
|
: ClassLikeChecker::getFQCLNFromNameObject(
|
2016-11-02 07:29:00 +01:00
|
|
|
$function->returnType,
|
|
|
|
$this->namespace,
|
|
|
|
$this->getAliasedClasses()
|
|
|
|
)
|
2016-08-11 01:21:03 +02:00
|
|
|
);
|
|
|
|
}
|
2016-10-28 16:54:20 +02:00
|
|
|
|
|
|
|
if ($config->use_docblock_types) {
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($docblock_info->return_type) {
|
2016-10-28 16:54:20 +02:00
|
|
|
$return_type =
|
|
|
|
Type::parseString(
|
|
|
|
self::fixUpLocalType(
|
2016-11-13 05:59:31 +01:00
|
|
|
(string)$docblock_info->return_type,
|
2016-10-28 16:54:20 +02:00
|
|
|
null,
|
|
|
|
$this->namespace,
|
|
|
|
$this->getAliasedClasses()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($docblock_info->params) {
|
2016-10-28 16:54:20 +02:00
|
|
|
$this->improveParamsFromDocblock(
|
2016-11-13 05:59:31 +01:00
|
|
|
$docblock_info->params,
|
2016-10-28 16:54:20 +02:00
|
|
|
$function_param_names,
|
|
|
|
self::$file_function_params[$file_name][$function_id],
|
|
|
|
$function->getLine()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
self::$function_return_types[$file_name][$function_id] = $return_type ?: false;
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-11 01:21:03 +02:00
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $function_id
|
2016-11-21 05:57:37 +01:00
|
|
|
* @return array|null
|
2016-10-09 23:54:58 +02:00
|
|
|
* @psalm-return array<array<FunctionLikeParameter>>|null
|
2016-08-22 21:00:12 +02:00
|
|
|
*/
|
|
|
|
public static function getParamsFromCallMap($function_id)
|
|
|
|
{
|
|
|
|
$call_map = self::getCallMap();
|
|
|
|
|
|
|
|
$call_map_key = strtolower($function_id);
|
|
|
|
|
|
|
|
if (!isset($call_map[$call_map_key])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$call_map_functions = [];
|
|
|
|
$call_map_functions[] = $call_map[$call_map_key];
|
|
|
|
|
|
|
|
for ($i = 1; $i < 10; $i++) {
|
2016-11-02 07:29:00 +01:00
|
|
|
if (!isset($call_map[$call_map_key . '\'' . $i])) {
|
2016-08-22 21:00:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
$call_map_functions[] = $call_map[$call_map_key . '\'' . $i];
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$function_type_options = [];
|
|
|
|
|
|
|
|
foreach ($call_map_functions as $call_map_function_args) {
|
|
|
|
array_shift($call_map_function_args);
|
|
|
|
|
|
|
|
$function_types = [];
|
|
|
|
|
|
|
|
foreach ($call_map_function_args as $arg_name => $arg_type) {
|
|
|
|
$by_reference = false;
|
2016-10-26 17:51:59 +02:00
|
|
|
$optional = false;
|
2016-08-22 21:00:12 +02:00
|
|
|
|
|
|
|
if ($arg_name[0] === '&') {
|
|
|
|
$arg_name = substr($arg_name, 1);
|
|
|
|
$by_reference = true;
|
|
|
|
}
|
|
|
|
|
2016-10-26 17:51:59 +02:00
|
|
|
if (substr($arg_name, -1) === '=') {
|
|
|
|
$arg_name = substr($arg_name, 0, -1);
|
|
|
|
$optional = true;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$function_types[] = new FunctionLikeParameter(
|
|
|
|
$arg_name,
|
|
|
|
$by_reference,
|
|
|
|
$arg_type ? Type::parseString($arg_type) : Type::getMixed(),
|
2016-10-30 17:46:18 +01:00
|
|
|
$optional,
|
|
|
|
false,
|
|
|
|
$arg_name === '...'
|
2016-10-09 23:54:58 +02:00
|
|
|
);
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$function_type_options[] = $function_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $function_type_options;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-11-01 05:39:41 +01:00
|
|
|
* @param string $function_id
|
2016-10-14 06:53:43 +02:00
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2016-11-01 05:39:41 +01:00
|
|
|
public static function getReturnTypeFromCallMap($function_id)
|
|
|
|
{
|
2016-08-22 21:00:12 +02:00
|
|
|
$call_map_key = strtolower($function_id);
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
$call_map = self::getCallMap();
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
if (!isset($call_map[$call_map_key])) {
|
|
|
|
throw new \InvalidArgumentException('Function ' . $function_id . ' was not found in callmap');
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
if (!$call_map[$call_map_key][0]) {
|
|
|
|
return Type::getMixed();
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
return Type::parseString($call_map[$call_map_key][0]);
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
* @param string $file_name
|
|
|
|
* @param int $line_number
|
|
|
|
* @param array $suppressed_issues
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
public static function getReturnTypeFromCallMapWithArgs(
|
|
|
|
$function_id,
|
|
|
|
array $call_args,
|
|
|
|
$file_name,
|
|
|
|
$line_number,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
|
|
|
$call_map_key = strtolower($function_id);
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-08-30 06:05:13 +02:00
|
|
|
$call_map = self::getCallMap();
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
if (!isset($call_map[$call_map_key])) {
|
|
|
|
throw new \InvalidArgumentException('Function ' . $function_id . ' was not found in callmap');
|
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
if ($call_args) {
|
|
|
|
if (in_array($call_map_key, ['str_replace', 'preg_replace', 'preg_replace_callback'])) {
|
|
|
|
if (isset($call_args[2]->value->inferredType)) {
|
|
|
|
|
|
|
|
/** @var Type\Union */
|
|
|
|
$subject_type = $call_args[2]->value->inferredType;
|
|
|
|
|
|
|
|
if (!$subject_type->hasString() && $subject_type->hasArray()) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($call_map_key, ['pathinfo'])) {
|
|
|
|
if (isset($call_args[1])) {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if (substr($call_map_key, 0, 6) === 'array_') {
|
2016-11-04 01:51:56 +01:00
|
|
|
$array_return_type = self::getArrayReturnType(
|
|
|
|
$call_map_key,
|
|
|
|
$call_args,
|
|
|
|
$file_name,
|
|
|
|
$line_number,
|
|
|
|
$suppressed_issues
|
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
if ($array_return_type) {
|
|
|
|
return $array_return_type;
|
2016-08-30 06:05:13 +02:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-30 06:05:13 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($call_map_key === 'explode' || $call_map_key === 'preg_split') {
|
|
|
|
return Type::parseString('array<int, string>');
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 23:51:34 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if (!$call_map[$call_map_key][0]) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
2016-10-26 23:51:34 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
return Type::parseString($call_map[$call_map_key][0]);
|
|
|
|
}
|
2016-10-26 23:51:34 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $call_map_key
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
* @param string $file_name
|
|
|
|
* @param int $line_number
|
|
|
|
* @param array $suppressed_issues
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
2016-11-04 01:51:56 +01:00
|
|
|
protected static function getArrayReturnType(
|
|
|
|
$call_map_key,
|
|
|
|
$call_args,
|
|
|
|
$file_name,
|
|
|
|
$line_number,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($call_map_key === 'array_map' || $call_map_key === 'array_filter') {
|
|
|
|
return self::getArrayMapReturnType($call_map_key, $call_args, $file_name, $line_number, $suppressed_issues);
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
$first_arg = isset($call_args[0]->value) ? $call_args[0]->value : null;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
$first_arg_array = $first_arg
|
2016-11-02 07:29:00 +01:00
|
|
|
&& isset($first_arg->inferredType)
|
|
|
|
&& isset($first_arg->inferredType->types['array'])
|
|
|
|
&& $first_arg->inferredType->types['array'] instanceof Type\Generic
|
|
|
|
? $first_arg->inferredType->types['array']
|
|
|
|
: null;
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
if ($call_map_key === 'array_values' || $call_map_key === 'array_unique') {
|
|
|
|
if ($first_arg_array) {
|
|
|
|
$inner_type = clone $first_arg_array->type_params[1];
|
|
|
|
return new Type\Union([new Type\Generic('array', [Type::getInt(), $inner_type])]);
|
2016-10-14 06:53:43 +02:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-10-14 06:53:43 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($call_map_key === 'array_keys') {
|
|
|
|
if ($first_arg_array) {
|
|
|
|
$inner_type = clone $first_arg_array->type_params[0];
|
|
|
|
return new Type\Union([new Type\Generic('array', [Type::getInt(), $inner_type])]);
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($call_map_key === 'array_merge') {
|
|
|
|
$inner_value_types = [];
|
|
|
|
$inner_key_types = [];
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
foreach ($call_args as $offset => $call_arg) {
|
|
|
|
if (!isset($call_arg->value->inferredType)) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
foreach ($call_arg->value->inferredType->types as $type_part) {
|
|
|
|
if (!$type_part instanceof Type\Generic) {
|
2016-08-22 21:00:12 +02:00
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($type_part->type_params[1]->isEmpty()) {
|
|
|
|
continue;
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
$inner_key_types = array_merge(array_values($type_part->type_params[0]->types), $inner_key_types);
|
2016-11-04 01:51:56 +01:00
|
|
|
$inner_value_types = array_merge(
|
|
|
|
array_values($type_part->type_params[1]->types),
|
|
|
|
$inner_value_types
|
|
|
|
);
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($inner_value_types) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Generic(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
Type::combineTypes($inner_key_types),
|
|
|
|
Type::combineTypes($inner_value_types)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($call_map_key === 'array_diff') {
|
|
|
|
if (!$first_arg_array) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return Type::getArray();
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
return new Type\Union([
|
|
|
|
new Type\Generic(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
Type::getInt(),
|
|
|
|
clone $first_arg_array->type_params[1]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($call_map_key === 'array_diff_key') {
|
|
|
|
if (!$first_arg_array) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone $first_arg->inferredType;
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($call_map_key === 'array_shift' || $call_map_key === 'array_pop') {
|
|
|
|
if (!$first_arg_array) {
|
|
|
|
return Type::getMixed();
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
return clone $first_arg_array->type_params[1];
|
|
|
|
}
|
2016-11-04 01:51:56 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $call_map_key
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
* @param string $file_name
|
|
|
|
* @param int $line_number
|
|
|
|
* @param array $suppressed_issues
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2016-11-04 01:51:56 +01:00
|
|
|
protected static function getArrayMapReturnType(
|
|
|
|
$call_map_key,
|
|
|
|
$call_args,
|
|
|
|
$file_name,
|
|
|
|
$line_number,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
2016-11-02 14:24:36 +01:00
|
|
|
$function_index = $call_map_key === 'array_map' ? 0 : 1;
|
|
|
|
$array_index = $call_map_key === 'array_map' ? 1 : 0;
|
|
|
|
|
|
|
|
$array_arg = isset($call_args[$array_index]->value) ? $call_args[$array_index]->value : null;
|
|
|
|
|
|
|
|
$array_arg_type = $array_arg
|
2016-11-02 07:29:00 +01:00
|
|
|
&& isset($array_arg->inferredType)
|
|
|
|
&& isset($array_arg->inferredType->types['array'])
|
|
|
|
&& $array_arg->inferredType->types['array'] instanceof Type\Generic
|
|
|
|
? $array_arg->inferredType->types['array']
|
|
|
|
: null;
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
if (isset($call_args[$function_index])) {
|
|
|
|
$function_call_arg = $call_args[$function_index];
|
|
|
|
|
|
|
|
if ($function_call_arg->value instanceof PhpParser\Node\Expr\Closure) {
|
|
|
|
$closure_yield_types = [];
|
2016-11-04 01:51:56 +01:00
|
|
|
$closure_return_types = EffectsAnalyser::getReturnTypes(
|
|
|
|
$function_call_arg->value->stmts,
|
|
|
|
$closure_yield_types,
|
|
|
|
true
|
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
if (!$closure_return_types) {
|
|
|
|
IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
|
|
|
'No return type could be found in the closure passed to ' . $call_map_key,
|
|
|
|
$file_name,
|
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
);
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
return Type::getArray();
|
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($call_map_key === 'array_map') {
|
|
|
|
$inner_type = new Type\Union($closure_return_types);
|
|
|
|
return new Type\Union([new Type\Generic('array', [Type::getInt(), $inner_type])]);
|
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($array_arg_type) {
|
|
|
|
$inner_type = clone $array_arg_type->type_params[1];
|
|
|
|
return new Type\Union([new Type\Generic('array', [Type::getInt(), $inner_type])]);
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
} elseif ($function_call_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$mapped_function_id = strtolower($function_call_arg->value->value);
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
$call_map = self::getCallMap();
|
2016-10-12 07:37:32 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if (isset($call_map[$mapped_function_id][0])) {
|
|
|
|
if ($call_map[$mapped_function_id][0]) {
|
|
|
|
$mapped_function_return = Type::parseString($call_map[$mapped_function_id][0]);
|
|
|
|
return new Type\Union([new Type\Generic('array', [Type::getInt(), $mapped_function_return])]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// @todo handle array_map('some_custom_function', $arr)
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
2016-10-12 07:37:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
// where there's no function passed to array_filter
|
|
|
|
if ($call_map_key === 'array_filter' && $array_arg_type) {
|
|
|
|
$inner_type = clone $array_arg_type->type_params[1];
|
|
|
|
|
|
|
|
$second_arg = isset($call_args[1]->value) ? $call_args[1]->value : null;
|
|
|
|
|
|
|
|
if (!$second_arg) {
|
|
|
|
$inner_type->removeType('null');
|
|
|
|
$inner_type->removeType('false');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Type\Union([new Type\Generic('array', [Type::getInt(), $inner_type])]);
|
2016-08-23 00:09:52 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
return Type::getArray();
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the method/function call map
|
|
|
|
*
|
2016-11-05 22:53:30 +01:00
|
|
|
* @return array<array<string, string>>
|
2016-11-05 23:46:17 +01:00
|
|
|
* @psalm-suppress MixedInferredReturnType as the use of require buggers things up
|
2016-08-22 21:00:12 +02:00
|
|
|
*/
|
|
|
|
protected static function getCallMap()
|
|
|
|
{
|
|
|
|
if (self::$call_map !== null) {
|
|
|
|
return self::$call_map;
|
|
|
|
}
|
|
|
|
|
2016-11-05 22:53:30 +01:00
|
|
|
/** @var array<array<string, string>> */
|
2016-08-22 21:00:12 +02:00
|
|
|
$call_map = require_once(__DIR__.'/../CallMap.php');
|
|
|
|
|
|
|
|
self::$call_map = [];
|
|
|
|
|
|
|
|
foreach ($call_map as $key => $value) {
|
2016-10-22 23:35:59 +02:00
|
|
|
$cased_key = strtolower($key);
|
|
|
|
self::$call_map[$cased_key] = $value;
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$call_map;
|
|
|
|
}
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-22 23:35:59 +02:00
|
|
|
public static function inCallMap($key)
|
|
|
|
{
|
|
|
|
return isset(self::getCallMap()[strtolower($key)]);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-21 00:12:13 +02:00
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$function_return_types = [];
|
|
|
|
self::$function_namespaces = [];
|
|
|
|
self::$existing_functions = [];
|
|
|
|
self::$deprecated_functions = [];
|
|
|
|
self::$have_registered_function = [];
|
|
|
|
|
|
|
|
self::$file_function_params = [];
|
|
|
|
|
|
|
|
self::$variadic_functions = [];
|
|
|
|
|
|
|
|
self::$builtin_function_params = [];
|
|
|
|
self::$builtin_functions = [];
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|