Skip to content

Commit 1950643

Browse files
authored
Allow enum suffix omission when in Enum namespace (#428)
Relax the ValidEnumNameSniff to allow omitting the "Enum" suffix when the enum is in a namespace containing "Enum" as a segment (e.g., `App\Model\Enum\Status` is now valid without the suffix). This avoids redundant naming like `App\Model\Enum\FooBarEnum`. Refs #425
1 parent 9cf1618 commit 1950643

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

CakePHP/Sniffs/NamingConventions/ValidEnumNameSniff.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
46
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
@@ -38,9 +40,37 @@ public function process(File $phpcsFile, $stackPtr)
3840
{
3941
$enumName = $phpcsFile->getDeclarationName($stackPtr);
4042

41-
if (!str_ends_with($enumName, 'Enum')) {
42-
$error = 'Enums must have an "Enum" suffix.';
43+
if (!str_ends_with($enumName, 'Enum') && !$this->isInEnumNamespace($phpcsFile)) {
44+
$error = 'Enums must have an "Enum" suffix (or be in an Enum namespace).';
4345
$phpcsFile->addError($error, $stackPtr, 'InvalidEnumName');
4446
}
4547
}
48+
49+
/**
50+
* Check if the file's namespace contains "Enum" as a segment.
51+
*
52+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
53+
* @return bool
54+
*/
55+
protected function isInEnumNamespace(File $phpcsFile): bool
56+
{
57+
$tokens = $phpcsFile->getTokens();
58+
$namespacePtr = $phpcsFile->findNext(T_NAMESPACE, 0);
59+
60+
if ($namespacePtr === false) {
61+
return false;
62+
}
63+
64+
$namespaceEnd = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $namespacePtr);
65+
$namespace = '';
66+
67+
for ($i = $namespacePtr + 1; $i < $namespaceEnd; $i++) {
68+
if ($tokens[$i]['code'] === T_STRING || $tokens[$i]['code'] === T_NAME_QUALIFIED) {
69+
$namespace .= $tokens[$i]['content'];
70+
}
71+
}
72+
73+
// Check if namespace ends with \Enum or contains \Enum\
74+
return (bool)preg_match('/\\\\Enum(\\\\|$)/', $namespace);
75+
}
4676
}

CakePHP/Tests/NamingConventions/ValidEnumNameUnitTest.inc renamed to CakePHP/Tests/NamingConventions/ValidEnumNameUnitTest.1.inc

File renamed without changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace App\Model\Enum;
3+
4+
// Valid: no suffix needed when in Enum namespace
5+
enum Status: string {
6+
case Draft = 'draft';
7+
}
8+
9+
// Also valid: suffix is still allowed
10+
enum StatusEnum: string {
11+
case Published = 'published';
12+
}

CakePHP/Tests/NamingConventions/ValidEnumNameUnitTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@ class ValidEnumNameUnitTest extends AbstractSniffTestCase
99
/**
1010
* @inheritDoc
1111
*/
12-
public function getErrorList()
12+
public function getErrorList($testFile = '')
1313
{
14-
return [
15-
2 => 1,
16-
];
14+
switch ($testFile) {
15+
case 'ValidEnumNameUnitTest.1.inc':
16+
return [
17+
2 => 1,
18+
];
19+
20+
case 'ValidEnumNameUnitTest.2.inc':
21+
// No errors - enums in Enum namespace don't need suffix
22+
return [];
23+
24+
default:
25+
return [];
26+
}
1727
}
1828

1929
/**
2030
* @inheritDoc
2131
*/
22-
public function getWarningList()
32+
public function getWarningList($testFile = '')
2333
{
2434
return [];
2535
}

0 commit comments

Comments
 (0)