1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 19:36:59 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php

970 lines
39 KiB
PHP
Raw Normal View History

2016-11-01 19:14:35 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer\Statements\Expression;
2016-11-01 19:14:35 +01:00
use PhpParser;
2020-11-03 22:15:44 +01:00
use Psalm\Internal\Algebra\FormulaGenerator;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
2020-07-22 01:40:35 +02:00
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Type\TemplateBound;
use Psalm\Internal\Type\TemplateResult;
use Psalm\CodeLocation;
2016-11-02 07:29:00 +01:00
use Psalm\Context;
2016-11-01 19:14:35 +01:00
use Psalm\Issue\InvalidArgument;
use Psalm\Issue\InvalidScalarArgument;
use Psalm\Issue\MixedArgumentTypeCoercion;
use Psalm\Issue\ArgumentTypeCoercion;
2016-11-01 19:14:35 +01:00
use Psalm\Issue\UndefinedFunction;
use Psalm\IssueBuffer;
2017-02-10 02:35:17 +01:00
use Psalm\Storage\ClassLikeStorage;
2016-11-01 19:14:35 +01:00
use Psalm\Type;
use Psalm\Type\Atomic\TNamedObject;
use function strtolower;
use function strpos;
use function count;
use function in_array;
use function is_string;
use function preg_match;
use function preg_replace;
use function str_replace;
use function is_int;
use function substr;
use function array_merge;
use function array_map;
2016-11-01 19:14:35 +01:00
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class CallAnalyzer
2016-11-01 19:14:35 +01:00
{
2017-02-10 02:35:17 +01:00
public static function collectSpecialInformation(
2018-11-06 03:57:36 +01:00
FunctionLikeAnalyzer $source,
string $method_name,
2017-02-10 02:35:17 +01:00
Context $context
): void {
2017-02-10 02:35:17 +01:00
$fq_class_name = (string)$source->getFQCLN();
2016-11-02 07:29:00 +01:00
2018-11-11 18:01:14 +01:00
$project_analyzer = $source->getFileAnalyzer()->project_analyzer;
2018-11-06 03:57:36 +01:00
$codebase = $source->getCodebase();
2017-02-10 02:35:17 +01:00
if ($context->collect_mutations &&
$context->self &&
(
$context->self === $fq_class_name ||
2018-02-01 06:50:01 +01:00
$codebase->classExtends(
2017-02-10 02:35:17 +01:00
$context->self,
$fq_class_name
)
)
) {
$method_id = new \Psalm\Internal\MethodIdentifier(
$fq_class_name,
strtolower($method_name)
);
2017-02-10 02:35:17 +01:00
if ((string) $method_id !== $source->getId()) {
2018-01-24 19:38:53 +01:00
if ($context->collect_initializations) {
if (isset($context->initialized_methods[(string) $method_id])) {
2018-01-24 19:38:53 +01:00
return;
}
if ($context->initialized_methods === null) {
$context->initialized_methods = [];
}
$context->initialized_methods[(string) $method_id] = true;
2018-01-24 19:38:53 +01:00
}
$project_analyzer->getMethodMutations(
$method_id,
$context,
$source->getRootFilePath(),
$source->getRootFileName()
);
}
2017-02-10 02:35:17 +01:00
} elseif ($context->collect_initializations &&
$context->self &&
(
$context->self === $fq_class_name
|| $codebase->classlikes->classExtends(
2017-02-10 02:35:17 +01:00
$context->self,
$fq_class_name
)
) &&
$source->getMethodName() !== $method_name
2017-02-10 02:35:17 +01:00
) {
$method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, strtolower($method_name));
2017-02-10 02:35:17 +01:00
2019-01-09 14:35:53 +01:00
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
if (isset($context->vars_in_scope['$this'])) {
foreach ($context->vars_in_scope['$this']->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TNamedObject) {
if ($fq_class_name === $atomic_type->value) {
$alt_declaring_method_id = $declaring_method_id;
} else {
2019-01-09 14:35:53 +01:00
$fq_class_name = $atomic_type->value;
$method_id = new \Psalm\Internal\MethodIdentifier(
$fq_class_name,
strtolower($method_name)
);
2019-01-09 14:35:53 +01:00
$alt_declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
}
2019-01-09 14:35:53 +01:00
if ($alt_declaring_method_id) {
$declaring_method_id = $alt_declaring_method_id;
break;
}
2019-01-09 14:35:53 +01:00
if (!$atomic_type->extra_types) {
continue;
}
2019-01-09 14:35:53 +01:00
foreach ($atomic_type->extra_types as $intersection_type) {
if ($intersection_type instanceof TNamedObject) {
$fq_class_name = $intersection_type->value;
$method_id = new \Psalm\Internal\MethodIdentifier(
$fq_class_name,
strtolower($method_name)
);
2019-01-09 14:35:53 +01:00
$alt_declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
2019-01-09 14:35:53 +01:00
if ($alt_declaring_method_id) {
$declaring_method_id = $alt_declaring_method_id;
break 2;
2019-01-09 14:35:53 +01:00
}
}
}
}
}
}
2019-01-09 14:35:53 +01:00
if (!$declaring_method_id) {
// can happen for __call
return;
2019-01-09 14:35:53 +01:00
}
2017-02-10 02:35:17 +01:00
if (isset($context->initialized_methods[(string) $declaring_method_id])) {
return;
}
if ($context->initialized_methods === null) {
$context->initialized_methods = [];
}
$context->initialized_methods[(string) $declaring_method_id] = true;
2018-02-04 00:52:35 +01:00
$method_storage = $codebase->methods->getStorage($declaring_method_id);
2017-02-10 02:35:17 +01:00
2018-11-11 18:01:14 +01:00
$class_analyzer = $source->getSource();
2017-02-10 02:35:17 +01:00
$is_final = $method_storage->final;
if ($method_name !== $declaring_method_id->method_name) {
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
if ($appearing_method_id) {
$appearing_class_storage = $codebase->classlike_storage_provider->get(
$appearing_method_id->fq_class_name
);
if (isset($appearing_class_storage->trait_final_map[strtolower($method_name)])) {
$is_final = true;
}
}
}
if ($class_analyzer instanceof ClassLikeAnalyzer
&& !$method_storage->is_static
&& ($context->collect_nonprivate_initializations
|| $method_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
|| $is_final)
) {
2017-02-10 02:35:17 +01:00
$local_vars_in_scope = [];
$local_vars_possibly_in_scope = [];
foreach ($context->vars_in_scope as $var => $_) {
2017-02-10 02:35:17 +01:00
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
$local_vars_in_scope[$var] = $context->vars_in_scope[$var];
}
}
foreach ($context->vars_possibly_in_scope as $var => $_) {
2017-02-10 02:35:17 +01:00
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
$local_vars_possibly_in_scope[$var] = $context->vars_possibly_in_scope[$var];
}
}
$old_calling_method_id = $context->calling_method_id;
if ($fq_class_name === $source->getFQCLN()) {
2020-07-26 21:21:05 +02:00
$class_analyzer->getMethodMutations($declaring_method_id->method_name, $context);
} else {
$declaring_fq_class_name = $declaring_method_id->fq_class_name;
$old_self = $context->self;
$context->self = $declaring_fq_class_name;
$project_analyzer->getMethodMutations(
$declaring_method_id,
$context,
$source->getRootFilePath(),
$source->getRootFileName()
);
$context->self = $old_self;
}
2017-02-10 02:35:17 +01:00
$context->calling_method_id = $old_calling_method_id;
2017-02-10 02:35:17 +01:00
foreach ($local_vars_in_scope as $var => $type) {
$context->vars_in_scope[$var] = $type;
}
2018-01-28 18:43:19 +01:00
foreach ($local_vars_possibly_in_scope as $var => $_) {
2017-12-03 00:28:18 +01:00
$context->vars_possibly_in_scope[$var] = true;
2017-02-10 02:35:17 +01:00
}
}
}
2016-11-01 19:14:35 +01:00
}
2017-02-10 02:35:17 +01:00
/**
2020-10-28 17:45:26 +01:00
* @param list<PhpParser\Node\Arg> $args
2017-02-10 02:35:17 +01:00
*/
public static function checkMethodArgs(
?\Psalm\Internal\MethodIdentifier $method_id,
2017-02-10 02:35:17 +01:00
array $args,
?TemplateResult $class_template_result,
2017-02-10 02:35:17 +01:00
Context $context,
CodeLocation $code_location,
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer
2020-05-18 21:13:27 +02:00
) : bool {
$codebase = $statements_analyzer->getCodebase();
if (!$method_id) {
return Call\ArgumentsAnalyzer::analyze(
$statements_analyzer,
$args,
null,
null,
true,
$context,
$class_template_result
) !== false;
2017-02-10 02:35:17 +01:00
}
$method_params = $codebase->methods->getMethodParams($method_id, $statements_analyzer, $args, $context);
2017-02-10 02:35:17 +01:00
$fq_class_name = $method_id->fq_class_name;
$method_name = $method_id->method_name;
2017-02-10 02:35:17 +01:00
$fq_class_name = strtolower($codebase->classlikes->getUnAliasedName($fq_class_name));
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
2017-11-02 20:07:39 +01:00
$method_storage = null;
if (isset($class_storage->declaring_method_ids[$method_name])) {
$declaring_method_id = $class_storage->declaring_method_ids[$method_name];
2017-11-02 20:07:39 +01:00
$declaring_fq_class_name = $declaring_method_id->fq_class_name;
$declaring_method_name = $declaring_method_id->method_name;
2017-11-02 20:07:39 +01:00
if ($declaring_fq_class_name !== $fq_class_name) {
$declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_fq_class_name);
2017-11-02 20:07:39 +01:00
} else {
$declaring_class_storage = $class_storage;
}
if (!isset($declaring_class_storage->methods[$declaring_method_name])) {
2017-11-02 20:07:39 +01:00
throw new \UnexpectedValueException('Storage should not be empty here');
}
$method_storage = $declaring_class_storage->methods[$declaring_method_name];
if ($declaring_class_storage->user_defined
&& !$method_storage->has_docblock_param_types
&& isset($declaring_class_storage->documenting_method_ids[$method_name])
) {
$documenting_method_id = $declaring_class_storage->documenting_method_ids[$method_name];
$documenting_method_storage = $codebase->methods->getStorage($documenting_method_id);
if ($documenting_method_storage->template_types) {
$method_storage = $documenting_method_storage;
}
}
if (!$context->isSuppressingExceptions($statements_analyzer)) {
$context->mergeFunctionExceptions($method_storage, $code_location);
}
2017-11-02 20:07:39 +01:00
}
2017-02-10 02:35:17 +01:00
if (Call\ArgumentsAnalyzer::analyze(
$statements_analyzer,
$args,
$method_params,
(string) $method_id,
$method_storage ? $method_storage->allow_named_arg_calls : true,
$context,
$class_template_result
) === false) {
return false;
}
2020-05-19 04:57:00 +02:00
if (Call\ArgumentsAnalyzer::checkArgumentsMatch(
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$args,
$method_id,
$method_params,
$method_storage,
$class_storage,
$class_template_result,
$code_location,
$context
) === false) {
return false;
2016-11-01 19:14:35 +01:00
}
if ($class_template_result) {
self::checkTemplateResult(
$statements_analyzer,
$class_template_result,
$code_location,
strtolower((string) $method_id)
);
}
2020-05-18 21:13:27 +02:00
return true;
2016-11-01 19:14:35 +01:00
}
/**
* @return array<string, array<string, Type\Union>>
* @param array<string, non-empty-array<string, Type\Union>> $existing_template_types
2020-05-19 04:57:00 +02:00
*/
public static function getTemplateTypesForCall(
\Psalm\Codebase $codebase,
?ClassLikeStorage $declaring_class_storage,
?string $appearing_class_name,
?ClassLikeStorage $calling_class_storage,
2020-05-19 04:57:00 +02:00
array $existing_template_types = []
) : array {
$template_types = $existing_template_types;
2020-05-19 04:57:00 +02:00
if ($declaring_class_storage) {
if ($calling_class_storage
&& $declaring_class_storage !== $calling_class_storage
&& $calling_class_storage->template_type_extends
) {
foreach ($calling_class_storage->template_type_extends as $class_name => $type_map) {
foreach ($type_map as $template_name => $type) {
if (is_string($template_name) && $class_name === $declaring_class_storage->name) {
$output_type = null;
2020-05-19 04:57:00 +02:00
foreach ($type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$output_type_candidate = self::getGenericParamForOffset(
$atomic_type->defining_class,
$atomic_type->param_name,
$calling_class_storage->template_type_extends,
$template_types
);
} else {
2020-05-19 04:57:00 +02:00
$output_type_candidate = new Type\Union([$atomic_type]);
}
2020-05-19 04:57:00 +02:00
if (!$output_type) {
$output_type = $output_type_candidate;
} else {
$output_type = Type::combineUnionTypes(
$output_type_candidate,
$output_type
);
}
}
2019-08-06 16:33:21 +02:00
$template_types[$template_name][$declaring_class_storage->name] = $output_type;
2019-08-06 16:33:21 +02:00
}
}
}
2020-05-19 04:57:00 +02:00
} elseif ($declaring_class_storage->template_types) {
foreach ($declaring_class_storage->template_types as $template_name => $type_map) {
foreach ($type_map as $key => $type) {
$template_types[$template_name][$key] = $type;
2019-08-14 06:47:57 +02:00
}
2019-08-06 16:33:21 +02:00
}
}
}
2020-05-19 04:57:00 +02:00
foreach ($template_types as $key => $type_map) {
foreach ($type_map as $class => $type) {
$template_types[$key][$class] = \Psalm\Internal\Type\TypeExpander::expandUnion(
$codebase,
$type,
$appearing_class_name,
$calling_class_storage ? $calling_class_storage->name : null,
null,
true,
false,
$calling_class_storage ? $calling_class_storage->final : false
);
2019-05-21 18:11:17 +02:00
}
}
2020-05-19 04:57:00 +02:00
return $template_types;
2016-11-01 19:14:35 +01:00
}
/**
* @param array<string, array<int|string, Type\Union>> $template_type_extends
* @param array<string, array<string, Type\Union>> $found_generic_params
*/
public static function getGenericParamForOffset(
string $fq_class_name,
string $template_name,
array $template_type_extends,
array $found_generic_params,
bool $mapped = false
): Type\Union {
if (isset($found_generic_params[$template_name][$fq_class_name])) {
if (!$mapped && isset($template_type_extends[$fq_class_name][$template_name])) {
foreach ($template_type_extends[$fq_class_name][$template_name]->getAtomicTypes() as $t) {
if ($t instanceof Type\Atomic\TTemplateParam) {
if ($t->param_name !== $template_name) {
return $t->as;
}
}
}
}
return $found_generic_params[$template_name][$fq_class_name];
}
foreach ($template_type_extends as $type_map) {
foreach ($type_map as $extended_template_name => $extended_type) {
foreach ($extended_type->getAtomicTypes() as $extended_atomic_type) {
if (is_string($extended_template_name)
&& $extended_atomic_type instanceof Type\Atomic\TTemplateParam
&& $extended_atomic_type->param_name === $template_name
&& $extended_template_name !== $template_name
) {
return self::getGenericParamForOffset(
$fq_class_name,
$extended_template_name,
$template_type_extends,
$found_generic_params,
true
);
}
}
}
}
return Type::getMixed();
}
/**
* @param PhpParser\Node\Scalar\String_|PhpParser\Node\Expr\Array_|PhpParser\Node\Expr\BinaryOp\Concat
* $callable_arg
*
2020-10-17 18:36:44 +02:00
* @return list<non-empty-string>
2020-05-15 16:18:05 +02:00
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
*/
public static function getFunctionIdsFromCallableArg(
\Psalm\FileSource $file_source,
2020-10-12 21:46:47 +02:00
PhpParser\Node\Expr $callable_arg
): array {
if ($callable_arg instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
if ($callable_arg->left instanceof PhpParser\Node\Expr\ClassConstFetch
&& $callable_arg->left->class instanceof PhpParser\Node\Name
&& $callable_arg->left->name instanceof PhpParser\Node\Identifier
&& strtolower($callable_arg->left->name->name) === 'class'
&& !in_array(strtolower($callable_arg->left->class->parts[0]), ['self', 'static', 'parent'])
&& $callable_arg->right instanceof PhpParser\Node\Scalar\String_
&& preg_match('/^::[A-Za-z0-9]+$/', $callable_arg->right->value)
) {
2018-10-17 20:37:32 +02:00
return [
(string) $callable_arg->left->class->getAttribute('resolvedName') . $callable_arg->right->value
];
}
return [];
}
if ($callable_arg instanceof PhpParser\Node\Scalar\String_) {
2019-08-12 21:04:43 +02:00
$potential_id = preg_replace('/^\\\/', '', $callable_arg->value);
if (preg_match('/^[A-Za-z0-9_]+(\\\[A-Za-z0-9_]+)*(::[A-Za-z0-9_]+)?$/', $potential_id)) {
return [$potential_id];
}
return [];
}
if (count($callable_arg->items) !== 2) {
return [];
}
2020-02-24 22:22:50 +01:00
/** @psalm-suppress PossiblyNullPropertyFetch */
if ($callable_arg->items[0]->key || $callable_arg->items[1]->key) {
return [];
}
2018-01-14 00:33:32 +01:00
if (!isset($callable_arg->items[0]) || !isset($callable_arg->items[1])) {
throw new \UnexpectedValueException('These should never be unset');
}
$class_arg = $callable_arg->items[0]->value;
$method_name_arg = $callable_arg->items[1]->value;
if (!$method_name_arg instanceof PhpParser\Node\Scalar\String_) {
return [];
}
if ($class_arg instanceof PhpParser\Node\Scalar\String_) {
return [preg_replace('/^\\\/', '', $class_arg->value) . '::' . $method_name_arg->value];
}
if ($class_arg instanceof PhpParser\Node\Expr\ClassConstFetch
&& $class_arg->name instanceof PhpParser\Node\Identifier
&& strtolower($class_arg->name->name) === 'class'
&& $class_arg->class instanceof PhpParser\Node\Name
) {
2018-11-06 03:57:36 +01:00
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
$class_arg->class,
$file_source->getAliases()
);
return [$fq_class_name . '::' . $method_name_arg->value];
}
if (!$file_source instanceof StatementsAnalyzer
|| !($class_arg_type = $file_source->node_data->getType($class_arg))
) {
return [];
}
$method_ids = [];
foreach ($class_arg_type->getAtomicTypes() as $type_part) {
if ($type_part instanceof TNamedObject) {
$method_id = $type_part->value . '::' . $method_name_arg->value;
if ($type_part->extra_types) {
foreach ($type_part->extra_types as $extra_type) {
if ($extra_type instanceof Type\Atomic\TTemplateParam
|| $extra_type instanceof Type\Atomic\TObjectWithProperties
) {
throw new \UnexpectedValueException('Shouldnt get a generic param here');
}
$method_id .= '&' . $extra_type->value . '::' . $method_name_arg->value;
}
}
$method_ids[] = '$' . $method_id;
}
}
return $method_ids;
}
2016-11-01 19:14:35 +01:00
/**
2020-05-15 16:18:05 +02:00
* @param non-empty-string $function_id
* @param bool $can_be_in_root_scope if true, the function can be shortened to the root version
2017-05-27 02:16:18 +02:00
*
2016-11-01 19:14:35 +01:00
*/
2020-05-19 04:57:00 +02:00
public static function checkFunctionExists(
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer,
2020-10-12 21:46:47 +02:00
string &$function_id,
CodeLocation $code_location,
bool $can_be_in_root_scope
): bool {
2016-11-01 19:14:35 +01:00
$cased_function_id = $function_id;
$function_id = strtolower($function_id);
2018-11-11 18:01:14 +01:00
$codebase = $statements_analyzer->getCodebase();
2018-11-11 18:01:14 +01:00
if (!$codebase->functions->functionExists($statements_analyzer, $function_id)) {
2020-05-15 16:18:05 +02:00
/** @var non-empty-lowercase-string */
$root_function_id = preg_replace('/.*\\\/', '', $function_id);
if ($can_be_in_root_scope
&& $function_id !== $root_function_id
2018-11-11 18:01:14 +01:00
&& $codebase->functions->functionExists($statements_analyzer, $root_function_id)
) {
$function_id = $root_function_id;
} else {
if (IssueBuffer::accepts(
new UndefinedFunction(
'Function ' . $cased_function_id . ' does not exist',
$code_location,
$function_id
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
return false;
2016-11-01 19:14:35 +01:00
}
}
return true;
}
/**
* @param PhpParser\Node\Identifier|PhpParser\Node\Name $expr
* @param \Psalm\Storage\Assertion[] $assertions
* @param string $thisName
2020-10-28 17:45:26 +01:00
* @param list<PhpParser\Node\Arg> $args
* @param array<string, array<string, TemplateBound>> $inferred_upper_bouunds,
*
*/
2020-11-09 06:58:45 +01:00
public static function applyAssertionsToContext(
2020-10-12 21:46:47 +02:00
PhpParser\NodeAbstract $expr,
?string $thisName,
array $assertions,
array $args,
array $inferred_upper_bouunds,
Context $context,
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer
): void {
$type_assertions = [];
$asserted_keys = [];
foreach ($assertions as $assertion) {
$assertion_var_id = null;
2019-01-05 20:50:11 +01:00
$arg_value = null;
if (is_int($assertion->var_id)) {
if (!isset($args[$assertion->var_id])) {
continue;
}
$arg_value = $args[$assertion->var_id]->value;
2020-05-18 21:13:27 +02:00
$arg_var_id = ExpressionIdentifier::getArrayVarId($arg_value, null, $statements_analyzer);
if ($arg_var_id) {
$assertion_var_id = $arg_var_id;
}
} elseif ($assertion->var_id === '$this' && $thisName !== null) {
$assertion_var_id = $thisName;
} elseif (strpos($assertion->var_id, '$this->') === 0 && $thisName !== null) {
$assertion_var_id = $thisName . str_replace('$this->', '->', $assertion->var_id);
} elseif (isset($context->vars_in_scope[$assertion->var_id])) {
$assertion_var_id = $assertion->var_id;
}
if ($assertion_var_id) {
$rule = $assertion->rule[0][0];
$prefix = '';
if ($rule[0] === '!') {
$prefix .= '!';
$rule = substr($rule, 1);
}
if ($rule[0] === '=') {
$prefix .= '=';
$rule = substr($rule, 1);
}
if ($rule[0] === '~') {
$prefix .= '~';
$rule = substr($rule, 1);
}
if (isset($inferred_upper_bouunds[$rule])) {
foreach ($inferred_upper_bouunds[$rule] as $template_map) {
if ($template_map->type->hasMixed()) {
continue 2;
}
$replacement_atomic_types = $template_map->type->getAtomicTypes();
if (count($replacement_atomic_types) > 1) {
continue 2;
}
$ored_type_assertions = [];
foreach ($replacement_atomic_types as $replacement_atomic_type) {
if ($replacement_atomic_type instanceof Type\Atomic\TMixed) {
continue 3;
}
if ($replacement_atomic_type instanceof Type\Atomic\TArray
|| $replacement_atomic_type instanceof Type\Atomic\TKeyedArray
) {
$ored_type_assertions[] = $prefix . 'array';
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNamedObject) {
$ored_type_assertions[] = $prefix . $replacement_atomic_type->value;
} elseif ($replacement_atomic_type instanceof Type\Atomic\Scalar) {
$ored_type_assertions[] = $prefix . $replacement_atomic_type->getId();
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNull) {
$ored_type_assertions[] = $prefix . 'null';
} elseif ($replacement_atomic_type instanceof Type\Atomic\TTemplateParam) {
$ored_type_assertions[] = $prefix . $replacement_atomic_type->param_name;
}
}
if ($ored_type_assertions) {
$type_assertions[$assertion_var_id] = [$ored_type_assertions];
}
2018-11-16 17:50:07 +01:00
}
} else {
if (isset($type_assertions[$assertion_var_id])) {
$type_assertions[$assertion_var_id] = array_merge(
$type_assertions[$assertion_var_id],
$assertion->rule
);
} else {
$type_assertions[$assertion_var_id] = $assertion->rule;
}
}
} elseif ($arg_value && ($assertion->rule === [['!falsy']] || $assertion->rule === [['true']])) {
if ($assertion->rule === [['true']]) {
$conditional = new PhpParser\Node\Expr\BinaryOp\Identical(
$arg_value,
new PhpParser\Node\Expr\ConstFetch(new PhpParser\Node\Name('true'))
);
2020-11-03 22:15:44 +01:00
$assert_clauses = FormulaGenerator::getFormula(
2020-08-26 21:35:29 +02:00
\mt_rand(0, 1000000),
\mt_rand(0, 1000000),
$conditional,
$context->self,
$statements_analyzer,
$statements_analyzer->getCodebase()
);
} else {
2020-11-03 22:15:44 +01:00
$assert_clauses = FormulaGenerator::getFormula(
2020-08-26 21:35:29 +02:00
\spl_object_id($arg_value),
\spl_object_id($arg_value),
$arg_value,
$context->self,
$statements_analyzer,
$statements_analyzer->getCodebase()
);
}
2019-01-05 20:50:11 +01:00
2020-11-03 22:15:44 +01:00
$simplified_clauses = \Psalm\Internal\Algebra::simplifyCNF(
2019-01-05 20:50:11 +01:00
array_merge($context->clauses, $assert_clauses)
);
2020-11-03 22:15:44 +01:00
$assert_type_assertions = \Psalm\Internal\Algebra::getTruthsFromFormula(
2019-01-05 20:50:11 +01:00
$simplified_clauses
);
$type_assertions = array_merge($type_assertions, $assert_type_assertions);
} elseif ($arg_value && $assertion->rule === [['falsy']]) {
2020-11-03 22:15:44 +01:00
$assert_clauses = \Psalm\Internal\Algebra::negateFormula(
FormulaGenerator::getFormula(
2020-08-26 21:35:29 +02:00
\spl_object_id($arg_value),
\spl_object_id($arg_value),
$arg_value,
$context->self,
$statements_analyzer,
$statements_analyzer->getCodebase()
)
);
2020-11-03 22:15:44 +01:00
$simplified_clauses = \Psalm\Internal\Algebra::simplifyCNF(
array_merge($context->clauses, $assert_clauses)
);
2020-11-03 22:15:44 +01:00
$assert_type_assertions = \Psalm\Internal\Algebra::getTruthsFromFormula(
$simplified_clauses
);
2019-01-05 20:50:11 +01:00
$type_assertions = array_merge($type_assertions, $assert_type_assertions);
}
}
$changed_var_ids = [];
foreach ($type_assertions as $var_id => $_) {
$asserted_keys[$var_id] = true;
}
if ($type_assertions) {
$template_type_map = array_map(
function ($type_map) {
return array_map(
function ($bound) {
return $bound->type;
},
$type_map
);
},
$inferred_upper_bouunds
);
foreach (($statements_analyzer->getTemplateTypeMap() ?: []) as $template_name => $map) {
foreach ($map as $ref => $type) {
$template_type_map[$template_name][$ref] = new Type\Union([
new Type\Atomic\TTemplateParam(
$template_name,
$type,
$ref
)
]);
}
}
// while in an and, we allow scope to boil over to support
// statements of the form if ($x && $x->foo())
$op_vars_in_scope = \Psalm\Type\Reconciler::reconcileKeyedTypes(
$type_assertions,
$type_assertions,
$context->vars_in_scope,
$changed_var_ids,
$asserted_keys,
$statements_analyzer,
$template_type_map,
$context->inside_loop,
new CodeLocation($statements_analyzer->getSource(), $expr)
);
foreach ($changed_var_ids as $var_id => $_) {
if (isset($op_vars_in_scope[$var_id])) {
$first_appearance = $statements_analyzer->getFirstAppearance($var_id);
$codebase = $statements_analyzer->getCodebase();
2020-05-11 03:09:48 +02:00
if ($first_appearance
&& isset($context->vars_in_scope[$var_id])
&& $context->vars_in_scope[$var_id]->hasMixed()
) {
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
&& (!(($parent_source = $statements_analyzer->getSource())
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
) {
$codebase->analyzer->decrementMixedCount($statements_analyzer->getFilePath());
}
IssueBuffer::remove(
$statements_analyzer->getFilePath(),
'MixedAssignment',
$first_appearance->raw_file_start
);
}
$op_vars_in_scope[$var_id]->from_docblock = true;
foreach ($op_vars_in_scope[$var_id]->getAtomicTypes() as $changed_atomic_type) {
$changed_atomic_type->from_docblock = true;
if ($changed_atomic_type instanceof Type\Atomic\TNamedObject
&& $changed_atomic_type->extra_types
) {
foreach ($changed_atomic_type->extra_types as $extra_type) {
$extra_type->from_docblock = true;
}
}
}
}
}
$context->vars_in_scope = $op_vars_in_scope;
}
}
public static function checkTemplateResult(
StatementsAnalyzer $statements_analyzer,
TemplateResult $template_result,
CodeLocation $code_location,
?string $function_id
) : void {
if ($template_result->upper_bounds && $template_result->lower_bounds) {
foreach ($template_result->lower_bounds as $template_name => $defining_map) {
foreach ($defining_map as $defining_id => $lower_bound) {
if (isset($template_result->upper_bounds[$template_name][$defining_id])) {
$upper_bound = $template_result->upper_bounds[$template_name][$defining_id];
2020-07-22 01:40:35 +02:00
$union_comparison_result = new \Psalm\Internal\Type\Comparator\TypeComparisonResult();
if (count($template_result->lower_bounds_unintersectable_types) > 1) {
[$upper_bound->type, $lower_bound->type]
= $template_result->lower_bounds_unintersectable_types;
}
2020-07-22 01:40:35 +02:00
if (!UnionTypeComparator::isContainedBy(
$statements_analyzer->getCodebase(),
$upper_bound->type,
$lower_bound->type,
false,
false,
$union_comparison_result
)) {
if ($union_comparison_result->type_coerced) {
if ($union_comparison_result->type_coerced_from_mixed) {
if (IssueBuffer::accepts(
new MixedArgumentTypeCoercion(
'Type ' . $upper_bound->type->getId() . ' should be a subtype of '
. $lower_bound->type->getId(),
$code_location,
$function_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// continue
}
} else {
if (IssueBuffer::accepts(
new ArgumentTypeCoercion(
'Type ' . $upper_bound->type->getId() . ' should be a subtype of '
. $lower_bound->type->getId(),
$code_location,
$function_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// continue
}
}
} elseif ($union_comparison_result->scalar_type_match_found) {
if (IssueBuffer::accepts(
new InvalidScalarArgument(
'Type ' . $upper_bound->type->getId() . ' should be a subtype of '
. $lower_bound->type->getId(),
$code_location,
$function_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// continue
}
} else {
if (IssueBuffer::accepts(
new InvalidArgument(
'Type ' . $upper_bound->type->getId() . ' should be a subtype of '
. $lower_bound->type->getId(),
$code_location,
$function_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// continue
}
}
}
} else {
$template_result->upper_bounds[$template_name][$defining_id] = new TemplateBound(
clone $lower_bound->type
);
}
}
}
}
}
2016-11-01 19:14:35 +01:00
}