2020-05-19 18:56:23 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements;
|
|
|
|
|
|
|
|
use PhpParser;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2020-05-19 18:56:23 +02:00
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\VariableFetchAnalyzer;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2021-01-29 00:58:02 +01:00
|
|
|
use Psalm\Internal\DataFlow\DataFlowNode;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\ReferenceConstraint;
|
2020-05-19 18:56:23 +02:00
|
|
|
use Psalm\Issue\InvalidGlobal;
|
|
|
|
use Psalm\IssueBuffer;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
use function is_string;
|
|
|
|
|
2022-01-03 07:55:32 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-19 18:56:23 +02:00
|
|
|
class GlobalAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Stmt\Global_ $stmt,
|
|
|
|
Context $context,
|
|
|
|
?Context $global_context
|
2021-12-05 18:51:26 +01:00
|
|
|
): void {
|
2020-05-19 18:56:23 +02:00
|
|
|
if (!$context->collect_initializations && !$global_context) {
|
2021-11-29 20:54:17 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2020-05-19 18:56:23 +02:00
|
|
|
new InvalidGlobal(
|
2022-02-18 18:36:25 +01:00
|
|
|
'Cannot use global scope here (unless this file is included from a non-global scope)',
|
2022-12-18 17:15:15 +01:00
|
|
|
new CodeLocation($statements_analyzer, $stmt),
|
2020-05-19 18:56:23 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$statements_analyzer->getSource()->getSuppressedIssues(),
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
|
|
|
|
2022-07-30 01:29:05 +02:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2020-05-19 18:56:23 +02:00
|
|
|
$source = $statements_analyzer->getSource();
|
|
|
|
$function_storage = $source instanceof FunctionLikeAnalyzer
|
|
|
|
? $source->getFunctionLikeStorage($statements_analyzer)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($var->name)) {
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
if ($var->name === 'argv' || $var->name === 'argc') {
|
2022-07-30 01:29:05 +02:00
|
|
|
$context->vars_in_scope[$var_id] =
|
|
|
|
VariableFetchAnalyzer::getGlobalType($var_id, $codebase->analysis_php_version_id);
|
2020-05-19 18:56:23 +02:00
|
|
|
} elseif (isset($function_storage->global_types[$var_id])) {
|
2022-11-04 19:04:23 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $function_storage->global_types[$var_id];
|
2020-05-19 18:56:23 +02:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] =
|
2020-09-30 18:28:13 +02:00
|
|
|
$global_context && $global_context->hasVariable($var_id)
|
2022-11-04 19:04:23 +01:00
|
|
|
? $global_context->vars_in_scope[$var_id]
|
2022-07-30 01:29:05 +02:00
|
|
|
: VariableFetchAnalyzer::getGlobalType($var_id, $codebase->analysis_php_version_id);
|
2020-05-19 18:56:23 +02:00
|
|
|
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$context->byref_constraints[$var_id] = new ReferenceConstraint();
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
2022-01-07 23:16:36 +01:00
|
|
|
|
2021-01-29 00:58:02 +01:00
|
|
|
$assignment_node = DataFlowNode::getForAssignment(
|
|
|
|
$var_id,
|
2022-12-18 17:15:15 +01:00
|
|
|
new CodeLocation($statements_analyzer, $var),
|
2021-01-29 00:58:02 +01:00
|
|
|
);
|
2022-11-04 19:04:23 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $context->vars_in_scope[$var_id]->setParentNodes([
|
2021-01-29 00:58:02 +01:00
|
|
|
$assignment_node->id => $assignment_node,
|
2022-11-04 19:04:23 +01:00
|
|
|
]);
|
2022-01-07 23:16:36 +01:00
|
|
|
$context->references_to_external_scope[$var_id] = true;
|
|
|
|
|
|
|
|
if (isset($context->references_in_scope[$var_id])) {
|
|
|
|
// Global shadows existing reference
|
2022-05-27 00:46:20 +02:00
|
|
|
$context->decrementReferenceCount($var_id);
|
2022-01-07 23:16:36 +01:00
|
|
|
unset($context->references_in_scope[$var_id]);
|
|
|
|
}
|
2021-01-29 00:58:02 +01:00
|
|
|
$statements_analyzer->registerVariable(
|
|
|
|
$var_id,
|
|
|
|
new CodeLocation($statements_analyzer, $var),
|
2022-12-18 17:15:15 +01:00
|
|
|
$context->branch_point,
|
2021-01-29 00:58:02 +01:00
|
|
|
);
|
|
|
|
$statements_analyzer->getCodebase()->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$var,
|
2022-12-18 17:15:15 +01:00
|
|
|
$var_id,
|
2021-01-29 00:58:02 +01:00
|
|
|
);
|
2022-01-07 23:16:36 +01:00
|
|
|
|
2022-01-26 19:54:42 +01:00
|
|
|
if ($global_context !== null && $global_context->hasVariable($var_id)) {
|
|
|
|
$global_context->referenced_globals[$var_id] = true;
|
2022-01-07 23:16:36 +01:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|