From 41a6afda32b7f3bbedf7afce07e85801fdead603 Mon Sep 17 00:00:00 2001 From: Gregory Hargreaves Date: Fri, 7 Oct 2022 09:44:10 +0100 Subject: [PATCH] Add check for const with reserved word class --- src/Psalm/Internal/Analyzer/ClassAnalyzer.php | 10 ++++++++++ tests/ClassTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/ClassAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassAnalyzer.php index f55fbaaa90d..eca4f0f498b 100644 --- a/src/Psalm/Internal/Analyzer/ClassAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassAnalyzer.php @@ -508,6 +508,16 @@ public function analyze( $member_stmts[] = $stmt; foreach ($stmt->consts as $const) { + if ($const->name->toLowerString() === 'class') { + IssueBuffer::maybeAdd( + new ReservedWord( + 'A class constant cannot be named \'class\'', + new CodeLocation($this, $this->class), + $this->fq_class_name + ) + ); + } + $const_id = strtolower($this->fq_class_name) . '::' . $const->name; foreach ($codebase->class_constants_to_rename as $original_const_id => $new_const_name) { diff --git a/tests/ClassTest.php b/tests/ClassTest.php index 342fe78b0a3..55a82d3f76c 100644 --- a/tests/ClassTest.php +++ b/tests/ClassTest.php @@ -925,6 +925,18 @@ final class C implements I {} ', 'error_message' => 'InvalidTraversableImplementation', ], + 'cannotNameClassConstantClass' => [ + ' */ + protected const CLASS = Bar::class; + } + + class Bar {} + ', + 'error_message' => 'ReservedWord', + ] ]; } }