1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Define globals in more places

This commit is contained in:
bugreportuser 2019-03-11 07:54:41 -06:00 committed by Matthew Brown
parent e22f216a5b
commit 0e9fb04f06
5 changed files with 29 additions and 14 deletions

View File

@ -1,10 +1,12 @@
<?php
namespace Psalm;
use Psalm\Config;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Clause;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Type\Reconciler;
use Psalm\Type;
use Psalm\Type\Union;
class Context
@ -742,4 +744,25 @@ class Context
}
return json_encode($summary);
}
public function defineGlobals() : void
{
$globals = [
'argv' => new Type\Union([
new Type\Atomic\TArray([Type::getInt(), Type::getString()]),
]),
'argc' => Type::getInt(),
];
$config = Config::getInstance();
foreach ($config->globals as $global_name => $type_string) {
$globals[$global_name] = Type::parseString($type_string);
}
foreach ($globals as $global_name => $type) {
$this->vars_in_scope['$' . $global_name] = $type;
$this->vars_possibly_in_scope[$global_name] = true;
}
}
}

View File

@ -142,6 +142,7 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
}
$this->context->is_global = true;
$this->context->defineGlobals();
try {
$stmts = $codebase->getStatementsForFile($this->file_path);

View File

@ -86,6 +86,7 @@ class NamespaceAnalyzer extends SourceAnalyzer implements StatementsSource
$context = new Context();
$context->collect_references = $codebase->collect_references;
$context->is_global = true;
$context->defineGlobals();
$statements_analyzer->analyze($leftover_stmts, $context);
}
}

View File

@ -110,20 +110,6 @@ class VariableFetchAnalyzer
return null;
}
if ($context->is_global && is_string($stmt->name)) {
$var_name = '$' . $stmt->name;
if (!$context->hasVariable($var_name, $statements_analyzer)) {
$type = $statements_analyzer->getGlobalType($stmt->name);
if ($type) {
$context->vars_in_scope[$var_name] = $type;
$context->vars_possibly_in_scope[$var_name] = true;
$stmt->inferredType = clone $type;
return null;
}
}
}
if (in_array(
$stmt->name,
[

View File

@ -1200,6 +1200,10 @@ class ConfigTest extends TestCase
ord($glob1);
ord($glob2["str"]);
$glob3->func();
}
namespace {
ord($glob1 ?? "str");
ord($_GET["str"] ?? "str");
}'
);