diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 006fdad57..82d975670 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -1924,9 +1924,6 @@ class Config throw new LogicException("IncludeCollector should be set at this point"); } - $this->collectPredefinedConstants(); - $this->collectPredefinedFunctions(); - $vendor_autoload_files_path = $this->base_dir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'autoload_files.php'; @@ -1945,6 +1942,8 @@ class Config $codebase = $project_analyzer->getCodebase(); + $this->collectPredefinedFunctions(); + if ($this->autoloader) { // somee classes that we think are missing may not actually be missing // as they might be autoloadable once we require the autoloader below @@ -1959,6 +1958,8 @@ class Config ); } + $this->collectPredefinedConstants(); + $autoload_included_files = $this->include_collector->getFilteredIncludedFiles(); if ($autoload_included_files) { diff --git a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php index e78d66c48..c6c97ed85 100644 --- a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php @@ -860,7 +860,9 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse $this->file_storage->declaring_constants[$const_name] = $this->file_path; } - if ($this->codebase->register_stub_files || $this->codebase->register_autoload_files) { + if (($this->codebase->register_stub_files || $this->codebase->register_autoload_files) + && !\defined($const_name) + ) { $this->codebase->addGlobalConstantType($const_name, $const_type); } } diff --git a/tests/StubTest.php b/tests/StubTest.php index af24010da..f082a1f81 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -509,6 +509,7 @@ class StubTest extends TestCase dirname(__DIR__), ' @@ -530,6 +531,37 @@ class StubTest extends TestCase $this->analyzeFile($file_path, new Context()); } + /** + * @return void + */ + public function testConditionalConstantDefined() + { + $this->project_analyzer = $this->getProjectAnalyzerWithConfig( + TestConfig::loadFromXML( + dirname(__DIR__), + ' + + + + + ' + ) + ); + + $file_path = getcwd() . '/src/somefile.php'; + + $this->addFile( + $file_path, + 'analyzeFile($file_path, new Context()); + } + /** * @return void */ @@ -540,6 +572,7 @@ class StubTest extends TestCase dirname(__DIR__), ' diff --git a/tests/fixtures/stubs/conditional_constant_define_inferred.php b/tests/fixtures/stubs/conditional_constant_define_inferred.php new file mode 100644 index 000000000..af6d3d6ab --- /dev/null +++ b/tests/fixtures/stubs/conditional_constant_define_inferred.php @@ -0,0 +1,6 @@ +