Skip to content

Commit

Permalink
Merge pull request #8542 from gphargreaves/class-constant-named-class
Browse files Browse the repository at this point in the history
Add check for class const with reserved word 'class'
  • Loading branch information
orklah committed Oct 7, 2022
2 parents b6ddcdf + 41a6afd commit 52e96be
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
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions tests/ClassTest.php
Expand Up @@ -925,6 +925,18 @@ final class C implements I {}
',
'error_message' => 'InvalidTraversableImplementation',
],
'cannotNameClassConstantClass' => [
'<?php
class Foo
{
/** @var class-string<Bar> */
protected const CLASS = Bar::class;
}
class Bar {}
',
'error_message' => 'ReservedWord',
]
];
}
}

0 comments on commit 52e96be

Please sign in to comment.