diff --git a/examples/TemplateChecker.php b/examples/TemplateChecker.php index 52df42f4f..7a7220c5d 100644 --- a/examples/TemplateChecker.php +++ b/examples/TemplateChecker.php @@ -96,12 +96,22 @@ class TemplateAnalyzer extends Psalm\Internal\Analyzer\FileAnalyzer $constructor_id = $class . '::__construct'; - $this->project_analyzer->getMethodMutations($constructor_id, $this_context); + $this->project_analyzer->getMethodMutations( + $constructor_id, + $this_context, + $this->getRootFilePath(), + $this->getRootFileName() + ); $this_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic\TNamedObject($class)]); // check the actual method - $this->project_analyzer->getMethodMutations($method_id, $this_context); + $this->project_analyzer->getMethodMutations( + $method_id, + $this_context, + $this->getRootFilePath(), + $this->getRootFileName() + ); $view_context = new Context(); $view_context->self = self::VIEW_CLASS; diff --git a/src/Psalm/Internal/Analyzer/FileAnalyzer.php b/src/Psalm/Internal/Analyzer/FileAnalyzer.php index b843842e0..75be3230b 100644 --- a/src/Psalm/Internal/Analyzer/FileAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FileAnalyzer.php @@ -277,7 +277,12 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource $class_analyzer_to_examine = $this->class_analyzers_to_analyze[strtolower($fq_class_name)]; } else { if (!$from_project_analyzer) { - $this->project_analyzer->getMethodMutations($method_id, $this_context); + $this->project_analyzer->getMethodMutations( + $method_id, + $this_context, + $this->getRootFilePath(), + $this->getRootFileName() + ); } return; diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 4cdc4a1b0..ca9861b38 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -810,12 +810,18 @@ class ProjectAnalyzer * * @return void */ - public function getMethodMutations($original_method_id, Context $this_context) - { + public function getMethodMutations( + $original_method_id, + Context $this_context, + string $root_file_path, + string $root_file_name + ) { list($fq_class_name) = explode('::', $original_method_id); $file_analyzer = $this->getFileAnalyzerForClassLike($fq_class_name); + $file_analyzer->setRootFilePath($root_file_path, $root_file_name); + $appearing_method_id = $this->codebase->methods->getAppearingMethodId($original_method_id); if (!$appearing_method_id) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index 6736d69c3..de3ab4ad7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -88,7 +88,12 @@ class CallAnalyzer $context->initialized_methods[$method_id] = true; } - $project_analyzer->getMethodMutations($method_id, $context); + $project_analyzer->getMethodMutations( + $method_id, + $context, + $source->getRootFilePath(), + $source->getRootFileName() + ); } } elseif ($context->collect_initializations && $context->self && @@ -186,7 +191,12 @@ class CallAnalyzer $old_self = $context->self; $context->self = $declaring_fq_class_name; - $project_analyzer->getMethodMutations($declaring_method_id, $context); + $project_analyzer->getMethodMutations( + $declaring_method_id, + $context, + $source->getRootFilePath(), + $source->getRootFileName() + ); $context->self = $old_self; } diff --git a/tests/IncludeTest.php b/tests/IncludeTest.php index cffbf938c..d108f18ef 100644 --- a/tests/IncludeTest.php +++ b/tests/IncludeTest.php @@ -491,6 +491,54 @@ class IncludeTest extends TestCase 'hoist_constants' => false, 'error_levels' => ['DuplicateClass', 'MissingPropertyType'], ], + 'functionsDefined' => [ + 'files' => [ + getcwd() . DIRECTORY_SEPARATOR . 'index.php' => ' ' ' 'x = 5; + } + }', + ], + 'files_to_check' => [ + getcwd() . DIRECTORY_SEPARATOR . 'index.php', + ], + ], ]; } diff --git a/tests/MethodMutationTest.php b/tests/MethodMutationTest.php index 342c79f40..50db896fa 100644 --- a/tests/MethodMutationTest.php +++ b/tests/MethodMutationTest.php @@ -93,7 +93,12 @@ class MethodMutationTest extends TestCase $this->project_analyzer->getCodebase()->scanFiles(); $method_context = new Context(); $method_context->collect_mutations = true; - $this->project_analyzer->getMethodMutations('FooController::barBar', $method_context); + $this->project_analyzer->getMethodMutations( + 'FooController::barBar', + $method_context, + 'somefile.php', + 'somefile.php' + ); $this->assertSame('UserViewData', (string)$method_context->vars_in_scope['$this->user_viewdata']); $this->assertSame('string', (string)$method_context->vars_in_scope['$this->user_viewdata->name']); @@ -131,7 +136,12 @@ class MethodMutationTest extends TestCase $this->project_analyzer->getCodebase()->scanFiles(); $method_context = new Context(); $method_context->collect_mutations = true; - $this->project_analyzer->getMethodMutations('FooController::__construct', $method_context); + $this->project_analyzer->getMethodMutations( + 'FooController::__construct', + $method_context, + 'somefile.php', + 'somefile.php' + ); $this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']); } @@ -168,7 +178,12 @@ class MethodMutationTest extends TestCase $this->project_analyzer->getCodebase()->scanFiles(); $method_context = new Context(); $method_context->collect_mutations = true; - $this->project_analyzer->getMethodMutations('FooController::__construct', $method_context); + $this->project_analyzer->getMethodMutations( + 'FooController::__construct', + $method_context, + 'somefile.php', + 'somefile.php' + ); $this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']); }