Skip to content

Commit 27c2ab7

Browse files
committed
add custom enum sniff
1 parent 6446baa commit 27c2ab7

4 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://github.com/cakephp/cakephp-codesniffer
12+
* @since CakePHP CodeSniffer 0.1.10
13+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
16+
/**
17+
* Ensures enum names use the Enum suffix.
18+
*/
19+
namespace CakePHP\Sniffs\NamingConventions;
20+
21+
use PHP_CodeSniffer\Files\File;
22+
use PHP_CodeSniffer\Sniffs\Sniff;
23+
24+
class ValidEnumNameSniff implements Sniff
25+
{
26+
/**
27+
* @inheritDoc
28+
*/
29+
public function register()
30+
{
31+
return [T_ENUM];
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function process(File $phpcsFile, $stackPtr)
38+
{
39+
$enumName = $phpcsFile->getDeclarationName($stackPtr);
40+
41+
if (!str_ends_with($enumName, 'Enum')) {
42+
$error = 'Enums must have an "Enum" suffix.';
43+
$phpcsFile->addError($error, $stackPtr, 'InvalidEnumName');
44+
}
45+
}
46+
}

CakePHP/Sniffs/NamingConventions/ValidTraitNameSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function process(File $phpcsFile, $stackPtr)
3939
$tokens = $phpcsFile->getTokens();
4040
$traitName = $tokens[$stackPtr + 2]['content'];
4141

42-
if (substr($traitName, -5) !== 'Trait') {
42+
if (!str_ends_with($traitName, 'Trait')) {
4343
$error = 'Traits must have a "Trait" suffix.';
4444
$phpcsFile->addError($error, $stackPtr, 'InvalidTraitName');
4545
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
enum Status: string {
3+
case Draft = 'draft';
4+
}
5+
6+
enum StatusEnum: string {
7+
case Published = 'published';
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace CakePHP\Tests\NamingConventions;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
6+
7+
class ValidEnumNameUnitTest extends AbstractSniffTestCase
8+
{
9+
/**
10+
* @inheritDoc
11+
*/
12+
public function getErrorList()
13+
{
14+
return [
15+
2 => 1,
16+
];
17+
}
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function getWarningList()
23+
{
24+
return [];
25+
}
26+
}

0 commit comments

Comments
 (0)