2018-01-29 00:29:38 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression\Call;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\FunctionChecker;
|
|
|
|
use Psalm\Checker\FunctionLikeChecker;
|
|
|
|
use Psalm\Checker\ProjectChecker;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
2018-02-04 00:52:35 +01:00
|
|
|
use Psalm\Codebase\CallMap;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2018-06-01 03:59:55 +02:00
|
|
|
use Psalm\FileManipulation\FileManipulationBuffer;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
|
|
|
use Psalm\Issue\InvalidFunctionCall;
|
|
|
|
use Psalm\Issue\NullFunctionCall;
|
|
|
|
use Psalm\Issue\PossiblyInvalidFunctionCall;
|
|
|
|
use Psalm\Issue\PossiblyNullFunctionCall;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\Type\Atomic\TCallable;
|
2018-04-20 16:52:23 +02:00
|
|
|
use Psalm\Type\Atomic\TGenericParam;
|
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;
|
|
|
|
|
|
|
|
class FunctionCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\FuncCall $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyze(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-02-21 17:32:52 +01:00
|
|
|
$function = $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;
|
|
|
|
|
|
|
|
$code_location = new CodeLocation($statements_checker->getSource(), $stmt);
|
|
|
|
$codebase = $project_checker->codebase;
|
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;
|
|
|
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Expr) {
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->name, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->name->inferredType)) {
|
|
|
|
if ($stmt->name->inferredType->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullFunctionCall(
|
|
|
|
'Cannot call function on null value',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->name->inferredType->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullFunctionCall(
|
|
|
|
'Cannot call function on possibly null value',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_function_call_types = [];
|
|
|
|
$has_valid_function_call_type = false;
|
|
|
|
|
|
|
|
foreach ($stmt->name->inferredType->getTypes() as $var_type_part) {
|
2018-03-27 06:12:41 +02:00
|
|
|
if ($var_type_part instanceof Type\Atomic\Fn || $var_type_part instanceof Type\Atomic\TCallable) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$function_params = $var_type_part->params;
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
if (isset($stmt->inferredType) && $var_type_part->return_type) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->inferredType = Type::combineUnionTypes(
|
|
|
|
$stmt->inferredType,
|
|
|
|
$var_type_part->return_type
|
|
|
|
);
|
|
|
|
} else {
|
2018-03-27 04:13:10 +02:00
|
|
|
$stmt->inferredType = $var_type_part->return_type ?: Type::getMixed();
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$function_exists = true;
|
|
|
|
$has_valid_function_call_type = true;
|
2018-04-20 16:52:23 +02:00
|
|
|
} elseif ($var_type_part instanceof TMixed || $var_type_part instanceof TGenericParam) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$has_valid_function_call_type = true;
|
2018-04-20 16:52:23 +02:00
|
|
|
// @todo maybe emit issue here
|
2018-03-27 07:05:11 +02:00
|
|
|
} elseif (($var_type_part instanceof TNamedObject && $var_type_part->value === 'Closure')) {
|
2018-02-12 02:56:34 +01:00
|
|
|
// this is fine
|
|
|
|
$has_valid_function_call_type = true;
|
2018-03-02 06:49:53 +01:00
|
|
|
} elseif ($var_type_part instanceof TString
|
2018-03-02 14:38:16 +01:00
|
|
|
|| $var_type_part instanceof Type\Atomic\TArray
|
2018-03-02 06:49:53 +01:00
|
|
|
|| ($var_type_part instanceof Type\Atomic\ObjectLike
|
|
|
|
&& count($var_type_part->properties) === 2)
|
|
|
|
) {
|
2018-02-12 02:56:34 +01:00
|
|
|
// this is also kind of fine
|
2018-01-29 00:29:38 +01:00
|
|
|
$has_valid_function_call_type = true;
|
|
|
|
} elseif ($var_type_part instanceof TNull) {
|
|
|
|
// handled above
|
2018-02-01 06:50:01 +01:00
|
|
|
} elseif (!$var_type_part instanceof TNamedObject
|
2018-02-04 00:52:35 +01:00
|
|
|
|| !$codebase->classlikes->classOrInterfaceExists($var_type_part->value)
|
|
|
|
|| !$codebase->methods->methodExists($var_type_part->value . '::__invoke')
|
2018-01-29 00:29:38 +01:00
|
|
|
) {
|
|
|
|
$invalid_function_call_types[] = (string)$var_type_part;
|
2018-04-01 00:57:13 +02:00
|
|
|
} else {
|
|
|
|
if (self::checkMethodArgs(
|
|
|
|
$var_type_part->value . '::__invoke',
|
|
|
|
$stmt->args,
|
|
|
|
$class_template_params,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-09 01:49:25 +02:00
|
|
|
|
|
|
|
$invokable_return_type = $codebase->methods->getMethodReturnType(
|
|
|
|
$var_type_part->value . '::__invoke',
|
|
|
|
$var_type_part->value
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes(
|
|
|
|
$invokable_return_type ?: Type::getMixed(),
|
|
|
|
$stmt->inferredType
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = $invokable_return_type ?: Type::getMixed();
|
|
|
|
}
|
2018-01-29 00:29:38 +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_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidFunctionCall(
|
|
|
|
'Cannot treat type ' . $var_type_part . ' as callable',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id = implode('\\', $stmt->name->parts);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-02-21 17:32:52 +01:00
|
|
|
$in_call_map = CallMap::inCallMap($function_id);
|
|
|
|
$is_stubbed = $codebase_functions->hasStubbedFunction($function_id);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
$is_predefined = true;
|
|
|
|
|
2018-02-25 17:13:00 +01:00
|
|
|
$is_maybe_root_function = !$stmt->name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
&& count($stmt->name->parts) === 1;
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if (!$in_call_map) {
|
|
|
|
$predefined_functions = $config->getPredefinedFunctions();
|
2018-02-21 17:32:52 +01:00
|
|
|
$is_predefined = isset($predefined_functions[$function_id]);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$in_call_map && !$stmt->name instanceof PhpParser\Node\Name\FullyQualified) {
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id = $codebase_functions->getFullyQualifiedFunctionNameFromString(
|
|
|
|
$function_id,
|
2018-02-04 00:52:35 +01:00
|
|
|
$statements_checker
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$in_call_map) {
|
|
|
|
if ($context->check_functions) {
|
|
|
|
if (self::checkFunctionExists(
|
|
|
|
$statements_checker,
|
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
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-21 17:32:52 +01:00
|
|
|
} else {
|
2018-02-25 17:13:00 +01:00
|
|
|
$function_id = self::getExistingFunctionId(
|
|
|
|
$statements_checker,
|
|
|
|
$function_id,
|
|
|
|
$is_maybe_root_function
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$function_exists = $is_stubbed || $codebase_functions->functionExists(
|
2018-01-29 00:29:38 +01:00
|
|
|
$statements_checker,
|
2018-02-21 17:32:52 +01:00
|
|
|
strtolower($function_id)
|
2018-01-29 00:29:38 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$function_exists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_exists) {
|
|
|
|
if (!$in_call_map || $is_stubbed) {
|
2018-02-04 00:52:35 +01:00
|
|
|
$function_storage = $codebase_functions->getStorage(
|
2018-01-29 00:29:38 +01:00
|
|
|
$statements_checker,
|
2018-02-21 17:32:52 +01:00
|
|
|
strtolower($function_id)
|
2018-01-29 00:29:38 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$function_params = $function_storage->params;
|
|
|
|
|
|
|
|
if (!$is_predefined) {
|
|
|
|
$defined_constants = $function_storage->defined_constants;
|
2018-05-31 04:09:46 +02:00
|
|
|
$global_variables = $function_storage->global_variables;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($in_call_map && !$is_stubbed) {
|
|
|
|
$function_params = FunctionLikeChecker::getFunctionParamsFromCallMapById(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::checkFunctionArguments(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$function_params,
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_exists) {
|
|
|
|
$generic_params = null;
|
|
|
|
|
2018-02-21 17:32:52 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $function_id) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (!$is_stubbed && $in_call_map) {
|
|
|
|
$function_params = FunctionLikeChecker::getFunctionParamsFromCallMapById(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do this here to allow closure param checks
|
2018-04-10 00:49:11 +02:00
|
|
|
if ($function_params !== null
|
|
|
|
&& self::checkFunctionLikeArgumentsMatch(
|
2018-04-10 03:50:38 +02:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->args,
|
|
|
|
$function_id,
|
|
|
|
$function_params,
|
|
|
|
$function_storage,
|
|
|
|
null,
|
|
|
|
$generic_params,
|
|
|
|
$code_location,
|
|
|
|
$context
|
|
|
|
) === false) {
|
2018-01-29 00:29:38 +01:00
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
2018-02-21 17:32:52 +01:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $function_id) {
|
2018-01-29 00:29:38 +01:00
|
|
|
if (!$in_call_map || $is_stubbed) {
|
|
|
|
if ($function_storage && $function_storage->template_types) {
|
|
|
|
foreach ($function_storage->template_types as $template_name => $_) {
|
|
|
|
if (!isset($generic_params[$template_name])) {
|
|
|
|
$generic_params[$template_name] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
if ($function_storage && $context->collect_exceptions) {
|
|
|
|
$context->possibly_thrown_exceptions += $function_storage->throws;
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
try {
|
|
|
|
if ($function_storage && $function_storage->return_type) {
|
|
|
|
$return_type = clone $function_storage->return_type;
|
|
|
|
|
|
|
|
if ($generic_params && $function_storage->template_types) {
|
|
|
|
$return_type->replaceTemplateTypesWithArgTypes(
|
|
|
|
$generic_params
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_location = $function_storage->return_type_location;
|
|
|
|
|
2018-06-01 03:59:55 +02:00
|
|
|
if ($config->after_function_checks) {
|
|
|
|
$file_manipulations = [];
|
|
|
|
|
|
|
|
foreach ($config->after_function_checks as $plugin_fq_class_name) {
|
|
|
|
$plugin_fq_class_name::afterFunctionCallCheck(
|
|
|
|
$statements_checker,
|
|
|
|
$function_id,
|
|
|
|
$stmt->args,
|
|
|
|
$return_type_location,
|
|
|
|
$context,
|
|
|
|
$file_manipulations,
|
|
|
|
$return_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
|
|
|
FileManipulationBuffer::add(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->inferredType = $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_checker,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
2018-08-10 05:29:30 +02:00
|
|
|
$context->phantom_classes
|
2018-01-29 00:29:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// this can happen when the function was defined in the Config startup script
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = FunctionChecker::getReturnTypeFromCallMapWithArgs(
|
|
|
|
$statements_checker,
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->args,
|
2018-09-26 00:37:24 +02:00
|
|
|
$context,
|
2018-01-29 00:29:38 +01:00
|
|
|
$code_location,
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if ($config->use_assert_for_type &&
|
2018-02-21 17:32:52 +01:00
|
|
|
$function instanceof PhpParser\Node\Name &&
|
|
|
|
$function->parts === ['assert'] &&
|
2018-01-29 00:29:38 +01:00
|
|
|
isset($stmt->args[0])
|
|
|
|
) {
|
2018-05-07 07:26:06 +02:00
|
|
|
$assert_clauses = \Psalm\Type\Algebra::getFormula(
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->args[0]->value,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$simplified_clauses = Algebra::simplifyCNF(array_merge($context->clauses, $assert_clauses));
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$assert_type_assertions = Algebra::getTruthsFromFormula($simplified_clauses);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
|
|
|
$changed_vars = [];
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$changed_vars,
|
|
|
|
[],
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($changed_vars as $changed_var) {
|
|
|
|
if (isset($op_vars_in_scope[$changed_var])) {
|
|
|
|
$op_vars_in_scope[$changed_var]->from_docblock = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:24:59 +02:00
|
|
|
if (!$config->remember_property_assignments_after_call
|
|
|
|
&& !$in_call_map
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$context->removeAllObjectVars();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
($stmt->name->parts === ['get_class'] || $stmt->name->parts === ['gettype']) &&
|
|
|
|
$stmt->args
|
|
|
|
) {
|
|
|
|
$var = $stmt->args[0]->value;
|
|
|
|
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
|
|
|
|
$atomic_type = $stmt->name->parts === ['get_class']
|
|
|
|
? new Type\Atomic\GetClassT('$' . $var->name)
|
|
|
|
: new Type\Atomic\GetTypeT('$' . $var->name);
|
|
|
|
|
|
|
|
$stmt->inferredType = new Type\Union([$atomic_type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
if ($function_storage) {
|
|
|
|
if ($function_storage->assertions) {
|
|
|
|
self::applyAssertionsToContext(
|
|
|
|
$function_storage->assertions,
|
|
|
|
$stmt->args,
|
|
|
|
$context,
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
if ($function_storage->if_true_assertions) {
|
|
|
|
$stmt->ifTrueAssertions = $function_storage->if_true_assertions;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_storage->if_false_assertions) {
|
|
|
|
$stmt->ifFalseAssertions = $function_storage->if_false_assertions;
|
2018-05-28 21:07:42 +02:00
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
}
|
|
|
|
|
2018-04-07 21:16:46 +02:00
|
|
|
if ($function instanceof PhpParser\Node\Name) {
|
|
|
|
$first_arg = isset($stmt->args[0]) ? $stmt->args[0] : null;
|
|
|
|
|
|
|
|
if ($function->parts === ['method_exists']) {
|
|
|
|
$context->check_methods = false;
|
|
|
|
} elseif ($function->parts === ['class_exists']) {
|
|
|
|
if ($first_arg && $first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
2018-08-10 05:29:30 +02:00
|
|
|
$context->phantom_classes[strtolower($first_arg->value->value)] = true;
|
2018-04-07 21:16:46 +02:00
|
|
|
} else {
|
|
|
|
$context->check_classes = false;
|
|
|
|
}
|
2018-08-10 05:29:30 +02:00
|
|
|
} elseif ($function->parts === ['file_exists'] && $first_arg) {
|
|
|
|
$var_id = ExpressionChecker::getArrayVarId($first_arg->value, null);
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->phantom_files[$var_id] = true;
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} elseif ($function->parts === ['extension_loaded']) {
|
|
|
|
$context->check_classes = false;
|
|
|
|
} elseif ($function->parts === ['function_exists']) {
|
|
|
|
$context->check_functions = false;
|
|
|
|
} elseif ($function->parts === ['is_callable']) {
|
|
|
|
$context->check_methods = false;
|
|
|
|
$context->check_functions = false;
|
|
|
|
} elseif ($function->parts === ['defined']) {
|
|
|
|
$context->check_consts = false;
|
|
|
|
} elseif ($function->parts === ['extract']) {
|
|
|
|
$context->check_variables = false;
|
|
|
|
} elseif ($function->parts === ['var_dump'] || $function->parts === ['shell_exec']) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenCode(
|
|
|
|
'Unsafe ' . implode('', $function->parts),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-18 22:42:51 +02:00
|
|
|
} elseif (isset($codebase->config->forbidden_functions[strtolower((string) $function)])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenCode(
|
|
|
|
'You have forbidden the use of ' . $function,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} elseif ($function->parts === ['define']) {
|
|
|
|
if ($first_arg && $first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$second_arg = $stmt->args[1];
|
|
|
|
ExpressionChecker::analyze($statements_checker, $second_arg->value, $context);
|
|
|
|
$const_name = $first_arg->value->value;
|
|
|
|
|
|
|
|
$statements_checker->setConstType(
|
|
|
|
$const_name,
|
|
|
|
isset($second_arg->value->inferredType) ? $second_arg->value->inferredType : Type::getMixed(),
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$context->check_consts = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|