Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables should outlive namespaces #8779

Merged
merged 1 commit into from Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php
Expand Up @@ -87,16 +87,17 @@ public function collectAnalyzableInformation(): void

if ($leftover_stmts) {
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider());
$context = new Context();
$context->is_global = true;
$context->defineGlobals();
$context->collect_exceptions = $codebase->config->check_for_throws_in_global_scope;
$statements_analyzer->analyze($leftover_stmts, $context, null, true);

$file_context = $this->source->context;
if ($file_context) {
$file_context->mergeExceptions($context);

if ($file_context !== null) {
$context = $file_context;
} else {
$context = new Context();
$context->is_global = true;
$context->defineGlobals();
$context->collect_exceptions = $codebase->config->check_for_throws_in_global_scope;
}
$statements_analyzer->analyze($leftover_stmts, $context, null, true);
}
}

Expand Down
13 changes: 8 additions & 5 deletions tests/Config/ConfigTest.php
Expand Up @@ -152,7 +152,7 @@ public function testIgnoreMissingProjectDirectory(): void
$config = $this->project_analyzer->getConfig();

$this->assertTrue($config->isInProjectDirs(realpath('src/Psalm/Type.php')));
$this->assertFalse($config->isInProjectDirs(realpath(__DIR__.'/../../').'/does/not/exist/FileAnalyzer.php'));
$this->assertFalse($config->isInProjectDirs(realpath(__DIR__ . '/../../') . '/does/not/exist/FileAnalyzer.php'));
$this->assertFalse($config->isInProjectDirs(realpath('examples/TemplateScanner.php')));
}

Expand Down Expand Up @@ -1159,15 +1159,18 @@ function example1(): void {
ord($_GET["str"]);
}
$glob1 = 0;
error_reporting($glob1);
$z = $glob1;
$z = 0;
error_reporting($z);
$old = $_GET["str"];
$_GET["str"] = 0;
error_reporting($_GET["str"]);
$_GET["str"] = $old;
function example2(): void {
global $glob1, $glob2, $glob3;
error_reporting($glob1);
global $z, $glob2, $glob3;
error_reporting($z);
ord($glob2["str"]);
$glob3->func();
ord($_GET["str"]);
Expand Down
18 changes: 18 additions & 0 deletions tests/NamespaceTest.php
Expand Up @@ -71,6 +71,24 @@ function foo() : void {
$c = $argv;
}',
],
'varsAreNotScoped' => [
'code' => '<?php
namespace A {
$a = "1";
}
namespace B\C {
$bc = "2";
}
namespace {
echo $a . PHP_EOL;
echo $bc . PHP_EOL;
}
',
'assertions' => [
'$a===' => "'1'",
'$bc===' => "'2'",
],
],
];
}

Expand Down