From b5d6e69d16396261ef2bc4b6bd12a392dc1b185c Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 23 May 2024 20:18:33 -0500 Subject: [PATCH 1/5] Add test case Co-Authored-By: Philip Hofstetter Signed-off-by: Nathanael Esayeas --- tests/OverrideTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/OverrideTest.php b/tests/OverrideTest.php index c64c0491cb0..b1bca79bead 100644 --- a/tests/OverrideTest.php +++ b/tests/OverrideTest.php @@ -98,6 +98,35 @@ public function __toString(): string { 'ignored_issues' => [], 'php_version' => '8.3', ], + 'Issue #10982 - https://github.com/vimeo/psalm/issues/10982' => [ + 'code' => ' + inTrait(); + } + } + + class B extends A { + use Foo; + + function baz(): void + { + $this->inTrait(); + } + } + ', + 'assertions' => [], + 'ignored_issues' => [], + 'php_version' => '8.3', + ], ]; } From b472898faa877ec4ba798b064c03ba96a4d2732b Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 23 May 2024 20:18:55 -0500 Subject: [PATCH 2/5] Update FunctionLikeAnalyzer.php Signed-off-by: Nathanael Esayeas --- src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index b6d941e98ea..b73fd330dcf 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -1995,6 +1995,7 @@ private function getFunctionInformation( if (!$has_override_attribute && $codebase->config->ensure_override_attribute && $overridden_method_ids + && $storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PRIVATE && $storage->cased_name !== '__construct' && ($storage->cased_name !== '__toString' || isset($appearing_class_storage->direct_class_interfaces['stringable'])) From d47ef0347639316309b6d6086734954c3b24ec0b Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Sun, 26 May 2024 22:06:58 -0500 Subject: [PATCH 3/5] Add an additional test case Signed-off-by: Nathanael Esayeas Co-Authored-By: Evan Shaw <369904+edsrzf@users.noreply.github.com> --- tests/OverrideTest.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/OverrideTest.php b/tests/OverrideTest.php index b1bca79bead..de308963148 100644 --- a/tests/OverrideTest.php +++ b/tests/OverrideTest.php @@ -98,7 +98,7 @@ public function __toString(): string { 'ignored_issues' => [], 'php_version' => '8.3', ], - 'Issue #10982 - https://github.com/vimeo/psalm/issues/10982' => [ + 'Test case #10982 - https://github.com/vimeo/psalm/issues/10982' => [ 'code' => ' [], 'php_version' => '8.3', ], + 'Test case #10989 - https://github.com/vimeo/psalm/pull/10989#discussion_r1615149365' => [ + 'code' => ' + inTrait(); + } + } + + class B extends A { + use Foo; + + function baz(): void + { + $this->inTrait(); + } + } + + $b = new B(); + $b->baz(); + ', + 'assertions' => [], + 'ignored_issues' => [], + 'php_version' => '8.3', + ], ]; } From 4d39914355ffd76829c5a1d6c23c4136230e2148 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Sun, 26 May 2024 22:08:27 -0500 Subject: [PATCH 4/5] Update FunctionLikeAnalyzer.php Signed-off-by: Nathanael Esayeas --- src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index b73fd330dcf..737ec2207cf 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -1995,10 +1995,10 @@ private function getFunctionInformation( if (!$has_override_attribute && $codebase->config->ensure_override_attribute && $overridden_method_ids - && $storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PRIVATE && $storage->cased_name !== '__construct' - && ($storage->cased_name !== '__toString' - || isset($appearing_class_storage->direct_class_interfaces['stringable'])) + && (array_key_exists($storage->cased_name, $appearing_class_storage->methods) + || array_key_exists('stringable', $appearing_class_storage->direct_class_interfaces) + ) ) { IssueBuffer::maybeAdd( new MissingOverrideAttribute( From 3a5ae1ce5b352a9fc5c6ec0c9179d9f81a1e7488 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Mon, 27 May 2024 13:51:27 -0500 Subject: [PATCH 5/5] Add additional test cases from PHP RFC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nathanael Esayeas Co-Authored-By: Tim Düsterhus <209270+TimWolla@users.noreply.github.com> --- tests/OverrideTest.php | 203 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) diff --git a/tests/OverrideTest.php b/tests/OverrideTest.php index de308963148..d5afec8cbc9 100644 --- a/tests/OverrideTest.php +++ b/tests/OverrideTest.php @@ -160,6 +160,116 @@ function baz(): void 'ignored_issues' => [], 'php_version' => '8.3', ], + 'Valid examples #1 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' + [], + 'ignored_issues' => [], + 'php_version' => '8.3', + ], + 'Valid examples #2 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' + */ + class Foo implements IteratorAggregate + { + #[\Override] + public function getIterator(): Traversable + { + yield from []; + } + } + ', + 'assertions' => [], + 'ignored_issues' => [], + 'php_version' => '8.3', + ], + 'Valid examples #3 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' [], + 'ignored_issues' => [], + 'php_version' => '8.3', + ], + 'Valid examples #4 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' [], + 'ignored_issues' => [], + 'php_version' => '8.3', + ], + 'Valid examples #5 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' [], + 'ignored_issues' => ['MissingReturnType'], + 'php_version' => '8.3', + ], ]; } @@ -278,6 +388,99 @@ public function __toString(): string { 'error_levels' => [], 'php_version' => '8.3', ], + 'Invalid examples #1 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' 'InvalidOverride', + 'error_levels' => [], + 'php_version' => '8.3', + ], + 'Invalid examples #2 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' 'InvalidOverride', + 'error_levels' => [], + 'php_version' => '8.3', + ], + 'Invalid examples #3 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' 'InvalidOverride', + 'error_levels' => [], + 'php_version' => '8.3', + ], + 'Invalid examples #4 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' 'InvalidOverride', + 'error_levels' => [], + 'php_version' => '8.3', + ], + 'Invalid examples #5 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' 'InvalidOverride', + 'error_levels' => [], + 'php_version' => '8.3', + ], + 'Invalid examples #6 - https://wiki.php.net/rfc/marking_overriden_methods' => [ + 'code' => ' 'InvalidOverride', + 'error_levels' => [], + 'php_version' => '8.3', + ], ]; } }