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;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\FunctionAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
2020-04-07 06:13:56 +02:00
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
|
2019-02-26 07:03:33 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\AssertionFinder;
|
2020-05-18 21:13:27 +02:00
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
|
2020-05-19 18:56:23 +02:00
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-07-22 01:40:35 +02:00
|
|
|
|
use Psalm\Internal\Type\Comparator\CallableTypeComparator;
|
|
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2020-05-25 19:10:06 +02:00
|
|
|
|
use Psalm\Internal\Codebase\InternalCallMapHandler;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
|
use Psalm\Context;
|
2018-11-12 16:46:55 +01:00
|
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2020-09-21 05:59:52 +02:00
|
|
|
|
use Psalm\Internal\ControlFlow\TaintSource;
|
|
|
|
|
use Psalm\Internal\ControlFlow\ControlFlowNode;
|
2020-09-30 18:28:13 +02:00
|
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2019-06-15 18:09:15 +02:00
|
|
|
|
use Psalm\Issue\DeprecatedFunction;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2019-02-10 22:15:52 +01:00
|
|
|
|
use Psalm\Issue\MixedFunctionCall;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
use Psalm\Issue\InvalidFunctionCall;
|
2019-07-18 07:31:48 +02:00
|
|
|
|
use Psalm\Issue\ImpureFunctionCall;
|
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;
|
2019-01-23 05:42:54 +01:00
|
|
|
|
use Psalm\Storage\Assertion;
|
2020-03-12 16:05:42 +01:00
|
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
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;
|
2019-02-22 03:40:06 +01:00
|
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
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;
|
2018-05-07 07:26:06 +02:00
|
|
|
|
use Psalm\Type\Algebra;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
use Psalm\Type\Reconciler;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function count;
|
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 implode;
|
|
|
|
|
use function strtolower;
|
|
|
|
|
use function array_merge;
|
|
|
|
|
use function is_string;
|
|
|
|
|
use function array_map;
|
|
|
|
|
use function extension_loaded;
|
|
|
|
|
use function strpos;
|
2019-11-04 03:27:40 +01:00
|
|
|
|
use Psalm\Internal\Type\TemplateResult;
|
2019-12-21 01:34:20 +01:00
|
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2020-02-15 02:54:26 +01:00
|
|
|
|
use function explode;
|
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-02-21 17:32:52 +01:00
|
|
|
|
$function_id = null;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$function_params = null;
|
|
|
|
|
$in_call_map = false;
|
|
|
|
|
|
|
|
|
|
$is_stubbed = false;
|
|
|
|
|
|
|
|
|
|
$function_storage = null;
|
|
|
|
|
|
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-02-04 00:52:35 +01:00
|
|
|
|
$codebase_functions = $codebase->functions;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$config = $codebase->config;
|
|
|
|
|
$defined_constants = [];
|
2018-05-31 04:09:46 +02:00
|
|
|
|
$global_variables = [];
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
$function_exists = false;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-01-24 19:34:50 +01:00
|
|
|
|
$stmt = new PhpParser\Node\Expr\FuncCall(
|
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;
|
|
|
|
|
|
2020-01-24 19:34:50 +01:00
|
|
|
|
$stmt = new PhpParser\Node\Expr\FuncCall(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$function_name,
|
2020-01-24 19:34:50 +01:00
|
|
|
|
[new PhpParser\Node\Arg($stmt->args[1]->value, false, true)],
|
|
|
|
|
$stmt->getAttributes()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 17:58:50 +02:00
|
|
|
|
$byref_uses = [];
|
|
|
|
|
|
2020-10-03 02:58:51 +02:00
|
|
|
|
$allow_named_args = true;
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($function_name instanceof PhpParser\Node\Expr) {
|
2020-09-02 06:17:41 +02:00
|
|
|
|
[$expr_function_exists, $expr_function_name, $expr_function_params, $byref_uses]
|
2020-06-14 17:58:50 +02:00
|
|
|
|
= self::getAnalyzeNamedExpression(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$codebase,
|
|
|
|
|
$stmt,
|
|
|
|
|
$real_stmt,
|
|
|
|
|
$function_name,
|
|
|
|
|
$context
|
|
|
|
|
);
|
2019-08-23 05:26:04 +02:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($expr_function_exists === false) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
return true;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($expr_function_exists === true) {
|
|
|
|
|
$function_exists = true;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($expr_function_name) {
|
|
|
|
|
$function_name = $expr_function_name;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($expr_function_params) {
|
|
|
|
|
$function_params = $expr_function_params;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$original_function_id = implode('\\', $function_name->parts);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if (!$function_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2019-01-05 20:50:11 +01:00
|
|
|
|
$function_id = $codebase_functions->getFullyQualifiedFunctionNameFromString(
|
|
|
|
|
$original_function_id,
|
|
|
|
|
$statements_analyzer
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$function_id = $original_function_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$namespaced_function_exists = $codebase_functions->functionExists(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
strtolower($function_id)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!$namespaced_function_exists
|
2020-03-12 16:05:42 +01:00
|
|
|
|
&& !$function_name instanceof PhpParser\Node\Name\FullyQualified
|
2019-01-05 20:50:11 +01:00
|
|
|
|
) {
|
2020-05-25 19:10:06 +02:00
|
|
|
|
$in_call_map = InternalCallMapHandler::inCallMap($original_function_id);
|
2019-01-05 20:50:11 +01:00
|
|
|
|
$is_stubbed = $codebase_functions->hasStubbedFunction($original_function_id);
|
|
|
|
|
|
|
|
|
|
if ($is_stubbed || $in_call_map) {
|
|
|
|
|
$function_id = $original_function_id;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-05-25 19:10:06 +02:00
|
|
|
|
$in_call_map = InternalCallMapHandler::inCallMap($function_id);
|
2019-01-05 20:50:11 +01:00
|
|
|
|
$is_stubbed = $codebase_functions->hasStubbedFunction($function_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($is_stubbed || $in_call_map || $namespaced_function_exists) {
|
|
|
|
|
$function_exists = true;
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-07-19 03:50:59 +02:00
|
|
|
|
if ($function_exists
|
2019-10-20 05:03:59 +02:00
|
|
|
|
&& $codebase->store_node_types
|
2019-07-19 03:50:59 +02:00
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
) {
|
|
|
|
|
ArgumentMapPopulator::recordArgumentPositions(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
|
|
|
|
$codebase,
|
|
|
|
|
$function_id
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$is_predefined = true;
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$is_maybe_root_function = !$function_name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
|
&& count($function_name->parts) === 1;
|
2018-02-25 17:13:00 +01:00
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
if (!$in_call_map) {
|
|
|
|
|
$predefined_functions = $config->getPredefinedFunctions();
|
2019-01-05 20:50:11 +01:00
|
|
|
|
$is_predefined = isset($predefined_functions[strtolower($original_function_id)])
|
|
|
|
|
|| isset($predefined_functions[strtolower($function_id)]);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
if ($context->check_functions) {
|
|
|
|
|
if (self::checkFunctionExists(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-02-21 17:32:52 +01:00
|
|
|
|
$function_id,
|
2018-02-25 17:13:00 +01:00
|
|
|
|
$code_location,
|
|
|
|
|
$is_maybe_root_function
|
2018-01-29 00:29:38 +01:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2020-05-19 04:57:00 +02:00
|
|
|
|
if (ArgumentsAnalyzer::analyze(
|
2019-12-18 17:37:48 +01:00
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
2020-10-03 02:58:51 +02:00
|
|
|
|
true,
|
2019-12-18 17:37:48 +01:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
return true;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$function_exists = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($function_exists) {
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$function_params = null;
|
|
|
|
|
|
|
|
|
|
if ($codebase->functions->params_provider->has($function_id)) {
|
|
|
|
|
$function_params = $codebase->functions->params_provider->getFunctionParams(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$function_id,
|
|
|
|
|
$stmt->args
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
|
if ($function_params === null) {
|
|
|
|
|
if (!$in_call_map || $is_stubbed) {
|
2019-12-21 01:34:20 +01:00
|
|
|
|
try {
|
|
|
|
|
$function_storage = $codebase_functions->getStorage(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
strtolower($function_id)
|
|
|
|
|
);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
2019-12-21 01:34:20 +01:00
|
|
|
|
$function_params = $function_storage->params;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2020-10-03 02:58:51 +02:00
|
|
|
|
if (!$function_storage->allow_named_arg_calls) {
|
|
|
|
|
$allow_named_args = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 01:34:20 +01:00
|
|
|
|
if (!$is_predefined) {
|
|
|
|
|
$defined_constants = $function_storage->defined_constants;
|
|
|
|
|
$global_variables = $function_storage->global_variables;
|
|
|
|
|
}
|
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
|
$function_params = [
|
|
|
|
|
new FunctionLikeParameter('args', false, null, null, null, false, false, true)
|
|
|
|
|
];
|
2019-03-01 14:57:10 +01:00
|
|
|
|
}
|
2019-12-21 01:34:20 +01:00
|
|
|
|
} else {
|
2020-05-25 19:10:06 +02:00
|
|
|
|
$function_callable = InternalCallMapHandler::getCallableFromCallMapById(
|
2019-03-01 14:57:10 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$function_id,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$stmt->args,
|
|
|
|
|
$statements_analyzer->node_data
|
2019-03-01 14:57:10 +01:00
|
|
|
|
);
|
2019-06-15 22:10:48 +02:00
|
|
|
|
|
|
|
|
|
$function_params = $function_callable->params;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
|
if ($codebase->store_node_types
|
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
) {
|
2018-10-26 22:17:15 +02:00
|
|
|
|
$codebase->analyzer->addNodeReference(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFilePath(),
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$function_name,
|
2018-10-26 22:17:15 +02:00
|
|
|
|
$function_id . '()'
|
|
|
|
|
);
|
|
|
|
|
}
|
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-05-19 04:57:00 +02:00
|
|
|
|
if (ArgumentsAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$stmt->args,
|
|
|
|
|
$function_params,
|
2018-02-21 17:32:52 +01:00
|
|
|
|
$function_id,
|
2020-10-03 02:58:51 +02:00
|
|
|
|
$allow_named_args,
|
2018-01-29 00:29:38 +01:00
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 04:38:30 +01:00
|
|
|
|
if ($set_inside_conditional) {
|
|
|
|
|
$context->inside_conditional = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result = null;
|
2020-10-12 19:46:43 +02:00
|
|
|
|
$function_callable = null;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
2018-11-16 00:50:08 +01:00
|
|
|
|
if ($function_exists) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($function_name instanceof PhpParser\Node\Name && $function_id) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
if (!$is_stubbed && $in_call_map) {
|
2020-05-25 19:10:06 +02:00
|
|
|
|
$function_callable = \Psalm\Internal\Codebase\InternalCallMapHandler::getCallableFromCallMapById(
|
2018-11-11 18:19:53 +01:00
|
|
|
|
$codebase,
|
2018-02-21 17:32:52 +01:00
|
|
|
|
$function_id,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$stmt->args,
|
|
|
|
|
$statements_analyzer->node_data
|
2018-01-29 00:29:38 +01:00
|
|
|
|
);
|
2019-06-15 22:10:48 +02:00
|
|
|
|
|
|
|
|
|
$function_params = $function_callable->params;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result = new TemplateResult([], []);
|
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
|
// do this here to allow closure param checks
|
2018-04-10 00:49:11 +02:00
|
|
|
|
if ($function_params !== null
|
2020-05-19 04:57:00 +02:00
|
|
|
|
&& ArgumentsAnalyzer::checkArgumentsMatch(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-04-10 03:50:38 +02:00
|
|
|
|
$stmt->args,
|
|
|
|
|
$function_id,
|
|
|
|
|
$function_params,
|
|
|
|
|
$function_storage,
|
|
|
|
|
null,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$template_result,
|
2018-04-10 03:50:38 +02:00
|
|
|
|
$code_location,
|
|
|
|
|
$context
|
2020-05-19 04:57:00 +02:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2018-01-29 00:29:38 +01:00
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
CallAnalyzer::checkTemplateResult(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$template_result,
|
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($function_name instanceof PhpParser\Node\Name && $function_id) {
|
|
|
|
|
$stmt_type = self::getFunctionCallReturnType(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$codebase,
|
|
|
|
|
$stmt,
|
|
|
|
|
$function_name,
|
|
|
|
|
$function_id,
|
|
|
|
|
$in_call_map,
|
|
|
|
|
$is_stubbed,
|
|
|
|
|
$function_storage,
|
2020-10-12 19:46:43 +02:00
|
|
|
|
$function_callable,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$template_result,
|
|
|
|
|
$context
|
|
|
|
|
);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
2020-06-23 21:52:19 +02:00
|
|
|
|
$statements_analyzer->node_data->setType($real_stmt, $stmt_type);
|
2020-02-13 13:04:02 +01:00
|
|
|
|
|
|
|
|
|
if ($config->after_every_function_checks) {
|
|
|
|
|
foreach ($config->after_every_function_checks as $plugin_fq_class_name) {
|
|
|
|
|
$plugin_fq_class_name::afterEveryFunctionCallAnalysis(
|
|
|
|
|
$stmt,
|
|
|
|
|
$function_id,
|
|
|
|
|
$context,
|
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
|
$codebase
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($defined_constants as $const_name => $const_type) {
|
|
|
|
|
$context->constants[$const_name] = clone $const_type;
|
|
|
|
|
$context->vars_in_scope[$const_name] = clone $const_type;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 04:09:46 +02:00
|
|
|
|
foreach ($global_variables as $var_id => $_) {
|
2018-06-01 03:59:55 +02:00
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
2018-05-31 04:09:46 +02:00
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:08:41 +02:00
|
|
|
|
if ($function_name instanceof PhpParser\Node\Name
|
|
|
|
|
&& $function_name->parts === ['assert']
|
|
|
|
|
&& isset($stmt->args[0])
|
2018-01-29 00:29:38 +01:00
|
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
self::processAssertFunctionEffects(
|
2020-02-09 17:42:41 +01:00
|
|
|
|
$statements_analyzer,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$codebase,
|
2020-02-09 17:42:41 +01:00
|
|
|
|
$stmt,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$stmt->args[0],
|
|
|
|
|
$context
|
2019-12-07 20:03:20 +01:00
|
|
|
|
);
|
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,
|
|
|
|
|
$function_id,
|
|
|
|
|
$in_call_map,
|
|
|
|
|
$function_storage,
|
|
|
|
|
$context
|
|
|
|
|
);
|
2019-08-13 19:15:23 +02:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($function_storage) {
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$generic_params = $template_result ? $template_result->upper_bounds : [];
|
2019-08-31 20:54:59 +02:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($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,
|
2018-05-28 21:07:42 +02:00
|
|
|
|
$function_storage->assertions,
|
|
|
|
|
$stmt->args,
|
2019-11-04 03:27:40 +01:00
|
|
|
|
$generic_params,
|
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
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
if ($function_storage->if_true_assertions) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data->setIfTrueAssertions(
|
|
|
|
|
$stmt,
|
|
|
|
|
array_map(
|
|
|
|
|
function (Assertion $assertion) use ($generic_params) : Assertion {
|
2019-12-30 16:01:31 +01:00
|
|
|
|
return $assertion->getUntemplatedCopy($generic_params ?: [], null);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
},
|
|
|
|
|
$function_storage->if_true_assertions
|
|
|
|
|
)
|
2019-01-23 05:42:54 +01:00
|
|
|
|
);
|
2018-07-11 17:22:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($function_storage->if_false_assertions) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data->setIfFalseAssertions(
|
|
|
|
|
$stmt,
|
|
|
|
|
array_map(
|
|
|
|
|
function (Assertion $assertion) use ($generic_params) : Assertion {
|
2019-12-30 16:01:31 +01:00
|
|
|
|
return $assertion->getUntemplatedCopy($generic_params ?: [], null);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
},
|
|
|
|
|
$function_storage->if_false_assertions
|
|
|
|
|
)
|
2019-01-23 05:42:54 +01:00
|
|
|
|
);
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2019-06-15 18:09:15 +02:00
|
|
|
|
|
|
|
|
|
if ($function_storage->deprecated && $function_id) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DeprecatedFunction(
|
|
|
|
|
'The function ' . $function_id . ' has been marked as deprecated',
|
|
|
|
|
$code_location,
|
|
|
|
|
$function_id
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 17:58:50 +02:00
|
|
|
|
if ($byref_uses) {
|
|
|
|
|
foreach ($byref_uses as $byref_use_var => $_) {
|
|
|
|
|
$context->vars_in_scope['$' . $byref_use_var] = Type::getMixed();
|
|
|
|
|
$context->vars_possibly_in_scope['$' . $byref_use_var] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($function_name instanceof PhpParser\Node\Name) {
|
|
|
|
|
self::handleNamedFunction(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$codebase,
|
|
|
|
|
$stmt,
|
|
|
|
|
$real_stmt,
|
|
|
|
|
$function_name,
|
|
|
|
|
$function_id,
|
|
|
|
|
$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-06-14 17:58:50 +02:00
|
|
|
|
* @return array{
|
|
|
|
|
* ?bool,
|
|
|
|
|
* ?PhpParser\Node\Expr|PhpParser\Node\Name,
|
|
|
|
|
* array<int, FunctionLikeParameter>|null,
|
|
|
|
|
* ?array<string, bool>
|
|
|
|
|
* }
|
2020-03-12 16:05:42 +01:00
|
|
|
|
*/
|
|
|
|
|
private static function getAnalyzeNamedExpression(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $real_stmt,
|
|
|
|
|
PhpParser\Node\Expr $function_name,
|
|
|
|
|
Context $context
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): array {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$function_params = null;
|
|
|
|
|
|
|
|
|
|
$explicit_function_name = null;
|
|
|
|
|
$function_exists = null;
|
|
|
|
|
$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-06-14 17:58:50 +02:00
|
|
|
|
return [false, null, null, null];
|
2020-03-12 16:05:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->inside_call = $was_in_call;
|
|
|
|
|
|
2020-06-14 17:58:50 +02:00
|
|
|
|
$byref_uses = [];
|
|
|
|
|
|
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-06-14 17:58:50 +02:00
|
|
|
|
return [false, null, null, null];
|
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
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
foreach ($stmt_name_type->getAtomicTypes() as $var_type_part) {
|
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-03-12 16:05:42 +01:00
|
|
|
|
$function_params = $var_type_part->params;
|
|
|
|
|
|
|
|
|
|
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-06-14 17:58:50 +02:00
|
|
|
|
$byref_uses += $var_type_part->byref_uses;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$function_exists = true;
|
|
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
|
} elseif ($var_type_part instanceof TTemplateParam && $var_type_part->as->hasCallableType()) {
|
|
|
|
|
$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 {
|
|
|
|
|
$explicit_function_name = new PhpParser\Node\Name\FullyQualified(
|
|
|
|
|
$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-06-14 17:58:50 +02:00
|
|
|
|
return [false, null, null, null];
|
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-06-14 17:58:50 +02:00
|
|
|
|
return [
|
|
|
|
|
$function_exists,
|
|
|
|
|
$explicit_function_name ?: $function_name,
|
|
|
|
|
$function_params,
|
|
|
|
|
$byref_uses
|
|
|
|
|
];
|
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;
|
|
|
|
|
|
|
|
|
|
$fake_method_call = new PhpParser\Node\Expr\MethodCall(
|
|
|
|
|
$function_name,
|
|
|
|
|
new PhpParser\Node\Identifier('__invoke', $function_name->getAttributes()),
|
|
|
|
|
$stmt->args
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
|
|
|
|
if (!in_array('PossiblyNullReference', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues(['PossiblyNullReference']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!in_array('InternalMethod', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues(['InternalMethod']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 05:41:05 +02:00
|
|
|
|
if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues(['PossiblyInvalidMethodCall']);
|
|
|
|
|
}
|
|
|
|
|
|
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('PossiblyNullReference', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['PossiblyNullReference']);
|
|
|
|
|
}
|
2019-02-26 07:03:33 +01:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if (!in_array('InternalMethod', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['InternalMethod']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 05:41:05 +02:00
|
|
|
|
if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['PossiblyInvalidMethodCall']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$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-03-12 16:05:42 +01:00
|
|
|
|
$assert_clauses = \Psalm\Type\Algebra::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
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$cond_assigned_var_ids = [];
|
|
|
|
|
|
|
|
|
|
\Psalm\Internal\Analyzer\AlgebraAnalyzer::checkForParadox(
|
|
|
|
|
$context->clauses,
|
|
|
|
|
$assert_clauses,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
|
|
|
|
$cond_assigned_var_ids
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$simplified_clauses = Algebra::simplifyCNF(array_merge($context->clauses, $assert_clauses));
|
|
|
|
|
|
|
|
|
|
$assert_type_assertions = Algebra::getTruthsFromFormula($simplified_clauses);
|
|
|
|
|
|
|
|
|
|
if ($assert_type_assertions) {
|
|
|
|
|
$changed_var_ids = [];
|
|
|
|
|
|
|
|
|
|
// 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-05-15 16:18:05 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param non-empty-string $function_id
|
|
|
|
|
*/
|
2020-03-12 16:05:42 +01:00
|
|
|
|
private static function getFunctionCallReturnType(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
|
PhpParser\Node\Name $function_name,
|
|
|
|
|
string $function_id,
|
|
|
|
|
bool $in_call_map,
|
|
|
|
|
bool $is_stubbed,
|
|
|
|
|
?FunctionLikeStorage $function_storage,
|
2020-10-12 19:46:43 +02:00
|
|
|
|
?TCallable $callmap_callable,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
TemplateResult $template_result,
|
|
|
|
|
Context $context
|
2020-06-23 21:52:19 +02:00
|
|
|
|
) : Type\Union {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$stmt_type = null;
|
|
|
|
|
$config = $codebase->config;
|
|
|
|
|
|
|
|
|
|
if ($codebase->functions->return_type_provider->has($function_id)) {
|
|
|
|
|
$stmt_type = $codebase->functions->return_type_provider->getReturnType(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$function_id,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
$context,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $function_name)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$stmt_type) {
|
|
|
|
|
if (!$in_call_map || $is_stubbed) {
|
|
|
|
|
if ($function_storage && $function_storage->template_types) {
|
|
|
|
|
foreach ($function_storage->template_types as $template_name => $_) {
|
2020-04-07 06:13:56 +02:00
|
|
|
|
if (!isset($template_result->upper_bounds[$template_name])) {
|
2020-05-01 22:02:53 +02:00
|
|
|
|
if ($template_name === 'TFunctionArgCount') {
|
|
|
|
|
$template_result->upper_bounds[$template_name] = [
|
|
|
|
|
'fn-' . $function_id => [Type::getInt(false, count($stmt->args)), 0]
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
$template_result->upper_bounds[$template_name] = [
|
|
|
|
|
'fn-' . $function_id => [Type::getEmpty(), 0]
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($function_storage && !$context->isSuppressingExceptions($statements_analyzer)) {
|
|
|
|
|
$context->mergeFunctionExceptions(
|
|
|
|
|
$function_storage,
|
2019-02-26 07:03:33 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-02-13 22:38:58 +01:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
try {
|
|
|
|
|
if ($function_storage && $function_storage->return_type) {
|
|
|
|
|
$return_type = clone $function_storage->return_type;
|
|
|
|
|
|
2020-04-07 06:13:56 +02:00
|
|
|
|
if ($template_result->upper_bounds && $function_storage->template_types) {
|
2020-05-11 04:45:01 +02:00
|
|
|
|
$return_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2020-03-29 19:37:37 +02:00
|
|
|
|
$codebase,
|
|
|
|
|
$return_type,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$return_type->replaceTemplateTypesWithArgTypes(
|
2020-04-07 06:13:56 +02:00
|
|
|
|
$template_result,
|
2020-03-29 16:42:57 +02:00
|
|
|
|
$codebase
|
2020-03-12 16:05:42 +01:00
|
|
|
|
);
|
2020-02-13 22:38:58 +01:00
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
|
2020-05-11 04:45:01 +02:00
|
|
|
|
$return_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$return_type,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$return_type_location = $function_storage->return_type_location;
|
|
|
|
|
|
|
|
|
|
if ($config->after_function_checks) {
|
|
|
|
|
$file_manipulations = [];
|
|
|
|
|
|
|
|
|
|
foreach ($config->after_function_checks as $plugin_fq_class_name) {
|
|
|
|
|
$plugin_fq_class_name::afterFunctionCallAnalysis(
|
|
|
|
|
$stmt,
|
|
|
|
|
$function_id,
|
|
|
|
|
$context,
|
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
|
$codebase,
|
2020-10-05 05:39:08 +02:00
|
|
|
|
$return_type,
|
|
|
|
|
$file_manipulations
|
2020-03-12 16:05:42 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
|
FileManipulationBuffer::add(
|
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
|
$file_manipulations
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$stmt_type = $return_type;
|
|
|
|
|
$return_type->by_ref = $function_storage->returns_by_ref;
|
|
|
|
|
|
|
|
|
|
// only check the type locally if it's defined externally
|
|
|
|
|
if ($return_type_location &&
|
|
|
|
|
!$is_stubbed && // makes lookups or array_* functions quicker
|
|
|
|
|
!$config->isInProjectDirs($return_type_location->file_path)
|
|
|
|
|
) {
|
|
|
|
|
$return_type->check(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2020-04-27 06:05:20 +02:00
|
|
|
|
$context->phantom_classes,
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
$context->calling_method_id
|
2020-03-12 16:05:42 +01:00
|
|
|
|
);
|
2020-02-13 22:38:58 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
// this can happen when the function was defined in the Config startup script
|
|
|
|
|
$stmt_type = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-10-12 19:46:43 +02:00
|
|
|
|
if (!$callmap_callable) {
|
|
|
|
|
throw new \UnexpectedValueException('We should have a callmap callable here');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$stmt_type = self::getReturnTypeFromCallMapWithArgs(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$function_id,
|
|
|
|
|
$stmt->args,
|
2020-10-12 19:46:43 +02:00
|
|
|
|
$callmap_callable,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$context
|
|
|
|
|
);
|
2020-05-22 04:47:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 21:52:19 +02:00
|
|
|
|
if (!$stmt_type) {
|
|
|
|
|
$stmt_type = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($function_storage) {
|
2020-06-21 05:11:42 +02:00
|
|
|
|
self::taintReturnType($statements_analyzer, $stmt, $function_id, $function_storage, $stmt_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $stmt_type;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 19:46:43 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
|
|
|
|
*/
|
|
|
|
|
private static function getReturnTypeFromCallMapWithArgs(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
string $function_id,
|
|
|
|
|
array $call_args,
|
|
|
|
|
TCallable $callmap_callable,
|
|
|
|
|
Context $context
|
|
|
|
|
): Type\Union {
|
|
|
|
|
$call_map_key = strtolower($function_id);
|
|
|
|
|
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
|
|
if (!$call_args) {
|
|
|
|
|
switch ($call_map_key) {
|
|
|
|
|
case 'hrtime':
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
new Type\Atomic\TKeyedArray([
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
Type::getInt()
|
|
|
|
|
])
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
case 'get_called_class':
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
new Type\Atomic\TClassString(
|
|
|
|
|
$context->self ?: 'object',
|
|
|
|
|
$context->self ? new Type\Atomic\TNamedObject($context->self, true) : null
|
|
|
|
|
)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
case 'get_parent_class':
|
|
|
|
|
if ($context->self && $codebase->classExists($context->self)) {
|
|
|
|
|
$classlike_storage = $codebase->classlike_storage_provider->get($context->self);
|
|
|
|
|
|
|
|
|
|
if ($classlike_storage->parent_classes) {
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
new Type\Atomic\TClassString(
|
2020-10-12 20:32:44 +02:00
|
|
|
|
\array_values($classlike_storage->parent_classes)[0]
|
2020-10-12 19:46:43 +02:00
|
|
|
|
)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
switch ($call_map_key) {
|
|
|
|
|
case 'count':
|
|
|
|
|
if (($first_arg_type = $statements_analyzer->node_data->getType($call_args[0]->value))) {
|
|
|
|
|
$atomic_types = $first_arg_type->getAtomicTypes();
|
|
|
|
|
|
|
|
|
|
if (count($atomic_types) === 1) {
|
|
|
|
|
if (isset($atomic_types['array'])) {
|
|
|
|
|
if ($atomic_types['array'] instanceof Type\Atomic\TCallableArray
|
|
|
|
|
|| $atomic_types['array'] instanceof Type\Atomic\TCallableList
|
|
|
|
|
|| $atomic_types['array'] instanceof Type\Atomic\TCallableKeyedArray
|
|
|
|
|
) {
|
|
|
|
|
return Type::getInt(false, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($atomic_types['array'] instanceof Type\Atomic\TNonEmptyArray) {
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
$atomic_types['array']->count !== null
|
|
|
|
|
? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
|
|
|
|
|
: new Type\Atomic\TInt
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($atomic_types['array'] instanceof Type\Atomic\TNonEmptyList) {
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
$atomic_types['array']->count !== null
|
|
|
|
|
? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
|
|
|
|
|
: new Type\Atomic\TInt
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($atomic_types['array'] instanceof Type\Atomic\TKeyedArray
|
|
|
|
|
&& $atomic_types['array']->sealed
|
|
|
|
|
) {
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
new Type\Atomic\TLiteralInt(count($atomic_types['array']->properties))
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'hrtime':
|
|
|
|
|
if (($first_arg_type = $statements_analyzer->node_data->getType($call_args[0]->value))) {
|
|
|
|
|
if ((string) $first_arg_type === 'true') {
|
|
|
|
|
$int = Type::getInt();
|
|
|
|
|
$int->from_calculation = true;
|
|
|
|
|
return $int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((string) $first_arg_type === 'false') {
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
new Type\Atomic\TKeyedArray([
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
Type::getInt()
|
|
|
|
|
])
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Type\Union([
|
|
|
|
|
new Type\Atomic\TKeyedArray([
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
Type::getInt()
|
|
|
|
|
]),
|
|
|
|
|
new Type\Atomic\TInt()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$int = Type::getInt();
|
|
|
|
|
$int->from_calculation = true;
|
|
|
|
|
return $int;
|
|
|
|
|
|
|
|
|
|
case 'min':
|
|
|
|
|
case 'max':
|
|
|
|
|
if (isset($call_args[0])) {
|
|
|
|
|
$first_arg = $call_args[0]->value;
|
|
|
|
|
|
|
|
|
|
if ($first_arg_type = $statements_analyzer->node_data->getType($first_arg)) {
|
|
|
|
|
if ($first_arg_type->hasArray()) {
|
|
|
|
|
/** @psalm-suppress PossiblyUndefinedStringArrayOffset */
|
|
|
|
|
$array_type = $first_arg_type->getAtomicTypes()['array'];
|
|
|
|
|
if ($array_type instanceof Type\Atomic\TKeyedArray) {
|
|
|
|
|
return $array_type->getGenericValueType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($array_type instanceof Type\Atomic\TArray) {
|
|
|
|
|
return clone $array_type->type_params[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($array_type instanceof Type\Atomic\TList) {
|
|
|
|
|
return clone $array_type->type_param;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($first_arg_type->hasScalarType()
|
|
|
|
|
&& ($second_arg = ($call_args[1]->value ?? null))
|
|
|
|
|
&& ($second_arg_type = $statements_analyzer->node_data->getType($second_arg))
|
|
|
|
|
&& $second_arg_type->hasScalarType()
|
|
|
|
|
) {
|
|
|
|
|
return Type::combineUnionTypes($first_arg_type, $second_arg_type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'get_parent_class':
|
|
|
|
|
// this is unreliable, as it's hard to know exactly what's wanted - attempted this in
|
|
|
|
|
// https://github.com/vimeo/psalm/commit/355ed831e1c69c96bbf9bf2654ef64786cbe9fd7
|
|
|
|
|
// but caused problems where it didn’t know exactly what level of child we
|
|
|
|
|
// were receiving.
|
|
|
|
|
//
|
|
|
|
|
// Really this should only work on instances we've created with new Foo(),
|
|
|
|
|
// but that requires more work
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'fgetcsv':
|
|
|
|
|
$string_type = Type::getString();
|
|
|
|
|
$string_type->addType(new Type\Atomic\TNull);
|
|
|
|
|
$string_type->ignore_nullable_issues = true;
|
|
|
|
|
|
|
|
|
|
$call_map_return_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TNonEmptyList(
|
|
|
|
|
$string_type
|
|
|
|
|
),
|
|
|
|
|
new Type\Atomic\TFalse,
|
|
|
|
|
new Type\Atomic\TNull
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($codebase->config->ignore_internal_nullable_issues) {
|
|
|
|
|
$call_map_return_type->ignore_nullable_issues = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($codebase->config->ignore_internal_falsable_issues) {
|
|
|
|
|
$call_map_return_type->ignore_falsable_issues = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $call_map_return_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$stmt_type = $callmap_callable->return_type
|
|
|
|
|
? clone $callmap_callable->return_type
|
|
|
|
|
: Type::getMixed();
|
|
|
|
|
|
|
|
|
|
switch ($function_id) {
|
|
|
|
|
case 'mb_strpos':
|
|
|
|
|
case 'mb_strrpos':
|
|
|
|
|
case 'mb_stripos':
|
|
|
|
|
case 'mb_strripos':
|
|
|
|
|
case 'strpos':
|
|
|
|
|
case 'strrpos':
|
|
|
|
|
case 'stripos':
|
|
|
|
|
case 'strripos':
|
|
|
|
|
case 'strstr':
|
|
|
|
|
case 'stristr':
|
|
|
|
|
case 'strrchr':
|
|
|
|
|
case 'strpbrk':
|
|
|
|
|
case 'array_search':
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if ($stmt_type->isFalsable()
|
|
|
|
|
&& $codebase->config->ignore_internal_falsable_issues
|
|
|
|
|
) {
|
|
|
|
|
$stmt_type->ignore_falsable_issues = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ($call_map_key) {
|
|
|
|
|
case 'array_replace':
|
|
|
|
|
case 'array_replace_recursive':
|
|
|
|
|
if ($codebase->config->ignore_internal_nullable_issues) {
|
|
|
|
|
$stmt_type->ignore_nullable_issues = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $stmt_type;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 05:11:42 +02:00
|
|
|
|
private static function taintReturnType(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
|
string $function_id,
|
|
|
|
|
FunctionLikeStorage $function_storage,
|
|
|
|
|
Type\Union $stmt_type
|
|
|
|
|
) : void {
|
2020-09-30 18:28:13 +02:00
|
|
|
|
if (!$statements_analyzer->control_flow_graph instanceof TaintFlowGraph
|
2020-07-02 05:23:38 +02:00
|
|
|
|
|| \in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
2020-05-26 05:28:11 +02:00
|
|
|
|
) {
|
2020-06-22 00:07:39 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-22 05:43:13 +02:00
|
|
|
|
|
2020-09-21 01:26:49 +02:00
|
|
|
|
$node_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
2020-03-12 16:05:42 +01:00
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$function_call_node = ControlFlowNode::getForMethodReturn(
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$function_id,
|
|
|
|
|
$function_id,
|
2020-09-21 01:26:49 +02:00
|
|
|
|
$function_storage->signature_return_type_location ?: $function_storage->location,
|
|
|
|
|
$function_storage->specialize_call ? $node_location : null
|
2020-06-22 00:07:39 +02:00
|
|
|
|
);
|
2020-05-22 04:47:58 +02:00
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$statements_analyzer->control_flow_graph->addNode($function_call_node);
|
2020-05-22 04:47:58 +02:00
|
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
|
$stmt_type->parent_nodes[$function_call_node->id] = $function_call_node;
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|
2020-06-22 00:07:39 +02:00
|
|
|
|
if ($function_storage->return_source_params) {
|
|
|
|
|
$removed_taints = $function_storage->removed_taints;
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|
2020-06-22 00:07:39 +02:00
|
|
|
|
if ($function_id === 'preg_replace' && count($stmt->args) > 2) {
|
|
|
|
|
$first_stmt_type = $statements_analyzer->node_data->getType($stmt->args[0]->value);
|
|
|
|
|
$second_stmt_type = $statements_analyzer->node_data->getType($stmt->args[1]->value);
|
|
|
|
|
|
|
|
|
|
if ($first_stmt_type
|
|
|
|
|
&& $second_stmt_type
|
|
|
|
|
&& $first_stmt_type->isSingleStringLiteral()
|
|
|
|
|
&& $second_stmt_type->isSingleStringLiteral()
|
|
|
|
|
) {
|
|
|
|
|
$first_arg_value = $first_stmt_type->getSingleStringLiteral()->value;
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$pattern = \substr($first_arg_value, 1, -1);
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|
2020-06-22 00:07:39 +02:00
|
|
|
|
if ($pattern[0] === '['
|
|
|
|
|
&& $pattern[1] === '^'
|
|
|
|
|
&& \substr($pattern, -1) === ']'
|
|
|
|
|
) {
|
|
|
|
|
$pattern = \substr($pattern, 2, -1);
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|
2020-06-22 00:07:39 +02:00
|
|
|
|
if (self::simpleExclusion($pattern, $first_arg_value[0])) {
|
|
|
|
|
$removed_taints[] = 'html';
|
|
|
|
|
$removed_taints[] = 'sql';
|
2020-06-21 05:11:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-22 00:07:39 +02:00
|
|
|
|
}
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|
2020-06-22 23:53:03 +02:00
|
|
|
|
foreach ($function_storage->return_source_params as $i => $path_type) {
|
2020-06-22 00:07:39 +02:00
|
|
|
|
if (!isset($stmt->args[$i])) {
|
|
|
|
|
continue;
|
2020-06-16 02:59:48 +02:00
|
|
|
|
}
|
2020-06-21 06:58:56 +02:00
|
|
|
|
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$arg_location = new CodeLocation(
|
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
|
$stmt->args[$i]->value
|
|
|
|
|
);
|
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$function_param_sink = ControlFlowNode::getForMethodArgument(
|
2020-06-21 06:58:56 +02:00
|
|
|
|
$function_id,
|
|
|
|
|
$function_id,
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$i,
|
|
|
|
|
$arg_location,
|
2020-09-21 01:26:49 +02:00
|
|
|
|
$function_storage->specialize_call ? $node_location : null
|
2020-06-21 06:58:56 +02:00
|
|
|
|
);
|
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$statements_analyzer->control_flow_graph->addNode($function_param_sink);
|
2020-06-21 06:58:56 +02:00
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$statements_analyzer->control_flow_graph->addPath(
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$function_param_sink,
|
2020-09-21 01:26:49 +02:00
|
|
|
|
$function_call_node,
|
2020-06-22 23:53:03 +02:00
|
|
|
|
$path_type,
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$function_storage->added_taints,
|
|
|
|
|
$removed_taints
|
|
|
|
|
);
|
2020-06-21 06:58:56 +02:00
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
|
}
|
2020-06-22 00:07:39 +02:00
|
|
|
|
|
|
|
|
|
if ($function_storage->taint_source_types) {
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$method_node = TaintSource::getForMethodReturn(
|
2020-06-22 00:07:39 +02:00
|
|
|
|
$function_id,
|
|
|
|
|
$function_id,
|
2020-09-21 01:26:49 +02:00
|
|
|
|
$node_location
|
2020-06-22 00:07:39 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$method_node->taints = $function_storage->taint_source_types;
|
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
|
$statements_analyzer->control_flow_graph->addSource($method_node);
|
2020-06-22 00:07:39 +02:00
|
|
|
|
}
|
2020-06-21 05:11:42 +02:00
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
|
|
2020-08-23 19:52:31 +02:00
|
|
|
|
/**
|
|
|
|
|
* @psalm-pure
|
|
|
|
|
*/
|
2020-06-21 05:11:42 +02:00
|
|
|
|
private static function simpleExclusion(string $pattern, string $escape_char) : bool
|
|
|
|
|
{
|
|
|
|
|
$str_length = \strlen($pattern);
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i < $str_length; $i++) {
|
|
|
|
|
$current = $pattern[$i];
|
|
|
|
|
$next = $pattern[$i + 1] ?? null;
|
|
|
|
|
|
|
|
|
|
if ($current === '\\') {
|
|
|
|
|
if ($next == null
|
|
|
|
|
|| $next === 'x'
|
|
|
|
|
|| $next === 'u'
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($next === '.'
|
|
|
|
|
|| $next === '('
|
|
|
|
|
|| $next === ')'
|
|
|
|
|
|| $next === '['
|
|
|
|
|
|| $next === ']'
|
|
|
|
|
|| $next === 's'
|
|
|
|
|
|| $next === 'w'
|
|
|
|
|
|| $next === $escape_char
|
|
|
|
|
) {
|
|
|
|
|
$i++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($next !== '-') {
|
|
|
|
|
if ($current === '_'
|
|
|
|
|
|| $current === '-'
|
|
|
|
|
|| $current === '|'
|
|
|
|
|
|| $current === ':'
|
|
|
|
|
|| $current === '#'
|
2020-07-02 07:11:46 +02:00
|
|
|
|
|| $current === '.'
|
2020-06-21 05:11:42 +02:00
|
|
|
|
|| $current === ' '
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($current === ']') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 23:04:01 +02:00
|
|
|
|
if (!isset($pattern[$i + 2])) {
|
2020-06-21 05:11:42 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($current === 'a' && $pattern[$i + 2] === 'z')
|
|
|
|
|
|| ($current === 'a' && $pattern[$i + 2] === 'Z')
|
|
|
|
|
|| ($current === 'A' && $pattern[$i + 2] === 'Z')
|
|
|
|
|
|| ($current === '0' && $pattern[$i + 2] === '9')
|
|
|
|
|
) {
|
|
|
|
|
$i += 2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
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,
|
|
|
|
|
?string $function_id,
|
|
|
|
|
bool $in_call_map,
|
|
|
|
|
?FunctionLikeStorage $function_storage,
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
$callmap_function_pure = $function_id && $in_call_map
|
2020-06-14 17:06:53 +02:00
|
|
|
|
? $codebase->functions->isCallMapFunctionPure(
|
|
|
|
|
$codebase,
|
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
|
$function_id,
|
|
|
|
|
$stmt->args,
|
|
|
|
|
$must_use
|
|
|
|
|
)
|
2020-03-12 16:05:42 +01:00
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if ((!$in_call_map
|
|
|
|
|
&& $function_storage
|
|
|
|
|
&& !$function_storage->pure)
|
|
|
|
|
|| ($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) {
|
|
|
|
|
$context->removeAllObjectVars();
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_id
|
|
|
|
|
&& (($function_storage
|
|
|
|
|
&& $function_storage->pure
|
|
|
|
|
&& !$function_storage->assertions
|
|
|
|
|
&& $must_use)
|
|
|
|
|
|| ($callmap_function_pure === true && $must_use))
|
|
|
|
|
&& $codebase->find_unused_variables
|
|
|
|
|
&& !$context->inside_conditional
|
|
|
|
|
&& !$context->inside_unset
|
|
|
|
|
) {
|
2020-09-30 18:28:13 +02:00
|
|
|
|
if (!$context->inside_assignment && !$context->inside_call && !$context->inside_use) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UnusedFunctionCall(
|
|
|
|
|
'The call to ' . $function_id . ' is not used',
|
|
|
|
|
new CodeLocation($statements_analyzer, $function_name),
|
|
|
|
|
$function_id
|
|
|
|
|
),
|
|
|
|
|
$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
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-14 23:10:30 +02:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
private static function handleNamedFunction(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $real_stmt,
|
|
|
|
|
PhpParser\Node\Name $function_name,
|
|
|
|
|
?string $function_id,
|
|
|
|
|
Context $context
|
|
|
|
|
) : void {
|
|
|
|
|
$first_arg = isset($stmt->args[0]) ? $stmt->args[0] : null;
|
|
|
|
|
|
2020-10-03 01:15:47 +02:00
|
|
|
|
if ($function_name->parts === ['get_class']
|
|
|
|
|
|| $function_name->parts === ['gettype']
|
|
|
|
|
|| $function_name->parts === ['get_debug_type']
|
|
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($first_arg) {
|
|
|
|
|
$var = $first_arg->value;
|
|
|
|
|
|
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
&& is_string($var->name)
|
|
|
|
|
) {
|
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
2020-10-03 01:15:47 +02:00
|
|
|
|
if ($function_name->parts === ['get_class']) {
|
|
|
|
|
$atomic_type = new Type\Atomic\TDependentGetClass(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$var_id,
|
|
|
|
|
$context->vars_in_scope[$var_id]->hasMixed()
|
|
|
|
|
? Type::getObject()
|
|
|
|
|
: $context->vars_in_scope[$var_id]
|
2020-10-03 01:15:47 +02:00
|
|
|
|
);
|
2020-10-03 02:33:35 +02:00
|
|
|
|
} elseif ($function_name->parts === ['gettype']) {
|
2020-10-03 01:15:47 +02:00
|
|
|
|
$atomic_type = new Type\Atomic\TDependentGetType($var_id);
|
|
|
|
|
} else {
|
|
|
|
|
$atomic_type = new Type\Atomic\TDependentGetDebugType($var_id);
|
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($real_stmt, new Type\Union([$atomic_type]));
|
|
|
|
|
}
|
2020-10-03 01:15:47 +02:00
|
|
|
|
} elseif (($var_type = $statements_analyzer->node_data->getType($var))
|
|
|
|
|
&& ($function_name->parts === ['get_class']
|
|
|
|
|
|| $function_name->parts === ['get_debug_type']
|
|
|
|
|
)
|
|
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$class_string_types = [];
|
|
|
|
|
|
|
|
|
|
foreach ($var_type->getAtomicTypes() as $class_type) {
|
|
|
|
|
if ($class_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TClassString($class_type->value, clone $class_type);
|
|
|
|
|
} elseif ($class_type instanceof Type\Atomic\TTemplateParam
|
|
|
|
|
&& $class_type->as->isSingle()
|
|
|
|
|
) {
|
|
|
|
|
$as_atomic_type = \array_values($class_type->as->getAtomicTypes())[0];
|
|
|
|
|
|
|
|
|
|
if ($as_atomic_type instanceof Type\Atomic\TObject) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TTemplateParamClass(
|
|
|
|
|
$class_type->param_name,
|
|
|
|
|
'object',
|
|
|
|
|
null,
|
|
|
|
|
$class_type->defining_class
|
|
|
|
|
);
|
|
|
|
|
} elseif ($as_atomic_type instanceof TNamedObject) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TTemplateParamClass(
|
|
|
|
|
$class_type->param_name,
|
|
|
|
|
$as_atomic_type->value,
|
|
|
|
|
$as_atomic_type,
|
|
|
|
|
$class_type->defining_class
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-10-03 01:15:47 +02:00
|
|
|
|
} elseif ($function_name->parts === ['get_class']) {
|
2020-07-16 19:56:42 +02:00
|
|
|
|
$class_string_types[] = new Type\Atomic\TClassString();
|
2020-10-03 01:15:47 +02:00
|
|
|
|
} elseif ($function_name->parts === ['get_debug_type']) {
|
|
|
|
|
if ($class_type instanceof Type\Atomic\TInt) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TLiteralString('int');
|
|
|
|
|
} elseif ($class_type instanceof Type\Atomic\TString) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TLiteralString('string');
|
|
|
|
|
} elseif ($class_type instanceof Type\Atomic\TFloat) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TLiteralString('float');
|
|
|
|
|
} elseif ($class_type instanceof Type\Atomic\TBool) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TLiteralString('bool');
|
|
|
|
|
} elseif ($class_type instanceof Type\Atomic\TClosedResource) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TLiteralString('resource (closed)');
|
|
|
|
|
} elseif ($class_type instanceof Type\Atomic\TNull) {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TLiteralString('null');
|
|
|
|
|
} else {
|
|
|
|
|
$class_string_types[] = new Type\Atomic\TString();
|
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($class_string_types) {
|
|
|
|
|
$statements_analyzer->node_data->setType($real_stmt, new Type\Union($class_string_types));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['get_class']
|
|
|
|
|
&& ($get_class_name = $statements_analyzer->getFQCLN())
|
2019-10-14 23:10:30 +02:00
|
|
|
|
) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
|
$real_stmt,
|
|
|
|
|
new Type\Union([
|
|
|
|
|
new Type\Atomic\TClassString(
|
|
|
|
|
$get_class_name,
|
|
|
|
|
new Type\Atomic\TNamedObject($get_class_name)
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($function_name->parts === ['method_exists']) {
|
|
|
|
|
$second_arg = isset($stmt->args[1]) ? $stmt->args[1] : null;
|
|
|
|
|
|
|
|
|
|
if ($first_arg
|
|
|
|
|
&& $first_arg->value instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
&& $second_arg
|
|
|
|
|
&& $second_arg->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
|
|
|
|
// do nothing
|
|
|
|
|
} else {
|
|
|
|
|
$context->check_methods = false;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['class_exists']) {
|
|
|
|
|
if ($first_arg) {
|
|
|
|
|
if ($first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
2020-03-17 20:04:50 +01:00
|
|
|
|
if (!$codebase->classlikes->classExists($first_arg->value->value)) {
|
|
|
|
|
$context->phantom_classes[strtolower($first_arg->value->value)] = true;
|
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
} elseif ($first_arg->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $first_arg->value->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $first_arg->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& $first_arg->value->name->name === 'class'
|
|
|
|
|
) {
|
|
|
|
|
$resolved_name = (string) $first_arg->value->class->getAttribute('resolvedName');
|
|
|
|
|
|
|
|
|
|
if (!$codebase->classlikes->classExists($resolved_name)) {
|
|
|
|
|
$context->phantom_classes[strtolower($resolved_name)] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['interface_exists']) {
|
|
|
|
|
if ($first_arg) {
|
|
|
|
|
if ($first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
$context->phantom_classes[strtolower($first_arg->value->value)] = true;
|
|
|
|
|
} elseif ($first_arg->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $first_arg->value->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $first_arg->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& $first_arg->value->name->name === 'class'
|
|
|
|
|
) {
|
|
|
|
|
$resolved_name = (string) $first_arg->value->class->getAttribute('resolvedName');
|
|
|
|
|
|
|
|
|
|
if (!$codebase->classlikes->interfaceExists($resolved_name)) {
|
|
|
|
|
$context->phantom_classes[strtolower($resolved_name)] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['file_exists'] && $first_arg) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId($first_arg->value, null);
|
2020-03-12 16:05:42 +01:00
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
|
$context->phantom_files[$var_id] = true;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['extension_loaded']) {
|
|
|
|
|
if ($first_arg
|
|
|
|
|
&& $first_arg->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
|
|
|
|
if (@extension_loaded($first_arg->value->value)) {
|
|
|
|
|
// do nothing
|
|
|
|
|
} else {
|
|
|
|
|
$context->check_classes = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['function_exists']) {
|
|
|
|
|
$context->check_functions = false;
|
|
|
|
|
} elseif ($function_name->parts === ['is_callable']) {
|
|
|
|
|
$context->check_methods = false;
|
|
|
|
|
$context->check_functions = false;
|
|
|
|
|
} elseif ($function_name->parts === ['defined']) {
|
|
|
|
|
$context->check_consts = false;
|
|
|
|
|
} elseif ($function_name->parts === ['extract']) {
|
|
|
|
|
$context->check_variables = false;
|
2020-09-28 06:45:02 +02:00
|
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var_id => $_) {
|
|
|
|
|
if ($var_id === '$this' || strpos($var_id, '[') || strpos($var_id, '>')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mixed_type = Type::getMixed();
|
|
|
|
|
$mixed_type->parent_nodes = $context->vars_in_scope[$var_id]->parent_nodes;
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $mixed_type;
|
|
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
|
|
|
|
$context->possibly_assigned_var_ids[$var_id] = true;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['compact']) {
|
|
|
|
|
$all_args_string_literals = true;
|
|
|
|
|
$new_items = [];
|
|
|
|
|
|
|
|
|
|
foreach ($stmt->args as $arg) {
|
|
|
|
|
$arg_type = $statements_analyzer->node_data->getType($arg->value);
|
|
|
|
|
|
|
|
|
|
if (!$arg_type || !$arg_type->isSingleStringLiteral()) {
|
|
|
|
|
$all_args_string_literals = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$var_name = $arg_type->getSingleStringLiteral()->value;
|
|
|
|
|
|
|
|
|
|
$new_items[] = new PhpParser\Node\Expr\ArrayItem(
|
|
|
|
|
new PhpParser\Node\Expr\Variable($var_name, $arg->value->getAttributes()),
|
|
|
|
|
new PhpParser\Node\Scalar\String_($var_name, $arg->value->getAttributes()),
|
|
|
|
|
false,
|
|
|
|
|
$arg->getAttributes()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($all_args_string_literals) {
|
|
|
|
|
$arr = new PhpParser\Node\Expr\Array_($new_items, $stmt->getAttributes());
|
|
|
|
|
$old_node_data = $statements_analyzer->node_data;
|
|
|
|
|
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
|
|
|
|
|
|
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $arr, $context);
|
|
|
|
|
|
|
|
|
|
$arr_type = $statements_analyzer->node_data->getType($arr);
|
|
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = $old_node_data;
|
|
|
|
|
|
|
|
|
|
if ($arr_type) {
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $arr_type);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-30 19:08:01 +02:00
|
|
|
|
} elseif ($function_name->parts === ['func_get_args']) {
|
|
|
|
|
$source = $statements_analyzer->getSource();
|
|
|
|
|
|
|
|
|
|
if ($statements_analyzer->control_flow_graph
|
|
|
|
|
&& $source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
if ($statements_analyzer->control_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph) {
|
|
|
|
|
foreach ($source->param_nodes as $param_node) {
|
|
|
|
|
$statements_analyzer->control_flow_graph->addPath(
|
|
|
|
|
$param_node,
|
|
|
|
|
new ControlFlowNode('variable-use', 'variable use', null),
|
|
|
|
|
'variable-use'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
} elseif (strtolower($function_name->parts[0]) === 'var_dump'
|
|
|
|
|
|| strtolower($function_name->parts[0]) === 'shell_exec') {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new ForbiddenCode(
|
|
|
|
|
'Unsafe ' . implode('', $function_name->parts),
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
} elseif (isset($codebase->config->forbidden_functions[strtolower((string) $function_name)])) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new ForbiddenCode(
|
|
|
|
|
'You have forbidden the use of ' . $function_name,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
} elseif ($function_name->parts === ['define']) {
|
|
|
|
|
if ($first_arg) {
|
2020-05-19 18:56:23 +02:00
|
|
|
|
$fq_const_name = ConstFetchAnalyzer::getConstName(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$first_arg->value,
|
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
|
$codebase,
|
|
|
|
|
$statements_analyzer->getAliases()
|
2019-10-14 23:10:30 +02:00
|
|
|
|
);
|
|
|
|
|
|
2020-04-28 20:43:12 +02:00
|
|
|
|
if ($fq_const_name !== null && isset($stmt->args[1])) {
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$second_arg = $stmt->args[1];
|
|
|
|
|
$was_in_call = $context->inside_call;
|
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $second_arg->value, $context);
|
|
|
|
|
$context->inside_call = $was_in_call;
|
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
|
ConstFetchAnalyzer::setConstType(
|
|
|
|
|
$statements_analyzer,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$fq_const_name,
|
|
|
|
|
$statements_analyzer->node_data->getType($second_arg->value) ?: Type::getMixed(),
|
|
|
|
|
$context
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$context->check_consts = false;
|
2019-10-14 23:10:30 +02:00
|
|
|
|
}
|
2020-03-12 16:05:42 +01:00
|
|
|
|
} elseif ($function_name->parts === ['constant']) {
|
|
|
|
|
if ($first_arg) {
|
2020-05-19 18:56:23 +02:00
|
|
|
|
$fq_const_name = ConstFetchAnalyzer::getConstName(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$first_arg->value,
|
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
|
$codebase,
|
|
|
|
|
$statements_analyzer->getAliases()
|
|
|
|
|
);
|
2019-10-14 23:10:30 +02:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($fq_const_name !== null) {
|
2020-05-19 18:56:23 +02:00
|
|
|
|
$const_type = ConstFetchAnalyzer::getConstType(
|
|
|
|
|
$statements_analyzer,
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$fq_const_name,
|
|
|
|
|
true,
|
|
|
|
|
$context
|
|
|
|
|
);
|
2019-11-14 20:44:29 +01:00
|
|
|
|
|
2020-03-12 16:05:42 +01:00
|
|
|
|
if ($const_type) {
|
|
|
|
|
$statements_analyzer->node_data->setType($real_stmt, $const_type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$context->check_consts = false;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($first_arg
|
|
|
|
|
&& $function_id
|
|
|
|
|
&& strpos($function_id, 'is_') === 0
|
|
|
|
|
&& $function_id !== 'is_a'
|
|
|
|
|
) {
|
|
|
|
|
$stmt_assertions = $statements_analyzer->node_data->getAssertions($stmt);
|
|
|
|
|
|
|
|
|
|
if ($stmt_assertions !== null) {
|
|
|
|
|
$assertions = $stmt_assertions;
|
|
|
|
|
} else {
|
|
|
|
|
$assertions = AssertionFinder::processFunctionCall(
|
|
|
|
|
$stmt,
|
|
|
|
|
$context->self,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$codebase,
|
|
|
|
|
$context->inside_negation
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$changed_vars = [];
|
|
|
|
|
|
|
|
|
|
$referenced_var_ids = array_map(
|
|
|
|
|
function (array $_) : bool {
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
$assertions
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($assertions) {
|
|
|
|
|
Reconciler::reconcileKeyedTypes(
|
|
|
|
|
$assertions,
|
|
|
|
|
$assertions,
|
|
|
|
|
$context->vars_in_scope,
|
|
|
|
|
$changed_vars,
|
|
|
|
|
$referenced_var_ids,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
[],
|
|
|
|
|
$context->inside_loop,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} elseif ($first_arg && $function_id === 'strtolower') {
|
|
|
|
|
$first_arg_type = $statements_analyzer->node_data->getType($first_arg->value);
|
|
|
|
|
|
|
|
|
|
if ($first_arg_type
|
2020-07-22 01:40:35 +02:00
|
|
|
|
&& UnionTypeComparator::isContainedBy(
|
2020-03-12 16:05:42 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$first_arg_type,
|
|
|
|
|
new Type\Union([new Type\Atomic\TLowercaseString()])
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
if ($first_arg_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new \Psalm\Issue\RedundantConditionGivenDocblockType(
|
|
|
|
|
'The call to strtolower is unnecessary given the docblock type',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($statements_analyzer, $function_name),
|
|
|
|
|
null
|
2020-03-12 16:05:42 +01:00
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new \Psalm\Issue\RedundantCondition(
|
|
|
|
|
'The call to strtolower is unnecessary',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($statements_analyzer, $function_name),
|
|
|
|
|
null
|
2020-03-12 16:05:42 +01:00
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|