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;
|
2019-02-26 07:03:33 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\AssertionFinder;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2018-11-12 16:46:55 +01:00
|
|
|
use Psalm\Internal\Codebase\CallMap;
|
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;
|
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;
|
2018-01-29 00:29:38 +01:00
|
|
|
use Psalm\Type;
|
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;
|
|
|
|
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;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class FunctionCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-29 00:29:38 +01:00
|
|
|
* @param PhpParser\Node\Expr\FuncCall $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
) {
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Expr) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context) === false) {
|
2019-06-16 00:15:04 +02:00
|
|
|
return;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->name->inferredType)) {
|
|
|
|
if ($stmt->name->inferredType->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullFunctionCall(
|
|
|
|
'Cannot call function on null value',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->name->inferredType->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullFunctionCall(
|
|
|
|
'Cannot call function on possibly null value',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_function_call_types = [];
|
|
|
|
$has_valid_function_call_type = false;
|
|
|
|
|
|
|
|
foreach ($stmt->name->inferredType->getTypes() as $var_type_part) {
|
2019-06-03 12:19:52 +02:00
|
|
|
if ($var_type_part instanceof Type\Atomic\TFn || $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;
|
2019-02-22 03:40:06 +01:00
|
|
|
} elseif ($var_type_part instanceof TMixed || $var_type_part instanceof TTemplateParam) {
|
2018-01-29 00:29:38 +01:00
|
|
|
$has_valid_function_call_type = true;
|
2019-02-10 22:15:52 +01:00
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedFunctionCall(
|
|
|
|
'Cannot call function on mixed',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2019-01-04 14:04:19 +01:00
|
|
|
} elseif ($var_type_part instanceof TCallableObject
|
|
|
|
|| $var_type_part instanceof TCallableString
|
|
|
|
) {
|
|
|
|
// this is fine
|
|
|
|
$has_valid_function_call_type = true;
|
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,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer
|
2018-04-01 00:57:13 +02:00
|
|
|
) === false) {
|
2019-06-16 00:15:04 +02:00
|
|
|
return;
|
2018-04-01 00:57:13 +02:00
|
|
|
}
|
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',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
2019-06-16 00:15:04 +02:00
|
|
|
return;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidFunctionCall(
|
|
|
|
'Cannot treat type ' . $var_type_part . ' as callable',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-01-29 00:29:38 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-29 00:29:38 +01:00
|
|
|
)) {
|
2019-06-16 00:15:04 +02:00
|
|
|
return;
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-05 20:50:11 +01:00
|
|
|
$original_function_id = implode('\\', $stmt->name->parts);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-05 20:50:11 +01:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$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
|
|
|
|
&& !$stmt->name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
) {
|
|
|
|
$in_call_map = CallMap::inCallMap($original_function_id);
|
|
|
|
$is_stubbed = $codebase_functions->hasStubbedFunction($original_function_id);
|
|
|
|
|
|
|
|
if ($is_stubbed || $in_call_map) {
|
|
|
|
$function_id = $original_function_id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$in_call_map = CallMap::inCallMap($function_id);
|
|
|
|
$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
|
|
|
|
&& !$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;
|
|
|
|
|
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();
|
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
|
|
|
|
) {
|
2019-06-15 22:14:36 +02:00
|
|
|
return;
|
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) {
|
|
|
|
$function_storage = $codebase_functions->getStorage(
|
|
|
|
$statements_analyzer,
|
|
|
|
strtolower($function_id)
|
|
|
|
);
|
|
|
|
|
|
|
|
$function_params = $function_storage->params;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if (!$is_predefined) {
|
|
|
|
$defined_constants = $function_storage->defined_constants;
|
|
|
|
$global_variables = $function_storage->global_variables;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($in_call_map && !$is_stubbed) {
|
2019-06-15 22:10:48 +02:00
|
|
|
$function_callable = \Psalm\Internal\Codebase\CallMap::getCallableFromCallMapById(
|
2019-03-01 14:57:10 +01:00
|
|
|
$codebase,
|
|
|
|
$function_id,
|
|
|
|
$stmt->args
|
|
|
|
);
|
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(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt->name,
|
|
|
|
$function_id . '()'
|
|
|
|
);
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 04:38:30 +01:00
|
|
|
$set_inside_conditional = false;
|
|
|
|
|
|
|
|
if ($function instanceof PhpParser\Node\Name
|
|
|
|
&& $function->parts === ['assert']
|
|
|
|
&& !$context->inside_conditional
|
|
|
|
) {
|
|
|
|
$context->inside_conditional = true;
|
|
|
|
$set_inside_conditional = true;
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
if (self::checkFunctionArguments(
|
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,
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-16 00:50:08 +01:00
|
|
|
$generic_params = null;
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-11-16 00:50:08 +01:00
|
|
|
if ($function_exists) {
|
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) {
|
2019-06-15 22:10:48 +02:00
|
|
|
$function_callable = \Psalm\Internal\Codebase\CallMap::getCallableFromCallMapById(
|
2018-11-11 18:19:53 +01:00
|
|
|
$codebase,
|
2018-02-21 17:32:52 +01:00
|
|
|
$function_id,
|
2018-01-29 00:29:38 +01:00
|
|
|
$stmt->args
|
|
|
|
);
|
2019-06-15 22:10:48 +02:00
|
|
|
|
|
|
|
$function_params = $function_callable->params;
|
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
|
|
|
|
&& self::checkFunctionLikeArgumentsMatch(
|
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,
|
|
|
|
$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) {
|
2019-02-27 23:06:44 +01:00
|
|
|
$stmt->inferredType = null;
|
|
|
|
|
2019-02-16 00:00:40 +01:00
|
|
|
if ($codebase->functions->return_type_provider->has($function_id)) {
|
|
|
|
$stmt->inferredType = $codebase->functions->return_type_provider->getReturnType(
|
|
|
|
$statements_analyzer,
|
|
|
|
$function_id,
|
|
|
|
$stmt->args,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->name)
|
|
|
|
);
|
2019-02-27 23:06:44 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
if (!$stmt->inferredType) {
|
|
|
|
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])) {
|
2019-03-22 20:59:10 +01:00
|
|
|
$generic_params[$template_name] = ['' => [Type::getMixed(), 0]];
|
2019-02-27 23:06:44 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-02-27 23:06:44 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-03-29 00:50:29 +01:00
|
|
|
if ($function_storage && !$context->isSuppressingExceptions($statements_analyzer)) {
|
2019-03-29 00:43:14 +01:00
|
|
|
$context->mergeFunctionExceptions(
|
|
|
|
$function_storage,
|
2019-03-24 21:08:05 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
2019-02-27 23:06:44 +01:00
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
try {
|
|
|
|
if ($function_storage && $function_storage->return_type) {
|
|
|
|
$return_type = clone $function_storage->return_type;
|
2018-06-01 03:59:55 +02:00
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
if ($generic_params && $function_storage->template_types) {
|
|
|
|
$return_type->replaceTemplateTypesWithArgTypes(
|
|
|
|
$generic_params
|
2018-06-01 03:59:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
$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,
|
|
|
|
$file_manipulations,
|
|
|
|
$return_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
FileManipulationBuffer::add(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
2018-06-01 03:59:55 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 23:06:44 +01:00
|
|
|
/** @var Type\Union $return_type */
|
|
|
|
$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_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
|
|
|
$context->phantom_classes
|
|
|
|
);
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-02-27 23:06:44 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// this can happen when the function was defined in the Config startup script
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
2019-02-27 23:06:44 +01:00
|
|
|
} else {
|
|
|
|
$stmt->inferredType = FunctionAnalyzer::getReturnTypeFromCallMapWithArgs(
|
|
|
|
$statements_analyzer,
|
|
|
|
$function_id,
|
|
|
|
$stmt->args,
|
|
|
|
$context
|
|
|
|
);
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
|
|
if ($codebase->taint) {
|
|
|
|
FunctionAnalyzer::taintBuiltinFunctionReturn(
|
|
|
|
$statements_analyzer,
|
|
|
|
$function_id,
|
|
|
|
$stmt->args,
|
|
|
|
$stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer,
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase
|
2018-01-29 00:29:38 +01:00
|
|
|
);
|
|
|
|
|
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
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
if ($assert_type_assertions) {
|
|
|
|
$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_analyzer,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2018-12-17 21:23:56 +01:00
|
|
|
$context->inside_loop,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
foreach ($changed_vars as $changed_var) {
|
|
|
|
if (isset($op_vars_in_scope[$changed_var])) {
|
|
|
|
$op_vars_in_scope[$changed_var]->from_docblock = true;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
$context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
}
|
2018-01-29 00:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2019-01-05 16:32:39 +01:00
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& is_string($var->name)
|
|
|
|
) {
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$atomic_type = $stmt->name->parts === ['get_class']
|
|
|
|
? new Type\Atomic\GetClassT($var_id, $context->vars_in_scope[$var_id])
|
|
|
|
: new Type\Atomic\GetTypeT($var_id);
|
2018-01-29 00:29:38 +01:00
|
|
|
|
2019-01-05 16:32:39 +01:00
|
|
|
$stmt->inferredType = new Type\Union([$atomic_type]);
|
|
|
|
}
|
2019-03-03 00:02:11 +01:00
|
|
|
} elseif (isset($var->inferredType)) {
|
|
|
|
$class_string_types = [];
|
|
|
|
|
|
|
|
foreach ($var->inferredType->getTypes() as $class_type) {
|
|
|
|
if ($class_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
$class_string_types[] = new Type\Atomic\TClassString($class_type->value, clone $class_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_string_types) {
|
|
|
|
$stmt->inferredType = new Type\Union($class_string_types);
|
|
|
|
}
|
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
|
2018-10-26 22:17:15 +02:00
|
|
|
&& isset($stmt->inferredType)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
if ($context->pure || $codebase->find_unused_variables) {
|
|
|
|
$callmap_function_pure = $function_id && $in_call_map
|
|
|
|
? $codebase->functions->isCallMapFunctionPure($codebase, $function_id, $stmt->args)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (($function_storage
|
|
|
|
&& !$function_storage->pure)
|
|
|
|
|| ($callmap_function_pure === false)
|
|
|
|
) {
|
|
|
|
if ($context->pure) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpureFunctionCall(
|
|
|
|
'Cannot call an impure function from a pure context',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->name)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($function_id
|
|
|
|
&& (($function_storage && $function_storage->pure) || $callmap_function_pure === true)
|
|
|
|
&& $codebase->find_unused_variables
|
|
|
|
&& !$context->inside_conditional
|
|
|
|
&& !$context->inside_unset
|
|
|
|
) {
|
2019-08-16 05:28:21 +02:00
|
|
|
if (!$context->inside_assignment && !$context->inside_call) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnusedFunctionCall(
|
|
|
|
'The call to ' . $function_id . ' is not used',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->name),
|
|
|
|
$function_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-17 05:33:28 +02:00
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
2019-08-16 05:28:21 +02:00
|
|
|
$stmt->pure = true;
|
2019-07-18 07:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
if ($function_storage) {
|
2018-11-14 18:25:17 +01:00
|
|
|
if ($function_storage->assertions && $stmt->name instanceof PhpParser\Node\Name) {
|
2018-05-28 21:07:42 +02:00
|
|
|
self::applyAssertionsToContext(
|
2018-11-14 18:25:17 +01:00
|
|
|
$stmt->name,
|
2018-05-28 21:07:42 +02:00
|
|
|
$function_storage->assertions,
|
|
|
|
$stmt->args,
|
2018-11-16 00:50:08 +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-01-23 05:42:54 +01:00
|
|
|
$stmt->ifTrueAssertions = array_map(
|
|
|
|
function (Assertion $assertion) use ($generic_params) : Assertion {
|
|
|
|
return $assertion->getUntemplatedCopy($generic_params ?: []);
|
|
|
|
},
|
|
|
|
$function_storage->if_true_assertions
|
|
|
|
);
|
2018-07-11 17:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_storage->if_false_assertions) {
|
2019-01-23 05:42:54 +01:00
|
|
|
$stmt->ifFalseAssertions = array_map(
|
|
|
|
function (Assertion $assertion) use ($generic_params) : Assertion {
|
|
|
|
return $assertion->getUntemplatedCopy($generic_params ?: []);
|
|
|
|
},
|
|
|
|
$function_storage->if_false_assertions
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
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']) {
|
2019-08-19 04:27:19 +02:00
|
|
|
$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;
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} elseif ($function->parts === ['class_exists']) {
|
2019-06-17 17:31:43 +02:00
|
|
|
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');
|
2019-06-27 16:17:11 +02:00
|
|
|
|
|
|
|
if (!$codebase->classlikes->classExists($resolved_name)) {
|
|
|
|
$context->phantom_classes[strtolower($resolved_name)] = true;
|
2019-06-28 16:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($function->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;
|
2019-06-27 16:17:11 +02:00
|
|
|
}
|
2019-06-17 17:31:43 +02:00
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
}
|
2018-08-10 05:29:30 +02:00
|
|
|
} elseif ($function->parts === ['file_exists'] && $first_arg) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::getArrayVarId($first_arg->value, null);
|
2018-08-10 05:29:30 +02:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->phantom_files[$var_id] = true;
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} elseif ($function->parts === ['extension_loaded']) {
|
2018-10-04 22:58:11 +02:00
|
|
|
if ($first_arg
|
|
|
|
&& $first_arg->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
) {
|
2018-10-08 19:08:03 +02:00
|
|
|
if (@extension_loaded($first_arg->value->value)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$context->check_classes = false;
|
|
|
|
}
|
2018-10-04 22:58:11 +02:00
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} 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;
|
2019-04-13 17:31:22 +02:00
|
|
|
} elseif (strtolower($function->parts[0]) === 'var_dump'
|
|
|
|
|| strtolower($function->parts[0]) === 'shell_exec') {
|
2018-04-07 21:16:46 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenCode(
|
|
|
|
'Unsafe ' . implode('', $function->parts),
|
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
|
|
|
)) {
|
2019-03-23 15:58:26 +01:00
|
|
|
// continue
|
2018-04-07 21:16:46 +02:00
|
|
|
}
|
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,
|
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
|
|
|
)) {
|
2019-03-23 15:58:26 +01:00
|
|
|
// continue
|
2018-09-18 22:42:51 +02:00
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} elseif ($function->parts === ['define']) {
|
2019-02-18 19:51:27 +01:00
|
|
|
if ($first_arg) {
|
2019-02-22 00:19:12 +01:00
|
|
|
$fq_const_name = StatementsAnalyzer::getConstName(
|
|
|
|
$first_arg->value,
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer->getAliases()
|
|
|
|
);
|
2019-02-18 19:51:27 +01:00
|
|
|
|
2019-02-22 00:19:12 +01:00
|
|
|
if ($fq_const_name !== null) {
|
2019-02-18 19:51:27 +01:00
|
|
|
$second_arg = $stmt->args[1];
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = true;
|
2019-02-18 19:51:27 +01:00
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $second_arg->value, $context);
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = false;
|
2019-02-18 19:51:27 +01:00
|
|
|
|
|
|
|
$statements_analyzer->setConstType(
|
2019-02-22 00:19:12 +01:00
|
|
|
$fq_const_name,
|
2019-02-18 19:51:27 +01:00
|
|
|
isset($second_arg->value->inferredType) ?
|
|
|
|
$second_arg->value->inferredType :
|
|
|
|
Type::getMixed(),
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
} else {
|
|
|
|
$context->check_consts = false;
|
|
|
|
}
|
2019-02-26 07:03:33 +01:00
|
|
|
} elseif ($first_arg
|
|
|
|
&& $function_id
|
|
|
|
&& strpos($function_id, 'is_') === 0
|
|
|
|
&& $function_id !== 'is_a'
|
|
|
|
) {
|
|
|
|
if (isset($stmt->assertions)) {
|
|
|
|
$assertions = $stmt->assertions;
|
|
|
|
} else {
|
|
|
|
$assertions = AssertionFinder::processFunctionCall(
|
|
|
|
$stmt,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context->inside_negation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$changed_vars = [];
|
|
|
|
|
|
|
|
$referenced_var_ids = array_map(
|
|
|
|
function (array $_) : bool {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
$assertions
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($assertions) {
|
|
|
|
Reconciler::reconcileKeyedTypes(
|
|
|
|
$assertions,
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$changed_vars,
|
|
|
|
$referenced_var_ids,
|
|
|
|
$statements_analyzer,
|
|
|
|
[],
|
|
|
|
$context->inside_loop,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
|
|
|
}
|
2018-04-07 21:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:29:38 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|