Skip to content

Commit 9812cd0

Browse files
committed
feat(ValidClassName): Check traits and enums for valid upperCamel names
1 parent 338469e commit 9812cd0

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* \Drupal\Sniffs\NamingConventions\ValidClassNameSniff.
1717
*
18-
* Ensures class and interface names start with a capital letter
18+
* Ensures class, enum, interface and trait names start with a capital letter
1919
* and do not use _ separators.
2020
*
2121
* @category PHP
@@ -35,7 +35,9 @@ public function register()
3535
{
3636
return [
3737
T_CLASS,
38+
T_ENUM,
3839
T_INTERFACE,
40+
T_TRAIT,
3941
];
4042

4143
}//end register()
@@ -60,7 +62,7 @@ public function process(File $phpcsFile, $stackPtr)
6062

6163
// Make sure the first letter is a capital.
6264
if (preg_match('|^[A-Z]|', $name) === 0) {
63-
$error = '%s name must begin with a capital letter';
65+
$error = '%s name must use UpperCamel naming and begin with a capital letter';
6466
$phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData);
6567
}
6668

@@ -70,6 +72,12 @@ public function process(File $phpcsFile, $stackPtr)
7072
$phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
7173
}
7274

75+
// Ensure the name is not all uppercase.
76+
if (strtoupper($name) === $name) {
77+
$error = '%s name must use UpperCamel naming and not be all uppercase';
78+
$phpcsFile->addError($error, $stackPtr, 'NotAllUppercase', $errorData);
79+
}
80+
7381
}//end process()
7482

7583

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
class CorrectClassName {}
4+
class CorrectJSONClassName {}
5+
class INCORRECT_CLASS_NAME {}
6+
class INCORRECTCLASSNAME {}
7+
class incorrectLowercaseClassName {}
8+
9+
interface CorrectInterfaceName {}
10+
interface CorrectJSONInterfaceName {}
11+
interface INCORRECT_INTERFACE_NAME {}
12+
interface INCORRECTINTERFACENAME {}
13+
interface incorrectLowercaseInterfaceName {}
14+
15+
trait CorrectTraitName {}
16+
trait CorrectJSONTraitName {}
17+
trait INCORRECT_TRAIT_NAME {}
18+
trait INCORRECTTRAITNAME {}
19+
trait incorrectLowercaseTraitName {}
20+
21+
enum CorrectEnumName {}
22+
enum CorrectJSONEnumName {}
23+
enum INCORRECT_ENUM_NAME {}
24+
enum INCORRECTENUMNAME {}
25+
enum incorrectLowercaseEnumName {}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Drupal\Test\NamingConventions;
4+
5+
use Drupal\Test\CoderSniffUnitTest;
6+
7+
class ValidClassNameUnitTest extends CoderSniffUnitTest
8+
{
9+
10+
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @param string $testFile The name of the file being tested.
18+
*
19+
* @return array<int, int>
20+
*/
21+
protected function getErrorList(string $testFile): array
22+
{
23+
return [
24+
5 => 2,
25+
6 => 1,
26+
7 => 1,
27+
11 => 2,
28+
12 => 1,
29+
13 => 1,
30+
17 => 2,
31+
18 => 1,
32+
19 => 1,
33+
23 => 2,
34+
24 => 1,
35+
25 => 1,
36+
];
37+
38+
}//end getErrorList()
39+
40+
41+
/**
42+
* Returns the lines where warnings should occur.
43+
*
44+
* The key of the array should represent the line number and the value
45+
* should represent the number of warnings that should occur on that line.
46+
*
47+
* @param string $testFile The name of the file being tested.
48+
*
49+
* @return array<int, int>
50+
*/
51+
protected function getWarningList(string $testFile): array
52+
{
53+
return [];
54+
55+
}//end getWarningList()
56+
57+
58+
}//end class

tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ enum Test: int {
55
case one = 1;
66
// Must not contain underscores.
77
case TWO_TEST = 2;
8+
// Must not contain only upper case.
9+
case THREE = 3;
10+
// Some upper case parts are allowed for now.
11+
case FourJSONCase = 4;
812
}

tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ protected function getErrorList(string $testFile): array
2222
{
2323
return [
2424
5 => 1,
25-
7 => 1,
25+
7 => 2,
26+
9 => 1,
2627
];
2728

2829
}//end getErrorList()

tests/Drupal/bad/BadUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ protected function getErrorList(string $testFile): array
380380
827 => 1,
381381
829 => 1,
382382
836 => 1,
383-
838 => 1,
383+
838 => 3,
384384
849 => 2,
385385
860 => 2,
386386
867 => 1,

0 commit comments

Comments
 (0)