From 9cbe38928569cfbd7d453b2bb4d359b140a772ba Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sun, 1 Sep 2019 16:52:40 -0400 Subject: [PATCH] Fix #910 - dead code issues with abstract vendor method --- src/Psalm/Internal/Codebase/ClassLikes.php | 16 +++++++ tests/StubTest.php | 55 +++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Codebase/ClassLikes.php b/src/Psalm/Internal/Codebase/ClassLikes.php index bfdcc9934..a6cc72fc3 100644 --- a/src/Psalm/Internal/Codebase/ClassLikes.php +++ b/src/Psalm/Internal/Codebase/ClassLikes.php @@ -1488,6 +1488,13 @@ class ClassLikes $method_id = $declaring_method_id; } + if ($method_storage->location + && !$project_analyzer->canReportIssues($method_storage->location->file_path) + && !$codebase->analyzer->canReportIssues($method_storage->location->file_path) + ) { + continue; + } + $method_referenced = $this->file_reference_provider->isClassMethodReferenced(strtolower($method_id)); if (!$method_referenced @@ -1510,12 +1517,21 @@ class ClassLikes foreach ($classlike_storage->overridden_method_ids[$method_name_lc] as $parent_method_id) { $parent_method_storage = $methods->getStorage($parent_method_id); + if ($parent_method_storage->location + && !$project_analyzer->canReportIssues($parent_method_storage->location->file_path) + ) { + // here we just don’t know + $has_parent_references = true; + break; + } + $parent_method_referenced = $this->file_reference_provider->isClassMethodReferenced( strtolower($parent_method_id) ); if (!$parent_method_storage->abstract || $parent_method_referenced) { $has_parent_references = true; + break; } } } diff --git a/tests/StubTest.php b/tests/StubTest.php index c530dac4f..4f1d7794d 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -929,7 +929,6 @@ class StubTest extends TestCase */ public function testStubFileWithTemplatedClassDefinitionAndMagicMethodOverride() { - //$this->markTestSkipped('Currently broken'); $this->project_analyzer = $this->getProjectAnalyzerWithConfig( TestConfig::loadFromXML( dirname(__DIR__), @@ -973,4 +972,58 @@ class StubTest extends TestCase $this->analyzeFile($file_path, new Context()); } + + public function testInheritedMethodUsedInStub() : void + { + $this->project_analyzer = $this->getProjectAnalyzerWithConfig( + TestConfig::loadFromXML( + dirname(__DIR__), + ' + + + + + ' + ) + ); + + $this->project_analyzer->getCodebase()->reportUnusedCode(); + + $vendor_file_path = getcwd() . '/vendor/vendor_class.php'; + + $this->addFile( + $vendor_file_path, + 'foo(); + } + }', + ); + + $file_path = getcwd() . '/src/somefile.php'; + + $this->addFile( + $file_path, + 'collect_references = true; + + $this->analyzeFile($file_path, $context, false); + + $this->project_analyzer->checkClassReferences(); + } }