From 85f2083acb7dcb09d636baae3912cdb6bb7e9c2d Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Thu, 11 Nov 2021 00:44:17 +0200 Subject: [PATCH] Flag duplicate constants Fixes vimeo/psalm#6882 Addresses p1 from vimeo/psalm#6471 --- config.xsd | 1 + docs/running_psalm/issues.md | 1 + .../running_psalm/issues/DuplicateConstant.md | 16 +++++++++ .../Reflector/ClassLikeNodeScanner.php | 25 ++++++++++++++ src/Psalm/Issue/DuplicateConstant.php | 9 +++++ tests/ConstantTest.php | 33 +++++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 docs/running_psalm/issues/DuplicateConstant.md create mode 100644 src/Psalm/Issue/DuplicateConstant.php diff --git a/config.xsd b/config.xsd index 9682ba657..ab37ca2dc 100644 --- a/config.xsd +++ b/config.xsd @@ -240,6 +240,7 @@ + diff --git a/docs/running_psalm/issues.md b/docs/running_psalm/issues.md index 7e986f76d..43838dafc 100644 --- a/docs/running_psalm/issues.md +++ b/docs/running_psalm/issues.md @@ -20,6 +20,7 @@ - [DocblockTypeContradiction](issues/DocblockTypeContradiction.md) - [DuplicateArrayKey](issues/DuplicateArrayKey.md) - [DuplicateClass](issues/DuplicateClass.md) + - [DuplicateConstant](issues/DuplicateConstant.md) - [DuplicateFunction](issues/DuplicateFunction.md) - [DuplicateMethod](issues/DuplicateMethod.md) - [DuplicateParam](issues/DuplicateParam.md) diff --git a/docs/running_psalm/issues/DuplicateConstant.md b/docs/running_psalm/issues/DuplicateConstant.md new file mode 100644 index 000000000..44a2d0b51 --- /dev/null +++ b/docs/running_psalm/issues/DuplicateConstant.md @@ -0,0 +1,16 @@ +# DuplicateConstant + +Emitted when a constant is defined twice in a single class or when there's a +clash between a constant and an enum case. + +```php +constants[$const->name->name]) + || isset($storage->enum_cases[$const->name->name]) + ) { + if (IssueBuffer::accepts(new DuplicateConstant( + 'Constant names should be unique', + new CodeLocation($this->file_scanner, $const), + $fq_classlike_name + ))) { + // fall through + } + continue; + } + $storage->constants[$const->name->name] = $constant_storage = new \Psalm\Storage\ClassConstantStorage( $const_type, $stmt->isProtected() @@ -1271,6 +1285,17 @@ class ClassLikeNodeScanner ClassLikeStorage $storage, string $fq_classlike_name ): void { + if (isset($storage->constants[$stmt->name->name])) { + if (IssueBuffer::accepts(new DuplicateConstant( + 'Constant names should be unique', + new CodeLocation($this->file_scanner, $stmt), + $fq_classlike_name + ))) { + // fall through + } + return; + } + $enum_value = null; if ($stmt->expr !== null) { diff --git a/src/Psalm/Issue/DuplicateConstant.php b/src/Psalm/Issue/DuplicateConstant.php new file mode 100644 index 000000000..73783e321 --- /dev/null +++ b/src/Psalm/Issue/DuplicateConstant.php @@ -0,0 +1,9 @@ + 'UndefinedClass', ], + 'duplicateConstants' => [ + ' 'DuplicateConstant', + ], + 'constantDuplicatesEnumCase' => [ + ' 'DuplicateConstant', + [], + false, + '8.1', + ], + 'enumCaseDuplicatesConstant' => [ + ' 'DuplicateConstant', + [], + false, + '8.1', + ], ]; } }