Skip to content

Commit

Permalink
Add check for const with reserved word class
Browse files Browse the repository at this point in the history
  • Loading branch information
gphargreaves committed Oct 7, 2022
1 parent 028ac7f commit d476baa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,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) {
Expand Down
12 changes: 12 additions & 0 deletions tests/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,18 @@ public function valid(): bool {
',
'error_message' => 'MissingTemplateParam',
],
'cannotNameClassConstantClass' => [
'<?php
class Foo
{
/** @var class-string<Bar> */
protected const CLASS = Bar::class;
}
class Bar {}
',
'error_message' => 'ReservedWord',
]
];
}
}

0 comments on commit d476baa

Please sign in to comment.