From 04c0db5affe61218a75524d2c39241ab50a2388c Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Mon, 21 Feb 2022 10:38:50 -0600 Subject: [PATCH 1/2] Use current context when analyzing attributes (fixes #7710). --- .../Internal/Analyzer/AttributeAnalyzer.php | 3 +- src/Psalm/Internal/Analyzer/ClassAnalyzer.php | 2 ++ .../Analyzer/FunctionLikeAnalyzer.php | 2 ++ .../Internal/Analyzer/InterfaceAnalyzer.php | 4 ++- tests/AttributeTest.php | 33 +++++++++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/AttributeAnalyzer.php b/src/Psalm/Internal/Analyzer/AttributeAnalyzer.php index 01b860c6c69..61c829218a5 100644 --- a/src/Psalm/Internal/Analyzer/AttributeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/AttributeAnalyzer.php @@ -27,6 +27,7 @@ class AttributeAnalyzer */ public static function analyze( SourceAnalyzer $source, + Context $context, AttributeStorage $attribute, AttributeGroup $attribute_group, array $suppressed_issues, @@ -111,7 +112,7 @@ public static function analyze( new NodeDataProvider() ); - $statements_analyzer->analyze(self::attributeGroupToStmts($attribute_group), new Context()); + $statements_analyzer->analyze(self::attributeGroupToStmts($attribute_group), $context); } /** diff --git a/src/Psalm/Internal/Analyzer/ClassAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassAnalyzer.php index 6fcfb427aea..d7bde976e1c 100644 --- a/src/Psalm/Internal/Analyzer/ClassAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassAnalyzer.php @@ -400,6 +400,7 @@ public function analyze( foreach ($storage->attributes as $i => $attribute) { AttributeAnalyzer::analyze( $this, + $class_context, $attribute, $class->attrGroups[$i], $storage->suppressed_issues + $this->getSuppressedIssues(), @@ -1526,6 +1527,7 @@ private function checkForMissingPropertyType( foreach ($property_storage->attributes as $i => $attribute) { AttributeAnalyzer::analyze( $source, + $context, $attribute, $stmt->attrGroups[$i], $this->source->getSuppressedIssues(), diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index 2102ddd006e..772ff8bda76 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -822,6 +822,7 @@ public function analyze( foreach ($storage->attributes as $i => $attribute) { AttributeAnalyzer::analyze( $this, + $context, $attribute, $this->function->attrGroups[$i], $storage->suppressed_issues + $this->getSuppressedIssues(), @@ -1271,6 +1272,7 @@ private function processParams( foreach ($function_param->attributes as $i => $attribute) { AttributeAnalyzer::analyze( $this, + $context, $attribute, $param_stmts[$offset]->attrGroups[$i], $storage->suppressed_issues, diff --git a/src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php b/src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php index 5759c1c6b2e..10cb397270d 100644 --- a/src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php @@ -95,10 +95,12 @@ public function analyze(): void } $class_storage = $codebase->classlike_storage_provider->get($fq_interface_name); + $interface_context = new Context($this->getFQCLN()); foreach ($class_storage->attributes as $i => $attribute) { AttributeAnalyzer::analyze( $this, + $interface_context, $attribute, $this->class->attrGroups[$i], $class_storage->suppressed_issues + $this->getSuppressedIssues(), @@ -113,7 +115,7 @@ public function analyze(): void $type_provider = new NodeDataProvider(); - $method_analyzer->analyze(new Context($this->getFQCLN()), $type_provider); + $method_analyzer->analyze($interface_context, $type_provider); $actual_method_id = $method_analyzer->getMethodId(); diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index 06ebe3f6ce7..a3472780bfa 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -264,6 +264,25 @@ public function __construct(?array $listOfB = null) {} class C {} ', ], + 'attributeUsesClassContext' => [ + ' [ + ' 'ParentNotFound', + ], ]; } } From 103ec628b0a9a2dd094a68efcd230126d0690a3d Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Mon, 21 Feb 2022 10:44:59 -0600 Subject: [PATCH 2/2] Improve tests. --- tests/AttributeTest.php | 55 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index a3472780bfa..c6735854541 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -264,7 +264,7 @@ public function __construct(?array $listOfB = null) {} class C {} ', ], - 'attributeUsesClassContext' => [ + 'selfInClassAttribute' => [ ' [ + ' [ + '