2018-01-29 00:29:38 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\Call;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Algebra;
|
2020-11-03 22:15:44 +01:00
|
|
|
use Psalm\Internal\Algebra\FormulaGenerator;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Codebase\InternalCallMapHandler;
|
2020-09-30 18:28:13 +02:00
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Type\Comparator\CallableTypeComparator;
|
|
|
|
use Psalm\Internal\Type\TemplateResult;
|
2019-06-15 18:09:15 +02:00
|
|
|
use Psalm\Issue\DeprecatedFunction;
|
2019-07-18 07:31:48 +02:00
|
|
|
use Psalm\Issue\ImpureFunctionCall;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Issue\InvalidFunctionCall;
|
|
|
|
use Psalm\Issue\MixedFunctionCall;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\NullFunctionCall;
|
|
|
|
use Psalm\Issue\PossiblyInvalidFunctionCall;
|
|
|
|
use Psalm\Issue\PossiblyNullFunctionCall;
|
2019-08-13 19:15:23 +02:00
|
|
|
use Psalm\Issue\UnusedFunctionCall;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2021-02-15 22:18:41 +01:00
|
|
|
use Psalm\Node\Expr\VirtualFuncCall;
|
|
|
|
use Psalm\Node\Expr\VirtualMethodCall;
|
|
|
|
use Psalm\Node\Name\VirtualFullyQualified;
|
|
|
|
use Psalm\Node\VirtualArg;
|
|
|
|
use Psalm\Node\VirtualIdentifier;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
|
2021-01-06 15:05:53 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\AfterEveryFunctionCallAnalysisEvent;
|
2019-01-23 05:42:54 +01:00
|
|
|
use Psalm\Storage\Assertion;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Type;
|
2020-10-12 19:46:43 +02:00
|
|
|
use Psalm\Type\Atomic\TCallable;
|
2019-01-04 14:04:19 +01:00
|
|
|
use Psalm\Type\Atomic\TCallableObject;
|
|
|
|
use Psalm\Type\Atomic\TCallableString;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
2018-02-12 02:56:34 +01:00
|
|
|
use Psalm\Type\Atomic\TString;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Type\Reconciler;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
|
|
|
use function array_map;
|
|
|
|
use function array_merge;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function count;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function explode;
|
|
|
|
use function implode;
|
2019-09-29 22:04:32 +02:00
|
|
|
use function in_array;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function reset;
|
|
|
|
use function strpos;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function strtolower;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-07 06:13:56 +02:00
|
|
|
class FunctionCallAnalyzer extends CallAnalyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
{
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
Context $context
|
2020-05-18 21:13:27 +02:00
|
|
|
) : bool {
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name = $stmt->name;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$code_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
2018-01-29 00:29:38 +01:00
|
|
|
$config = $codebase->config;
|
|
|
|
|
2020-01-24 19:34:50 +01:00
|
|
|
$real_stmt = $stmt;
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Name
|
2020-01-24 19:34:50 +01:00
|
|
|
&& isset($stmt->args[0])
|
|
|
|
&& !$stmt->args[0]->unpack
|
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
$original_function_id = implode('\\', $function_name->parts);
|
2020-01-24 19:34:50 +01:00
|
|
|
|
|
|
|
if ($original_function_id === 'call_user_func') {
|
2020-01-24 20:08:01 +01:00
|
|
|
$other_args = \array_slice($stmt->args, 1);
|
2020-01-24 19:34:50 +01:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name = $stmt->args[0]->value;
|
|
|
|
|
2021-02-15 22:18:41 +01:00
|
|
|
$stmt = new VirtualFuncCall(
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name,
|
2020-01-24 19:34:50 +01:00
|
|
|
$other_args,
|
|
|
|
$stmt->getAttributes()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($original_function_id === 'call_user_func_array' && isset($stmt->args[1])) {
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name = $stmt->args[0]->value;
|
|
|
|
|
2021-02-15 22:18:41 +01:00
|
|
|
$stmt = new VirtualFuncCall(
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name,
|
2021-02-15 22:18:41 +01:00
|
|
|
[new VirtualArg($stmt->args[1]->value, false, true)],
|
2020-01-24 19:34:50 +01:00
|
|
|
$stmt->getAttributes()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Expr) {
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info = self::getAnalyzeNamedExpression(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$real_stmt,
|
|
|
|
$function_name,
|
|
|
|
$context
|
|
|
|
);
|
2019-08-23 05:26:04 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->function_exists === false) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->new_function_name) {
|
|
|
|
$function_name = $function_call_info->new_function_name;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info = self::handleNamedFunction(
|
2019-01-05 20:50:11 +01:00
|
|
|
$statements_analyzer,
|
2020-11-27 22:24:59 +01:00
|
|
|
$stmt,
|
|
|
|
$function_name,
|
|
|
|
$context,
|
|
|
|
$code_location
|
2019-01-05 20:50:11 +01:00
|
|
|
);
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if (!$function_call_info->function_exists) {
|
|
|
|
return true;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 04:38:30 +01:00
|
|
|
$set_inside_conditional = false;
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Name
|
|
|
|
&& $function_name->parts === ['assert']
|
2019-03-11 04:38:30 +01:00
|
|
|
&& !$context->inside_conditional
|
|
|
|
) {
|
|
|
|
$context->inside_conditional = true;
|
|
|
|
$set_inside_conditional = true;
|
|
|
|
}
|
|
|
|
|
2020-12-02 06:52:35 +01:00
|
|
|
ArgumentsAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->args,
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_params,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$function_call_info->allow_named_args,
|
2018-01-29 00:29:38 +01:00
|
|
|
$context
|
2020-12-02 06:52:35 +01:00
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-03-11 04:38:30 +01:00
|
|
|
if ($set_inside_conditional) {
|
|
|
|
$context->inside_conditional = false;
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:46:43 +02:00
|
|
|
$function_callable = null;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
|
|
|
|
if (!$function_call_info->is_stubbed && $function_call_info->in_call_map) {
|
|
|
|
$function_callable = \Psalm\Internal\Codebase\InternalCallMapHandler::getCallableFromCallMapById(
|
|
|
|
$codebase,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$stmt->args,
|
|
|
|
$statements_analyzer->node_data
|
|
|
|
);
|
2019-06-15 22:10:48 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_params = $function_callable->params;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2020-11-27 22:24:59 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$template_result = new TemplateResult([], []);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
// do this here to allow closure param checks
|
2020-12-02 06:52:35 +01:00
|
|
|
if ($function_call_info->function_params !== null) {
|
|
|
|
ArgumentsAnalyzer::checkArgumentsMatch(
|
2020-04-07 06:13:56 +02:00
|
|
|
$statements_analyzer,
|
2020-11-27 22:24:59 +01:00
|
|
|
$stmt->args,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$function_call_info->function_params,
|
|
|
|
$function_call_info->function_storage,
|
|
|
|
null,
|
2020-04-07 06:13:56 +02:00
|
|
|
$template_result,
|
|
|
|
$code_location,
|
2020-11-27 22:24:59 +01:00
|
|
|
$context
|
2020-12-02 06:52:35 +01:00
|
|
|
);
|
2020-11-27 22:24:59 +01:00
|
|
|
}
|
2020-04-07 06:13:56 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
CallAnalyzer::checkTemplateResult(
|
|
|
|
$statements_analyzer,
|
|
|
|
$template_result,
|
|
|
|
$code_location,
|
|
|
|
$function_call_info->function_id
|
|
|
|
);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
|
|
|
|
$stmt_type = FunctionCallReturnTypeFetcher::fetch(
|
|
|
|
$statements_analyzer,
|
|
|
|
$codebase,
|
|
|
|
$stmt,
|
|
|
|
$function_name,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$function_call_info->in_call_map,
|
|
|
|
$function_call_info->is_stubbed,
|
|
|
|
$function_call_info->function_storage,
|
|
|
|
$function_callable,
|
|
|
|
$template_result,
|
|
|
|
$context
|
|
|
|
);
|
2020-02-13 13:04:02 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$statements_analyzer->node_data->setType($real_stmt, $stmt_type);
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
$event = new AfterEveryFunctionCallAnalysisEvent(
|
|
|
|
$stmt,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$context,
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
|
|
|
|
$config->eventDispatcher->dispatchAfterEveryFunctionCallAnalysis($event);
|
2020-11-27 22:24:59 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
foreach ($function_call_info->defined_constants as $const_name => $const_type) {
|
|
|
|
$context->constants[$const_name] = clone $const_type;
|
|
|
|
$context->vars_in_scope[$const_name] = clone $const_type;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
foreach ($function_call_info->global_variables as $var_id => $_) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2018-05-31 04:09:46 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Name
|
|
|
|
&& $function_name->parts === ['assert']
|
|
|
|
&& isset($stmt->args[0])
|
|
|
|
) {
|
|
|
|
self::processAssertFunctionEffects(
|
|
|
|
$statements_analyzer,
|
|
|
|
$codebase,
|
|
|
|
$stmt,
|
|
|
|
$stmt->args[0],
|
|
|
|
$context
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types
|
2019-07-01 15:55:39 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
2020-01-24 19:34:50 +01:00
|
|
|
&& ($stmt_type = $statements_analyzer->node_data->getType($real_stmt))
|
2018-10-26 22:17:15 +02:00
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt,
|
2020-02-23 23:03:27 +01:00
|
|
|
$stmt_type->getId()
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
self::checkFunctionCallPurity(
|
|
|
|
$statements_analyzer,
|
|
|
|
$codebase,
|
|
|
|
$stmt,
|
|
|
|
$function_name,
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info,
|
2020-03-12 16:05:42 +01:00
|
|
|
$context
|
|
|
|
);
|
2019-08-13 19:15:23 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->function_storage) {
|
2021-07-10 20:08:09 +02:00
|
|
|
$inferred_lower_bounds = $template_result->lower_bounds;
|
2019-08-31 20:54:59 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->function_storage->assertions && $function_name instanceof PhpParser\Node\Name) {
|
2018-05-28 21:07:42 +02:00
|
|
|
self::applyAssertionsToContext(
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name,
|
2019-09-06 03:00:02 +02:00
|
|
|
null,
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_storage->assertions,
|
2018-05-28 21:07:42 +02:00
|
|
|
$stmt->args,
|
2021-07-10 20:08:09 +02:00
|
|
|
$inferred_lower_bounds,
|
2018-05-28 21:07:42 +02:00
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->function_storage->if_true_assertions) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setIfTrueAssertions(
|
|
|
|
$stmt,
|
|
|
|
array_map(
|
2021-07-11 18:03:21 +02:00
|
|
|
function (Assertion $assertion) use ($inferred_lower_bounds, $codebase) : Assertion {
|
|
|
|
return $assertion->getUntemplatedCopy($inferred_lower_bounds ?: [], null, $codebase);
|
2019-11-25 17:44:54 +01:00
|
|
|
},
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_storage->if_true_assertions
|
2019-11-25 17:44:54 +01:00
|
|
|
)
|
2019-01-23 05:42:54 +01:00
|
|
|
);
|
2018-07-11 17:22:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->function_storage->if_false_assertions) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setIfFalseAssertions(
|
|
|
|
$stmt,
|
|
|
|
array_map(
|
2021-07-11 18:03:21 +02:00
|
|
|
function (Assertion $assertion) use ($inferred_lower_bounds, $codebase) : Assertion {
|
|
|
|
return $assertion->getUntemplatedCopy($inferred_lower_bounds ?: [], null, $codebase);
|
2019-11-25 17:44:54 +01:00
|
|
|
},
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_storage->if_false_assertions
|
2019-11-25 17:44:54 +01:00
|
|
|
)
|
2019-01-23 05:42:54 +01:00
|
|
|
);
|
2018-05-28 21:07:42 +02:00
|
|
|
}
|
2019-06-15 18:09:15 +02:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->function_storage->deprecated && $function_call_info->function_id) {
|
2019-06-15 18:09:15 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedFunction(
|
2020-11-27 22:24:59 +01:00
|
|
|
'The function ' . $function_call_info->function_id . ' has been marked as deprecated',
|
2019-06-15 18:09:15 +02:00
|
|
|
$code_location,
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_id
|
2019-06-15 18:09:15 +02:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_call_info->byref_uses) {
|
|
|
|
foreach ($function_call_info->byref_uses as $byref_use_var => $_) {
|
2020-06-14 17:58:50 +02:00
|
|
|
$context->vars_in_scope['$' . $byref_use_var] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $byref_use_var] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
|
2020-11-27 21:25:16 +01:00
|
|
|
NamedFunctionCallHandler::handle(
|
2020-03-12 16:05:42 +01:00
|
|
|
$statements_analyzer,
|
|
|
|
$codebase,
|
|
|
|
$stmt,
|
|
|
|
$real_stmt,
|
|
|
|
$function_name,
|
2020-11-27 22:24:59 +01:00
|
|
|
strtolower($function_call_info->function_id),
|
2020-03-12 16:05:42 +01:00
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if (!$statements_analyzer->node_data->getType($real_stmt)) {
|
|
|
|
$statements_analyzer->node_data->setType($real_stmt, Type::getMixed());
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-27 22:24:59 +01:00
|
|
|
* @return FunctionCallInfo
|
2020-03-12 16:05:42 +01:00
|
|
|
*/
|
2020-11-27 22:24:59 +01:00
|
|
|
private static function handleNamedFunction(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
PhpParser\Node\Name $function_name,
|
|
|
|
Context $context,
|
|
|
|
CodeLocation $code_location
|
|
|
|
) {
|
|
|
|
$function_call_info = new FunctionCallInfo();
|
|
|
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
$codebase_functions = $codebase->functions;
|
|
|
|
|
|
|
|
$original_function_id = implode('\\', $function_name->parts);
|
|
|
|
|
|
|
|
if (!$function_name instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$function_call_info->function_id = $codebase_functions->getFullyQualifiedFunctionNameFromString(
|
|
|
|
$original_function_id,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$function_call_info->function_id = $original_function_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$namespaced_function_exists = $codebase_functions->functionExists(
|
|
|
|
$statements_analyzer,
|
|
|
|
strtolower($function_call_info->function_id)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$namespaced_function_exists
|
|
|
|
&& !$function_name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
) {
|
|
|
|
$function_call_info->in_call_map = InternalCallMapHandler::inCallMap($original_function_id);
|
|
|
|
$function_call_info->is_stubbed = $codebase_functions->hasStubbedFunction($original_function_id);
|
|
|
|
|
|
|
|
if ($function_call_info->is_stubbed || $function_call_info->in_call_map) {
|
|
|
|
$function_call_info->function_id = $original_function_id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$function_call_info->in_call_map = InternalCallMapHandler::inCallMap($function_call_info->function_id);
|
|
|
|
$function_call_info->is_stubbed = $codebase_functions->hasStubbedFunction($function_call_info->function_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_call_info->function_exists
|
|
|
|
= $function_call_info->is_stubbed || $function_call_info->in_call_map || $namespaced_function_exists;
|
|
|
|
|
|
|
|
if ($function_call_info->function_exists
|
|
|
|
&& $codebase->store_node_types
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
|
|
|
ArgumentMapPopulator::recordArgumentPositions(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$codebase,
|
|
|
|
$function_call_info->function_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_predefined = true;
|
|
|
|
|
|
|
|
$is_maybe_root_function = !$function_name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
&& count($function_name->parts) === 1;
|
|
|
|
|
|
|
|
if (!$function_call_info->in_call_map) {
|
|
|
|
$predefined_functions = $codebase->config->getPredefinedFunctions();
|
|
|
|
$is_predefined = isset($predefined_functions[strtolower($original_function_id)])
|
|
|
|
|| isset($predefined_functions[strtolower($function_call_info->function_id)]);
|
|
|
|
|
|
|
|
if ($context->check_functions) {
|
|
|
|
if (self::checkFunctionExists(
|
|
|
|
$statements_analyzer,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$code_location,
|
|
|
|
$is_maybe_root_function
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
if (ArgumentsAnalyzer::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return $function_call_info;
|
2020-12-02 06:52:35 +01:00
|
|
|
} else {
|
|
|
|
$function_call_info->function_exists = true;
|
2020-11-27 22:24:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$function_call_info->function_exists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_call_info->function_params = null;
|
|
|
|
$function_call_info->defined_constants = [];
|
|
|
|
$function_call_info->global_variables = [];
|
|
|
|
|
|
|
|
if ($function_call_info->function_exists) {
|
|
|
|
if ($codebase->functions->params_provider->has($function_call_info->function_id)) {
|
|
|
|
$function_call_info->function_params = $codebase->functions->params_provider->getFunctionParams(
|
|
|
|
$statements_analyzer,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$stmt->args,
|
|
|
|
null,
|
|
|
|
$code_location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_call_info->function_params === null) {
|
|
|
|
if (!$function_call_info->in_call_map || $function_call_info->is_stubbed) {
|
|
|
|
try {
|
|
|
|
$function_call_info->function_storage = $function_storage = $codebase_functions->getStorage(
|
|
|
|
$statements_analyzer,
|
|
|
|
strtolower($function_call_info->function_id)
|
|
|
|
);
|
|
|
|
|
|
|
|
$function_call_info->function_params = $function_call_info->function_storage->params;
|
|
|
|
|
|
|
|
if (!$function_storage->allow_named_arg_calls) {
|
|
|
|
$function_call_info->allow_named_args = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$is_predefined) {
|
|
|
|
$function_call_info->defined_constants = $function_storage->defined_constants;
|
|
|
|
$function_call_info->global_variables = $function_storage->global_variables;
|
|
|
|
}
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
$function_call_info->function_params = [
|
|
|
|
new FunctionLikeParameter('args', false, null, null, null, false, false, true)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$function_callable = InternalCallMapHandler::getCallableFromCallMapById(
|
|
|
|
$codebase,
|
|
|
|
$function_call_info->function_id,
|
|
|
|
$stmt->args,
|
|
|
|
$statements_analyzer->node_data
|
|
|
|
);
|
|
|
|
|
|
|
|
$function_call_info->function_params = $function_callable->params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$function_name,
|
|
|
|
$function_call_info->function_id . '()'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $function_call_info;
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
private static function getAnalyzeNamedExpression(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
PhpParser\Node\Expr\FuncCall $real_stmt,
|
|
|
|
PhpParser\Node\Expr $function_name,
|
|
|
|
Context $context
|
2020-11-27 22:24:59 +01:00
|
|
|
): FunctionCallInfo {
|
|
|
|
$function_call_info = new FunctionCallInfo();
|
|
|
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2020-03-12 16:05:42 +01:00
|
|
|
|
|
|
|
$was_in_call = $context->inside_call;
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $function_name, $context) === false) {
|
|
|
|
$context->inside_call = $was_in_call;
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
return $function_call_info;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$context->inside_call = $was_in_call;
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->byref_uses = [];
|
2020-06-14 17:58:50 +02:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($stmt_name_type = $statements_analyzer->node_data->getType($function_name)) {
|
|
|
|
if ($stmt_name_type->isNull()) {
|
2018-04-07 21:16:46 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2020-03-12 16:05:42 +01:00
|
|
|
new NullFunctionCall(
|
|
|
|
'Cannot call function on null value',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-04-07 21:16:46 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-07 21:16:46 +02:00
|
|
|
)) {
|
2020-03-12 16:05:42 +01:00
|
|
|
// fall through
|
2018-04-07 21:16:46 +02:00
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
return $function_call_info;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_name_type->isNullable()) {
|
2018-09-18 22:42:51 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2020-03-12 16:05:42 +01:00
|
|
|
new PossiblyNullFunctionCall(
|
|
|
|
'Cannot call function on possibly null value',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-09-18 22:42:51 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-09-18 22:42:51 +02:00
|
|
|
)) {
|
2020-03-12 16:05:42 +01:00
|
|
|
// fall through
|
2018-09-18 22:42:51 +02:00
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
2019-02-18 19:51:27 +01:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
$invalid_function_call_types = [];
|
|
|
|
$has_valid_function_call_type = false;
|
2019-02-18 19:51:27 +01:00
|
|
|
|
2021-01-27 04:43:42 +01:00
|
|
|
$var_atomic_types = $stmt_name_type->getAtomicTypes();
|
|
|
|
|
|
|
|
while ($var_atomic_types) {
|
|
|
|
$var_type_part = \array_shift($var_atomic_types);
|
|
|
|
|
|
|
|
if ($var_type_part instanceof TTemplateParam) {
|
|
|
|
$var_atomic_types = \array_merge($var_atomic_types, $var_type_part->as->getAtomicTypes());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:46:43 +02:00
|
|
|
if ($var_type_part instanceof Type\Atomic\TClosure || $var_type_part instanceof TCallable) {
|
2020-08-26 22:51:22 +02:00
|
|
|
if (!$var_type_part->is_pure && $context->pure) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpureFunctionCall(
|
|
|
|
'Cannot call an impure function from a mutation-free context',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_params = $var_type_part->params;
|
2020-03-12 16:05:42 +01:00
|
|
|
|
|
|
|
if (($stmt_type = $statements_analyzer->node_data->getType($real_stmt))
|
|
|
|
&& $var_type_part->return_type
|
|
|
|
) {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$real_stmt,
|
|
|
|
Type::combineUnionTypes(
|
|
|
|
$stmt_type,
|
|
|
|
$var_type_part->return_type
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$real_stmt,
|
|
|
|
$var_type_part->return_type ?: Type::getMixed()
|
2019-02-18 19:51:27 +01:00
|
|
|
);
|
2020-01-14 22:56:09 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 05:32:01 +02:00
|
|
|
if ($var_type_part instanceof Type\Atomic\TClosure) {
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->byref_uses += $var_type_part->byref_uses;
|
2020-06-14 17:58:50 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_exists = true;
|
2020-03-12 16:05:42 +01:00
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
} elseif ($var_type_part instanceof TMixed || $var_type_part instanceof TTemplateParam) {
|
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedFunctionCall(
|
|
|
|
'Cannot call function on ' . $var_type_part->getId(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} elseif ($var_type_part instanceof TCallableObject
|
|
|
|
|| $var_type_part instanceof TCallableString
|
|
|
|
) {
|
|
|
|
// this is fine
|
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
} elseif (($var_type_part instanceof TNamedObject && $var_type_part->value === 'Closure')) {
|
|
|
|
// this is fine
|
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
} elseif ($var_type_part instanceof TString
|
|
|
|
|| $var_type_part instanceof Type\Atomic\TArray
|
|
|
|
|| $var_type_part instanceof Type\Atomic\TList
|
2020-08-30 17:44:14 +02:00
|
|
|
|| ($var_type_part instanceof Type\Atomic\TKeyedArray
|
2020-03-12 16:05:42 +01:00
|
|
|
&& count($var_type_part->properties) === 2)
|
|
|
|
) {
|
|
|
|
$potential_method_id = null;
|
|
|
|
|
2020-08-30 17:44:14 +02:00
|
|
|
if ($var_type_part instanceof Type\Atomic\TKeyedArray) {
|
|
|
|
$potential_method_id = CallableTypeComparator::getCallableMethodIdFromTKeyedArray(
|
2020-03-12 16:05:42 +01:00
|
|
|
$var_type_part,
|
|
|
|
$codebase,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2020-03-12 16:05:42 +01:00
|
|
|
$statements_analyzer->getFilePath()
|
2020-01-14 22:56:09 +01:00
|
|
|
);
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($potential_method_id === 'not-callable') {
|
|
|
|
$potential_method_id = null;
|
|
|
|
}
|
|
|
|
} elseif ($var_type_part instanceof Type\Atomic\TLiteralString) {
|
2020-06-04 21:40:44 +02:00
|
|
|
if (!$var_type_part->value) {
|
|
|
|
$invalid_function_call_types[] = '\'\'';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if (strpos($var_type_part->value, '::')) {
|
|
|
|
$parts = explode('::', strtolower($var_type_part->value));
|
2020-06-20 00:02:39 +02:00
|
|
|
$fq_class_name = $parts[0];
|
|
|
|
$fq_class_name = \preg_replace('/^\\\\/', '', $fq_class_name);
|
|
|
|
$potential_method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, $parts[1]);
|
2020-03-12 16:05:42 +01:00
|
|
|
} else {
|
2021-02-15 22:18:41 +01:00
|
|
|
$function_call_info->new_function_name = new VirtualFullyQualified(
|
2020-03-12 16:05:42 +01:00
|
|
|
$var_type_part->value,
|
|
|
|
$function_name->getAttributes()
|
|
|
|
);
|
2020-01-14 23:03:58 +01:00
|
|
|
}
|
2019-02-18 19:51:27 +01:00
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($potential_method_id) {
|
|
|
|
$codebase->methods->methodExists(
|
|
|
|
$potential_method_id,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2020-03-12 16:05:42 +01:00
|
|
|
null,
|
|
|
|
$statements_analyzer,
|
|
|
|
$statements_analyzer->getFilePath()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is also kind of fine
|
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
} elseif ($var_type_part instanceof TNull) {
|
|
|
|
// handled above
|
|
|
|
} elseif (!$var_type_part instanceof TNamedObject
|
|
|
|
|| !$codebase->classlikes->classOrInterfaceExists($var_type_part->value)
|
|
|
|
|| !$codebase->methods->methodExists(
|
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$var_type_part->value,
|
|
|
|
'__invoke'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$invalid_function_call_types[] = (string)$var_type_part;
|
2019-02-26 07:03:33 +01:00
|
|
|
} else {
|
2020-03-12 16:05:42 +01:00
|
|
|
self::analyzeInvokeCall(
|
2019-02-26 07:03:33 +01:00
|
|
|
$statements_analyzer,
|
2020-03-12 16:05:42 +01:00
|
|
|
$stmt,
|
|
|
|
$real_stmt,
|
|
|
|
$function_name,
|
2020-10-02 06:47:42 +02:00
|
|
|
$context,
|
|
|
|
$var_type_part
|
2019-02-26 07:03:33 +01:00
|
|
|
);
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
2019-02-26 07:03:33 +01:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($invalid_function_call_types) {
|
|
|
|
$var_type_part = reset($invalid_function_call_types);
|
|
|
|
|
|
|
|
if ($has_valid_function_call_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidFunctionCall(
|
|
|
|
'Cannot treat type ' . $var_type_part . ' as callable',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidFunctionCall(
|
|
|
|
'Cannot treat type ' . $var_type_part . ' as callable',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
return $function_call_info;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
2020-11-17 18:44:31 +01:00
|
|
|
|
|
|
|
if ($statements_analyzer->data_flow_graph instanceof TaintFlowGraph
|
|
|
|
&& $stmt_name_type->parent_nodes
|
2020-11-20 00:44:29 +01:00
|
|
|
&& $stmt_name_type->hasString()
|
2020-11-17 18:44:31 +01:00
|
|
|
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
|
|
|
) {
|
|
|
|
$arg_location = new CodeLocation($statements_analyzer->getSource(), $function_name);
|
|
|
|
|
|
|
|
$custom_call_sink = \Psalm\Internal\DataFlow\TaintSink::getForMethodArgument(
|
|
|
|
'variable-call',
|
|
|
|
'variable-call',
|
|
|
|
0,
|
|
|
|
$arg_location,
|
|
|
|
$arg_location
|
|
|
|
);
|
|
|
|
|
2020-11-20 01:01:19 +01:00
|
|
|
$custom_call_sink->taints = [\Psalm\Type\TaintKind::INPUT_CALLABLE];
|
2020-11-17 18:44:31 +01:00
|
|
|
|
|
|
|
$statements_analyzer->data_flow_graph->addSink($custom_call_sink);
|
|
|
|
|
2021-03-20 03:41:41 +01:00
|
|
|
$event = new AddRemoveTaintsEvent($stmt, $context, $statements_analyzer, $codebase);
|
|
|
|
|
|
|
|
$added_taints = $codebase->config->eventDispatcher->dispatchAddTaints($event);
|
|
|
|
$removed_taints = $codebase->config->eventDispatcher->dispatchRemoveTaints($event);
|
|
|
|
|
2020-11-17 18:44:31 +01:00
|
|
|
foreach ($stmt_name_type->parent_nodes as $parent_node) {
|
2021-03-20 03:41:41 +01:00
|
|
|
$statements_analyzer->data_flow_graph->addPath(
|
|
|
|
$parent_node,
|
|
|
|
$custom_call_sink,
|
|
|
|
'call',
|
|
|
|
$added_taints,
|
|
|
|
$removed_taints
|
|
|
|
);
|
2020-11-17 18:44:31 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$statements_analyzer->node_data->getType($real_stmt)) {
|
|
|
|
$statements_analyzer->node_data->setType($real_stmt, Type::getMixed());
|
|
|
|
}
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
return $function_call_info;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function analyzeInvokeCall(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
PhpParser\Node\Expr\FuncCall $real_stmt,
|
|
|
|
PhpParser\Node\Expr $function_name,
|
2020-10-02 06:47:42 +02:00
|
|
|
Context $context,
|
|
|
|
Type\Atomic $atomic_type
|
2020-03-12 16:05:42 +01:00
|
|
|
) : void {
|
|
|
|
$old_data_provider = $statements_analyzer->node_data;
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
|
|
|
|
|
2021-02-15 22:18:41 +01:00
|
|
|
$fake_method_call = new VirtualMethodCall(
|
2020-03-12 16:05:42 +01:00
|
|
|
$function_name,
|
2021-02-15 22:18:41 +01:00
|
|
|
new VirtualIdentifier('__invoke', $function_name->getAttributes()),
|
2020-03-12 16:05:42 +01:00
|
|
|
$stmt->args
|
|
|
|
);
|
|
|
|
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
|
|
if (!in_array('InternalMethod', $suppressed_issues, true)) {
|
|
|
|
$statements_analyzer->addSuppressedIssues(['InternalMethod']);
|
|
|
|
}
|
|
|
|
|
2020-10-02 06:47:42 +02:00
|
|
|
$statements_analyzer->node_data->setType($function_name, new Type\Union([$atomic_type]));
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
\Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$fake_method_call,
|
|
|
|
$context,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!in_array('InternalMethod', $suppressed_issues, true)) {
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['InternalMethod']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fake_method_call_type = $statements_analyzer->node_data->getType($fake_method_call);
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = $old_data_provider;
|
|
|
|
|
|
|
|
if ($stmt_type = $statements_analyzer->node_data->getType($real_stmt)) {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$real_stmt,
|
|
|
|
Type::combineUnionTypes(
|
|
|
|
$fake_method_call_type ?: Type::getMixed(),
|
|
|
|
$stmt_type
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$real_stmt,
|
|
|
|
$fake_method_call_type ?: Type::getMixed()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function processAssertFunctionEffects(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
PhpParser\Node\Arg $first_arg,
|
|
|
|
Context $context
|
|
|
|
) : void {
|
2020-08-26 21:35:29 +02:00
|
|
|
$first_arg_value_id = \spl_object_id($first_arg->value);
|
|
|
|
|
2020-11-03 22:15:44 +01:00
|
|
|
$assert_clauses = FormulaGenerator::getFormula(
|
2020-08-26 21:35:29 +02:00
|
|
|
$first_arg_value_id,
|
|
|
|
$first_arg_value_id,
|
2020-03-12 16:05:42 +01:00
|
|
|
$first_arg->value,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer,
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
|
|
|
|
\Psalm\Internal\Analyzer\AlgebraAnalyzer::checkForParadox(
|
|
|
|
$context->clauses,
|
|
|
|
$assert_clauses,
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
2020-11-07 06:58:20 +01:00
|
|
|
[]
|
2020-03-12 16:05:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$simplified_clauses = Algebra::simplifyCNF(array_merge($context->clauses, $assert_clauses));
|
|
|
|
|
|
|
|
$assert_type_assertions = Algebra::getTruthsFromFormula($simplified_clauses);
|
|
|
|
|
2020-11-25 20:04:02 +01:00
|
|
|
$changed_var_ids = [];
|
2020-03-12 16:05:42 +01:00
|
|
|
|
2020-11-25 20:04:02 +01:00
|
|
|
if ($assert_type_assertions) {
|
2020-03-12 16:05:42 +01:00
|
|
|
// while in an and, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x && $x->foo())
|
|
|
|
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
|
|
|
|
$assert_type_assertions,
|
|
|
|
$assert_type_assertions,
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$changed_var_ids,
|
|
|
|
array_map(
|
2020-09-30 18:28:13 +02:00
|
|
|
function ($_): bool {
|
2019-02-26 07:03:33 +01:00
|
|
|
return true;
|
|
|
|
},
|
2020-03-12 16:05:42 +01:00
|
|
|
$assert_type_assertions
|
|
|
|
),
|
|
|
|
$statements_analyzer,
|
|
|
|
$statements_analyzer->getTemplateTypeMap() ?: [],
|
|
|
|
$context->inside_loop,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
2019-02-26 07:03:33 +01:00
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
foreach ($changed_var_ids as $var_id => $_) {
|
2020-05-02 20:55:21 +02:00
|
|
|
$first_appearance = $statements_analyzer->getFirstAppearance($var_id);
|
|
|
|
|
2020-05-08 20:21:10 +02:00
|
|
|
if ($first_appearance
|
|
|
|
&& isset($context->vars_in_scope[$var_id])
|
|
|
|
&& $context->vars_in_scope[$var_id]->hasMixed()
|
|
|
|
) {
|
2020-05-02 20:55:21 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
IssueBuffer::remove(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
'MixedAssignment',
|
|
|
|
$first_appearance->raw_file_start
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($op_vars_in_scope[$var_id])) {
|
|
|
|
$op_vars_in_scope[$var_id]->from_docblock = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
}
|
2020-11-25 20:04:02 +01:00
|
|
|
|
|
|
|
if ($changed_var_ids) {
|
|
|
|
$simplified_clauses = Context::removeReconciledClauses($simplified_clauses, $changed_var_ids)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->clauses = $simplified_clauses;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function checkFunctionCallPurity(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
PhpParser\Node $function_name,
|
2020-11-27 22:24:59 +01:00
|
|
|
FunctionCallInfo $function_call_info,
|
2020-03-12 16:05:42 +01:00
|
|
|
Context $context
|
|
|
|
) : void {
|
|
|
|
$config = $codebase->config;
|
|
|
|
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& ($context->mutation_free
|
|
|
|
|| $context->external_mutation_free
|
|
|
|
|| $codebase->find_unused_variables
|
2020-08-23 19:34:32 +02:00
|
|
|
|| !$config->remember_property_assignments_after_call
|
2020-08-28 18:42:55 +02:00
|
|
|
|| ($statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
|
|
|
&& $statements_analyzer->getSource()->track_mutations))
|
2019-10-14 23:10:30 +02:00
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
$must_use = true;
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
$callmap_function_pure = $function_call_info->function_id && $function_call_info->in_call_map
|
2020-06-14 17:06:53 +02:00
|
|
|
? $codebase->functions->isCallMapFunctionPure(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer->node_data,
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_id,
|
2020-06-14 17:06:53 +02:00
|
|
|
$stmt->args,
|
|
|
|
$must_use
|
|
|
|
)
|
2020-03-12 16:05:42 +01:00
|
|
|
: null;
|
|
|
|
|
2020-11-27 22:24:59 +01:00
|
|
|
if ((!$function_call_info->in_call_map
|
|
|
|
&& $function_call_info->function_storage
|
|
|
|
&& !$function_call_info->function_storage->pure)
|
2020-03-12 16:05:42 +01:00
|
|
|
|| ($callmap_function_pure === false)
|
2019-11-25 17:44:54 +01:00
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
if ($context->mutation_free || $context->external_mutation_free) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpureFunctionCall(
|
|
|
|
'Cannot call an impure function from a mutation-free context',
|
|
|
|
new CodeLocation($statements_analyzer, $function_name)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2020-08-28 18:42:55 +02:00
|
|
|
} elseif ($statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
2020-08-23 16:28:26 +02:00
|
|
|
) {
|
2020-08-25 01:24:27 +02:00
|
|
|
$statements_analyzer->getSource()->inferred_has_mutation = true;
|
2020-08-23 16:28:26 +02:00
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$config->remember_property_assignments_after_call) {
|
2020-12-08 22:39:06 +01:00
|
|
|
$context->removeMutableObjectVars();
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
2020-11-27 22:24:59 +01:00
|
|
|
} elseif ($function_call_info->function_id
|
|
|
|
&& (($function_call_info->function_storage
|
|
|
|
&& $function_call_info->function_storage->pure
|
|
|
|
&& !$function_call_info->function_storage->assertions
|
2020-03-12 16:05:42 +01:00
|
|
|
&& $must_use)
|
|
|
|
|| ($callmap_function_pure === true && $must_use))
|
|
|
|
&& $codebase->find_unused_variables
|
|
|
|
&& !$context->inside_conditional
|
|
|
|
&& !$context->inside_unset
|
|
|
|
) {
|
2021-03-24 00:34:12 +01:00
|
|
|
/**
|
|
|
|
* If a function is pure, and has the return type of 'no-return',
|
|
|
|
* it's okay to dismiss it's return value.
|
|
|
|
*/
|
2021-06-25 16:14:49 +02:00
|
|
|
if (!$context->insideUse()
|
2021-03-31 16:03:08 +02:00
|
|
|
&& !self::callUsesByReferenceArguments($function_call_info, $stmt)
|
|
|
|
&& !(
|
2021-03-24 00:34:12 +01:00
|
|
|
$function_call_info->function_storage &&
|
|
|
|
$function_call_info->function_storage->return_type &&
|
2021-03-24 00:42:56 +01:00
|
|
|
$function_call_info->function_storage->return_type->isNever()
|
2021-03-24 00:34:12 +01:00
|
|
|
)
|
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnusedFunctionCall(
|
2020-11-27 22:24:59 +01:00
|
|
|
'The call to ' . $function_call_info->function_id . ' is not used',
|
2020-03-12 16:05:42 +01:00
|
|
|
new CodeLocation($statements_analyzer, $function_name),
|
2020-11-27 22:24:59 +01:00
|
|
|
$function_call_info->function_id
|
2020-03-12 16:05:42 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
|
|
|
$stmt->pure = true;
|
|
|
|
}
|
2019-10-14 23:10:30 +02:00
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 14:05:59 +01:00
|
|
|
|
|
|
|
private static function callUsesByReferenceArguments(
|
|
|
|
FunctionCallInfo $function_call_info,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt
|
|
|
|
): bool {
|
|
|
|
// If the function doesn't have any by-reference parameters
|
|
|
|
// we shouldn't look any further.
|
|
|
|
if (!$function_call_info->hasByReferenceParameters() || null === $function_call_info->function_params) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = $function_call_info->function_params;
|
|
|
|
|
|
|
|
// If no arguments were passed
|
|
|
|
if (0 === \count($stmt->args)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->args as $index => $argument) {
|
|
|
|
$parameter = null;
|
|
|
|
if (null !== $argument->name) {
|
|
|
|
$argument_name = $argument->name->toString();
|
|
|
|
foreach ($parameters as $param) {
|
|
|
|
if ($param->name === $argument_name) {
|
|
|
|
$parameter = $param;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$parameter = $parameters[$index] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parameter && $parameter->by_ref) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|