1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Merge pull request #8542 from gphargreaves/class-constant-named-class

Add check for class const with reserved word 'class'
This commit is contained in:
orklah 2022-10-07 18:13:24 +02:00 committed by GitHub
commit 52e96bea38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -508,6 +508,16 @@ class ClassAnalyzer extends ClassLikeAnalyzer
$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) {

View File

@ -925,6 +925,18 @@ class ClassTest extends TestCase
',
'error_message' => 'InvalidTraversableImplementation',
],
'cannotNameClassConstantClass' => [
'<?php
class Foo
{
/** @var class-string<Bar> */
protected const CLASS = Bar::class;
}
class Bar {}
',
'error_message' => 'ReservedWord',
]
];
}
}