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;
|
2018-01-17 22:07:46 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\AssertionFinder;
|
2018-02-04 00:52:35 +01:00
|
|
|
use Psalm\Codebase\CallMap;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
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;
|
2018-01-17 22:07:46 +01:00
|
|
|
use Psalm\Type\Reconciler;
|
2018-05-20 01:44:03 +02:00
|
|
|
use Psalm\Type\TypeCombination;
|
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-06-16 02:16:40 +02:00
|
|
|
/**
|
2016-08-14 05:26:45 +02:00
|
|
|
* @param StatementsSource $source
|
2016-06-16 02:16:40 +02:00
|
|
|
*/
|
2018-01-08 06:14:02 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\Function_ $function, StatementsSource $source)
|
2016-05-16 22:12:02 +02:00
|
|
|
{
|
2016-08-14 05:26:45 +02:00
|
|
|
parent::__construct($function, $source);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-11-01 05:39:41 +01:00
|
|
|
* @param array $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-01 05:39:41 +01:00
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
public static function getReturnTypeFromCallMapWithArgs(
|
2017-08-15 01:30:11 +02:00
|
|
|
StatementsChecker $statements_checker,
|
2016-11-01 05:39:41 +01:00
|
|
|
$function_id,
|
|
|
|
array $call_args,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location,
|
2016-11-01 05:39:41 +01:00
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
|
|
|
$call_map_key = strtolower($function_id);
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$call_map = CallMap::getCallMap();
|
2016-08-30 06:05:13 +02:00
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2018-01-08 06:09:22 +01:00
|
|
|
if ($call_map_key === 'getenv') {
|
2018-01-21 03:22:33 +01:00
|
|
|
if (!empty($call_args)) {
|
2018-01-08 06:09:22 +01:00
|
|
|
return new Type\Union([new Type\Atomic\TString, new Type\Atomic\TFalse]);
|
|
|
|
}
|
|
|
|
|
2018-01-08 06:14:02 +01:00
|
|
|
return new Type\Union([new Type\Atomic\TArray([Type::getMixed(), Type::getString()])]);
|
2018-01-08 06:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
if ($call_args) {
|
2018-05-09 06:34:44 +02:00
|
|
|
if (in_array(
|
|
|
|
$call_map_key,
|
2018-05-09 06:39:45 +02:00
|
|
|
['str_replace', 'str_ireplace', 'substr_replace', 'preg_replace', 'preg_replace_callback'],
|
2018-05-09 06:34:44 +02:00
|
|
|
true
|
|
|
|
)) {
|
2016-11-01 05:39:41 +01:00
|
|
|
if (isset($call_args[2]->value->inferredType)) {
|
|
|
|
$subject_type = $call_args[2]->value->inferredType;
|
|
|
|
|
|
|
|
if (!$subject_type->hasString() && $subject_type->hasArray()) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
2018-01-24 23:07:03 +01:00
|
|
|
$return_type = Type::getString();
|
|
|
|
|
|
|
|
if (in_array($call_map_key, ['preg_replace', 'preg_replace_callback'], true)) {
|
|
|
|
$return_type->addType(new Type\Atomic\TNull());
|
|
|
|
$return_type->ignore_nullable_issues = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return_type;
|
2018-04-07 21:16:46 +02:00
|
|
|
} else {
|
|
|
|
return Type::getMixed();
|
2016-11-01 05:39:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 04:19:58 +02:00
|
|
|
if ($call_map_key === 'pathinfo') {
|
2016-11-01 05:39:41 +01:00
|
|
|
if (isset($call_args[1])) {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
if ($call_map_key === 'count' && isset($call_args[0]->value->inferredType)) {
|
|
|
|
$atomic_types = $call_args[0]->value->inferredType->getTypes();
|
|
|
|
|
|
|
|
if (count($atomic_types) === 1 && isset($atomic_types['array'])) {
|
|
|
|
if ($atomic_types['array'] instanceof Type\Atomic\TArray) {
|
|
|
|
return new Type\Union([
|
2018-05-18 17:02:50 +02:00
|
|
|
$atomic_types['array']->count !== null
|
|
|
|
? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
|
2018-05-03 19:56:30 +02:00
|
|
|
: new Type\Atomic\TInt
|
|
|
|
]);
|
|
|
|
} elseif ($atomic_types['array'] instanceof Type\Atomic\ObjectLike
|
|
|
|
&& $atomic_types['array']->sealed
|
|
|
|
) {
|
|
|
|
return new Type\Union([
|
2018-05-18 17:02:50 +02:00
|
|
|
new Type\Atomic\TLiteralInt(count($atomic_types['array']->properties))
|
2018-05-03 19:56:30 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 06:31:27 +02:00
|
|
|
if ($call_map_key === 'var_export'
|
|
|
|
|| $call_map_key === 'highlight_string'
|
|
|
|
|| $call_map_key === 'highlight_file'
|
|
|
|
) {
|
2018-04-03 04:19:58 +02:00
|
|
|
if (isset($call_args[1]->value->inferredType)) {
|
|
|
|
$subject_type = $call_args[1]->value->inferredType;
|
|
|
|
|
|
|
|
if ((string) $subject_type === 'true') {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
|
2018-04-30 07:49:55 +02:00
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TString,
|
|
|
|
$call_map_key === 'var_export' ? new Type\Atomic\TNull : new Type\Atomic\TBool
|
|
|
|
]);
|
2018-04-03 04:19:58 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 07:49:55 +02:00
|
|
|
return $call_map_key === 'var_export' ? Type::getVoid() : Type::getBool();
|
2018-04-03 04:19:58 +02:00
|
|
|
}
|
|
|
|
|
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(
|
2017-08-15 01:30:11 +02:00
|
|
|
$statements_checker,
|
2016-11-04 01:51:56 +01:00
|
|
|
$call_map_key,
|
|
|
|
$call_args,
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location,
|
2016-11-04 01:51:56 +01:00
|
|
|
$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
|
|
|
|
2018-04-16 22:03:04 +02:00
|
|
|
if ($call_map_key === 'explode'
|
|
|
|
&& $call_args[0]->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
) {
|
|
|
|
if ($call_args[0]->value->value === '') {
|
|
|
|
return Type::getFalse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
Type::getString()
|
|
|
|
])
|
|
|
|
]);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2018-05-19 00:10:10 +02:00
|
|
|
if ($call_map_key === 'iterator_to_array'
|
|
|
|
&& isset($call_args[0]->value->inferredType)
|
|
|
|
&& $call_args[0]->value->inferredType->hasObjectType()
|
|
|
|
) {
|
|
|
|
$value_type = null;
|
|
|
|
|
|
|
|
foreach ($call_args[0]->value->inferredType->getTypes() as $call_arg_atomic_type) {
|
|
|
|
if ($call_arg_atomic_type instanceof Type\Atomic\TGenericObject) {
|
|
|
|
$type_params = $call_arg_atomic_type->type_params;
|
|
|
|
$last_param_type = $type_params[count($type_params) - 1];
|
|
|
|
|
|
|
|
$value_type = $value_type
|
|
|
|
? Type::combineUnionTypes($value_type, $last_param_type)
|
|
|
|
: $last_param_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value_type) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getMixed(),
|
|
|
|
$value_type
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 23:05:36 +01:00
|
|
|
if ($call_map_key === 'abs'
|
|
|
|
&& isset($call_args[0]->value)
|
|
|
|
) {
|
|
|
|
$first_arg = $call_args[0]->value;
|
|
|
|
|
|
|
|
if (isset($first_arg->inferredType)) {
|
|
|
|
$numeric_types = [];
|
|
|
|
|
|
|
|
foreach ($first_arg->inferredType->getTypes() as $inner_type) {
|
|
|
|
if ($inner_type->isNumericType()) {
|
|
|
|
$numeric_types[] = $inner_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($numeric_types) {
|
|
|
|
return new Type\Union($numeric_types);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
if ($call_map_key === 'min' || $call_map_key === 'max') {
|
|
|
|
if (isset($call_args[0])) {
|
|
|
|
$first_arg = $call_args[0]->value;
|
|
|
|
|
2016-12-31 02:05:32 +01:00
|
|
|
if (isset($first_arg->inferredType)) {
|
2016-12-17 00:56:23 +01:00
|
|
|
if ($first_arg->inferredType->hasArray()) {
|
2018-01-09 21:05:48 +01:00
|
|
|
$array_type = $first_arg->inferredType->getTypes()['array'];
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($array_type instanceof Type\Atomic\ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
return $array_type->getGenericValueType();
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($array_type instanceof Type\Atomic\TArray) {
|
2016-12-17 00:56:23 +01:00
|
|
|
return clone $array_type->type_params[1];
|
|
|
|
}
|
|
|
|
} elseif ($first_arg->inferredType->hasScalarType() &&
|
|
|
|
($second_arg = $call_args[1]->value) &&
|
2016-12-31 02:39:12 +01:00
|
|
|
isset($second_arg->inferredType) &&
|
2016-12-17 00:56:23 +01:00
|
|
|
$second_arg->inferredType->hasScalarType()
|
|
|
|
) {
|
|
|
|
return Type::combineUnionTypes($first_arg->inferredType, $second_arg->inferredType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
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
|
|
|
|
2018-01-25 19:07:36 +01:00
|
|
|
$call_map_return_type = Type::parseString($call_map[$call_map_key][0]);
|
|
|
|
|
|
|
|
if (!in_array(
|
|
|
|
$call_map_key,
|
|
|
|
['mb_strpos', 'mb_strrpos', 'mb_stripos', 'mb_strripos', 'strpos', 'strrpos', 'stripos', 'strripos'],
|
|
|
|
true
|
|
|
|
) && $call_map_return_type->isFalsable()
|
|
|
|
) {
|
|
|
|
$call_map_return_type->ignore_falsable_issues = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $call_map_return_type;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
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
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-11-02 14:24:36 +01:00
|
|
|
* @param array $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 14:24:36 +01:00
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
2016-11-04 01:51:56 +01:00
|
|
|
protected static function getArrayReturnType(
|
2017-08-15 01:30:11 +02:00
|
|
|
StatementsChecker $statements_checker,
|
2016-11-04 01:51:56 +01:00
|
|
|
$call_map_key,
|
|
|
|
$call_args,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location,
|
2016-11-04 01:51:56 +01:00
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
2016-12-25 02:33:14 +01:00
|
|
|
if ($call_map_key === 'array_map') {
|
2017-08-15 01:30:11 +02:00
|
|
|
return self::getArrayMapReturnType(
|
|
|
|
$statements_checker,
|
2018-01-17 22:07:46 +01:00
|
|
|
$call_args,
|
|
|
|
$code_location,
|
|
|
|
$suppressed_issues
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($call_map_key === 'array_filter') {
|
|
|
|
return self::getArrayFilterReturnType(
|
|
|
|
$statements_checker,
|
2017-08-15 01:30:11 +02:00
|
|
|
$call_args,
|
|
|
|
$code_location,
|
|
|
|
$suppressed_issues
|
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
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;
|
2017-01-17 17:17:49 +01:00
|
|
|
$second_arg = isset($call_args[1]->value) ? $call_args[1]->value : null;
|
2016-10-14 06:53:43 +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
|
|
|
|
2017-11-11 20:19:45 +01:00
|
|
|
$generic_properties = [];
|
|
|
|
|
2017-11-09 05:14:27 +01:00
|
|
|
foreach ($call_args as $call_arg) {
|
2016-11-02 14:24:36 +01:00
|
|
|
if (!isset($call_arg->value->inferredType)) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($call_arg->value->inferredType->getTypes() as $type_part) {
|
2017-12-23 02:09:58 +01:00
|
|
|
if ($call_arg->unpack) {
|
|
|
|
if (!$type_part instanceof Type\Atomic\TArray) {
|
|
|
|
if ($type_part instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$type_part_value_type = $type_part->getGenericValueType();
|
|
|
|
} else {
|
|
|
|
return Type::getArray();
|
2017-11-11 20:19:45 +01:00
|
|
|
}
|
2017-05-06 00:53:45 +02:00
|
|
|
} else {
|
2018-03-13 18:50:41 +01:00
|
|
|
$type_part_value_type = $type_part->type_params[1];
|
2017-05-06 00:53:45 +02:00
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2017-12-23 02:09:58 +01:00
|
|
|
$unpacked_type_parts = [];
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($type_part_value_type->getTypes() as $value_type_part) {
|
2017-12-23 02:09:58 +01:00
|
|
|
$unpacked_type_parts[] = $value_type_part;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$unpacked_type_parts = [$type_part];
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2017-12-23 02:09:58 +01:00
|
|
|
foreach ($unpacked_type_parts as $unpacked_type_part) {
|
|
|
|
if (!$unpacked_type_part instanceof Type\Atomic\TArray) {
|
|
|
|
if ($unpacked_type_part instanceof Type\Atomic\ObjectLike) {
|
|
|
|
if ($generic_properties !== null) {
|
|
|
|
$generic_properties = array_merge(
|
|
|
|
$generic_properties,
|
|
|
|
$unpacked_type_part->properties
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$unpacked_type_part = $unpacked_type_part->getGenericArrayType();
|
|
|
|
} else {
|
2018-04-07 00:28:22 +02:00
|
|
|
if ($unpacked_type_part instanceof Type\Atomic\TMixed
|
|
|
|
&& $unpacked_type_part->from_isset
|
|
|
|
) {
|
|
|
|
$unpacked_type_part = new Type\Atomic\TArray([
|
|
|
|
Type::getMixed(),
|
|
|
|
Type::getMixed(true)
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
2017-12-23 02:09:58 +01:00
|
|
|
}
|
|
|
|
} elseif (!$unpacked_type_part->type_params[0]->isEmpty()) {
|
|
|
|
$generic_properties = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($unpacked_type_part->type_params[1]->isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inner_key_types = array_merge(
|
|
|
|
$inner_key_types,
|
2018-01-09 21:05:48 +01:00
|
|
|
array_values($unpacked_type_part->type_params[0]->getTypes())
|
2017-12-23 02:09:58 +01:00
|
|
|
);
|
|
|
|
$inner_value_types = array_merge(
|
|
|
|
$inner_value_types,
|
2018-01-09 21:05:48 +01:00
|
|
|
array_values($unpacked_type_part->type_params[1]->getTypes())
|
2017-12-23 02:09:58 +01:00
|
|
|
);
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
2017-01-19 07:32:35 +01:00
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2017-11-11 20:19:45 +01:00
|
|
|
if ($generic_properties) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\ObjectLike($generic_properties),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-01-19 07:32:35 +01:00
|
|
|
if ($inner_value_types) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
2018-05-20 01:44:03 +02:00
|
|
|
TypeCombination::combineTypes($inner_key_types),
|
|
|
|
TypeCombination::combineTypes($inner_value_types),
|
2017-05-27 02:05:57 +02:00
|
|
|
]),
|
2017-01-19 07:32:35 +01:00
|
|
|
]);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
2017-11-09 03:56:54 +01:00
|
|
|
if ($call_map_key === 'array_rand') {
|
|
|
|
$first_arg_array = $first_arg
|
|
|
|
&& isset($first_arg->inferredType)
|
2018-01-09 21:05:48 +01:00
|
|
|
&& $first_arg->inferredType->hasType('array')
|
|
|
|
&& ($array_atomic_type = $first_arg->inferredType->getTypes()['array'])
|
|
|
|
&& ($array_atomic_type instanceof Type\Atomic\TArray ||
|
|
|
|
$array_atomic_type instanceof Type\Atomic\ObjectLike)
|
|
|
|
? $array_atomic_type
|
2017-11-09 03:56:54 +01:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!$first_arg_array) {
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($first_arg_array instanceof Type\Atomic\TArray) {
|
|
|
|
$key_type = clone $first_arg_array->type_params[0];
|
|
|
|
} else {
|
2017-12-19 00:47:17 +01:00
|
|
|
$key_type = $first_arg_array->getGenericKeyType();
|
2017-11-09 03:56:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$second_arg
|
|
|
|
|| ($second_arg instanceof PhpParser\Node\Scalar\LNumber && $second_arg->value === 1)
|
|
|
|
) {
|
|
|
|
return $key_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr_type = new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
$key_type,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
return $arr_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::combineUnionTypes($key_type, $arr_type);
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
return null;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-11-02 14:24:36 +01:00
|
|
|
* @param array $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 14:24:36 +01:00
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2016-11-04 01:51:56 +01:00
|
|
|
protected static function getArrayMapReturnType(
|
2017-08-15 01:30:11 +02:00
|
|
|
StatementsChecker $statements_checker,
|
2016-11-04 01:51:56 +01:00
|
|
|
$call_args,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location,
|
2016-11-04 01:51:56 +01:00
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
2018-01-17 22:07:46 +01:00
|
|
|
$array_arg = isset($call_args[1]->value) ? $call_args[1]->value : null;
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2018-02-06 19:52:59 +01:00
|
|
|
$array_arg_type = null;
|
|
|
|
|
|
|
|
if ($array_arg && isset($array_arg->inferredType)) {
|
|
|
|
$arg_types = $array_arg->inferredType->getTypes();
|
|
|
|
|
2018-02-26 16:39:48 +01:00
|
|
|
if (isset($arg_types['array'])
|
|
|
|
&& ($arg_types['array'] instanceof Type\Atomic\TArray
|
|
|
|
|| $arg_types['array'] instanceof Type\Atomic\ObjectLike)
|
|
|
|
) {
|
2018-02-06 19:52:59 +01:00
|
|
|
$array_arg_type = $arg_types['array'];
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2018-01-17 22:07:46 +01:00
|
|
|
if (isset($call_args[0])) {
|
|
|
|
$function_call_arg = $call_args[0];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2018-02-26 16:39:48 +01:00
|
|
|
if (count($call_args) === 2) {
|
|
|
|
if ($array_arg_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$generic_key_type = $array_arg_type->getGenericKeyType();
|
|
|
|
} else {
|
|
|
|
$generic_key_type = $array_arg_type ? clone $array_arg_type->type_params[0] : Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$generic_key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
if ($function_call_arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
&& isset($function_call_arg->value->inferredType)
|
|
|
|
&& ($closure_atomic_type = $function_call_arg->value->inferredType->getTypes()['Closure'])
|
|
|
|
&& $closure_atomic_type instanceof Type\Atomic\Fn
|
2016-12-07 20:13:39 +01:00
|
|
|
) {
|
2018-03-27 04:13:10 +02:00
|
|
|
$closure_return_type = $closure_atomic_type->return_type ?: Type::getMixed();
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if ($closure_return_type->isVoid()) {
|
2016-11-02 14:24:36 +01:00
|
|
|
IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
2018-01-17 22:07:46 +01:00
|
|
|
'No return type could be found in the closure passed to array_map',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-02 14:24:36 +01:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
);
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
return Type::getArray();
|
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2018-01-17 22:07:46 +01:00
|
|
|
$inner_type = clone $closure_return_type;
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2018-02-26 17:01:30 +01:00
|
|
|
if ($array_arg_type instanceof Type\Atomic\ObjectLike && count($call_args) === 2) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\ObjectLike(
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2018-02-27 17:39:26 +01:00
|
|
|
function (Type\Union $_) use ($inner_type) {
|
2018-02-26 17:01:30 +01:00
|
|
|
return clone $inner_type;
|
|
|
|
},
|
|
|
|
$array_arg_type->properties
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-01-17 22:07:46 +01:00
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
2018-02-26 16:39:48 +01:00
|
|
|
$generic_key_type,
|
2018-01-17 22:07:46 +01:00
|
|
|
$inner_type,
|
|
|
|
]),
|
|
|
|
]);
|
2017-08-15 01:30:11 +02:00
|
|
|
} elseif ($function_call_arg->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $function_call_arg->value instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
|
|
|
$mapping_function_ids = Statements\Expression\CallChecker::getFunctionIdsFromCallableArg(
|
|
|
|
$statements_checker,
|
|
|
|
$function_call_arg->value
|
|
|
|
);
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$call_map = CallMap::getCallMap();
|
2016-10-12 07:37:32 +02:00
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
$mapping_return_type = null;
|
|
|
|
|
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|
|
|
foreach ($mapping_function_ids as $mapping_function_id) {
|
2018-03-05 15:05:38 +01:00
|
|
|
$mapping_function_id = strtolower($mapping_function_id);
|
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
$mapping_function_id_parts = explode('&', $mapping_function_id);
|
2017-08-18 23:02:15 +02:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
$part_match_found = false;
|
2017-10-20 00:23:18 +02:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
foreach ($mapping_function_id_parts as $mapping_function_id_part) {
|
|
|
|
if (isset($call_map[$mapping_function_id_part][0])) {
|
|
|
|
if ($call_map[$mapping_function_id_part][0]) {
|
|
|
|
$mapped_function_return =
|
|
|
|
Type::parseString($call_map[$mapping_function_id_part][0]);
|
2018-01-26 16:59:30 +01:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
if ($mapping_return_type) {
|
|
|
|
$mapping_return_type = Type::combineUnionTypes(
|
|
|
|
$mapping_return_type,
|
|
|
|
$mapped_function_return
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$mapping_return_type = $mapped_function_return;
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
$part_match_found = true;
|
2017-08-15 01:30:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-19 01:29:41 +01:00
|
|
|
if (strpos($mapping_function_id_part, '::') !== false) {
|
|
|
|
list($callable_fq_class_name) = explode('::', $mapping_function_id_part);
|
2017-10-20 00:23:18 +02:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
if (in_array($callable_fq_class_name, ['self', 'static', 'parent'], true)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
if (!$codebase->methodExists($mapping_function_id_part)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
2018-03-19 01:29:41 +01:00
|
|
|
$part_match_found = true;
|
|
|
|
|
|
|
|
$self_class = 'self';
|
|
|
|
|
|
|
|
$return_type = $codebase->methods->getMethodReturnType(
|
|
|
|
$mapping_function_id_part,
|
|
|
|
$self_class
|
|
|
|
) ?: Type::getMixed();
|
|
|
|
|
|
|
|
if ($mapping_return_type) {
|
|
|
|
$mapping_return_type = Type::combineUnionTypes(
|
|
|
|
$mapping_return_type,
|
|
|
|
$return_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$mapping_return_type = $return_type;
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
} else {
|
2018-03-19 01:29:41 +01:00
|
|
|
if (!$codebase->functions->functionExists(
|
|
|
|
$statements_checker,
|
|
|
|
$mapping_function_id_part
|
|
|
|
)) {
|
|
|
|
$mapping_return_type = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$part_match_found = true;
|
|
|
|
|
|
|
|
$function_storage = $codebase->functions->getStorage(
|
|
|
|
$statements_checker,
|
|
|
|
$mapping_function_id_part
|
|
|
|
);
|
|
|
|
|
|
|
|
$return_type = $function_storage->return_type ?: Type::getMixed();
|
|
|
|
|
|
|
|
if ($mapping_return_type) {
|
|
|
|
$mapping_return_type = Type::combineUnionTypes(
|
|
|
|
$mapping_return_type,
|
|
|
|
$return_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$mapping_return_type = $return_type;
|
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2018-03-19 01:29:41 +01:00
|
|
|
|
|
|
|
if ($part_match_found === false) {
|
|
|
|
$mapping_return_type = Type::getMixed();
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|
|
|
if ($mapping_return_type) {
|
2018-02-26 17:01:30 +01:00
|
|
|
if ($array_arg_type instanceof Type\Atomic\ObjectLike && count($call_args) === 2) {
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\ObjectLike(
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2018-02-27 17:39:26 +01:00
|
|
|
function (Type\Union $_) use ($mapping_return_type) {
|
2018-02-26 17:01:30 +01:00
|
|
|
return clone $mapping_return_type;
|
|
|
|
},
|
|
|
|
$array_arg_type->properties
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
2018-02-26 16:39:48 +01:00
|
|
|
$generic_key_type,
|
2017-08-15 01:30:11 +02:00
|
|
|
$mapping_return_type,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
2016-10-12 07:37:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
return Type::getArray();
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 22:07:46 +01:00
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @param array $suppressed_issues
|
|
|
|
*
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
protected static function getArrayFilterReturnType(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
$call_args,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
|
|
|
$array_arg = isset($call_args[0]->value) ? $call_args[0]->value : null;
|
|
|
|
|
|
|
|
$first_arg_array = $array_arg
|
|
|
|
&& isset($array_arg->inferredType)
|
|
|
|
&& $array_arg->inferredType->hasType('array')
|
|
|
|
&& ($array_atomic_type = $array_arg->inferredType->getTypes()['array'])
|
|
|
|
&& ($array_atomic_type instanceof Type\Atomic\TArray ||
|
|
|
|
$array_atomic_type instanceof Type\Atomic\ObjectLike)
|
|
|
|
? $array_atomic_type
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!$first_arg_array) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($first_arg_array instanceof Type\Atomic\TArray) {
|
|
|
|
$inner_type = $first_arg_array->type_params[1];
|
|
|
|
$key_type = clone $first_arg_array->type_params[0];
|
|
|
|
} else {
|
|
|
|
$inner_type = $first_arg_array->getGenericValueType();
|
|
|
|
$key_type = $first_arg_array->getGenericKeyType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($call_args[1])) {
|
|
|
|
$inner_type->removeType('null');
|
|
|
|
$inner_type->removeType('false');
|
|
|
|
} elseif (!isset($call_args[2])) {
|
|
|
|
$function_call_arg = $call_args[1];
|
|
|
|
|
|
|
|
if ($function_call_arg->value instanceof PhpParser\Node\Expr\Closure
|
|
|
|
&& isset($function_call_arg->value->inferredType)
|
|
|
|
&& ($closure_atomic_type = $function_call_arg->value->inferredType->getTypes()['Closure'])
|
|
|
|
&& $closure_atomic_type instanceof Type\Atomic\Fn
|
|
|
|
) {
|
2018-03-27 04:13:10 +02:00
|
|
|
$closure_return_type = $closure_atomic_type->return_type ?: Type::getMixed();
|
2018-01-17 22:07:46 +01:00
|
|
|
|
|
|
|
if ($closure_return_type->isVoid()) {
|
|
|
|
IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
|
|
|
'No return type could be found in the closure passed to array_filter',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
);
|
|
|
|
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (count($function_call_arg->value->stmts) === 1 && count($function_call_arg->value->params)) {
|
2018-02-07 00:44:53 +01:00
|
|
|
$first_param = $function_call_arg->value->params[0];
|
|
|
|
$stmt = $function_call_arg->value->stmts[0];
|
|
|
|
|
|
|
|
if ($first_param->variadic === false
|
2018-04-17 18:16:25 +02:00
|
|
|
&& is_string($first_param->var->name)
|
2018-02-07 00:44:53 +01:00
|
|
|
&& $stmt instanceof PhpParser\Node\Stmt\Return_
|
|
|
|
&& $stmt->expr
|
|
|
|
) {
|
|
|
|
$assertions = AssertionFinder::getAssertions($stmt->expr, null, $statements_checker);
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (isset($assertions['$' . $first_param->var->name])) {
|
2018-02-07 00:44:53 +01:00
|
|
|
$changed_var_ids = [];
|
|
|
|
|
|
|
|
$reconciled_types = Reconciler::reconcileKeyedTypes(
|
2018-05-13 01:38:43 +02:00
|
|
|
['$inner_type' => [[$assertions['$' . $first_param->var->name]]]],
|
2018-02-07 00:44:53 +01:00
|
|
|
['$inner_type' => $inner_type],
|
|
|
|
$changed_var_ids,
|
|
|
|
['$inner_type' => true],
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
2018-01-17 22:07:46 +01:00
|
|
|
|
2018-02-07 00:44:53 +01:00
|
|
|
if (isset($reconciled_types['$inner_type'])) {
|
|
|
|
$inner_type = $reconciled_types['$inner_type'];
|
|
|
|
}
|
2018-01-17 22:07:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
$key_type,
|
|
|
|
$inner_type,
|
|
|
|
]),
|
|
|
|
]);
|
2018-01-17 22:07:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
$key_type,
|
|
|
|
$inner_type,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|