Skip to content

Commit 04a4563

Browse files
authored
feat(ValidClassName): Check traits and enums for valid upperCamel names (#3497580)
1 parent 338469e commit 04a4563

File tree

9 files changed

+126
-10
lines changed

9 files changed

+126
-10
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ jobs:
8484
# core is updated to that version.
8585
run: |
8686
cd drupal/core
87-
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php
87+
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php,core/lib/Drupal/Core/FileTransfer/FTP.php,core/lib/Drupal/Core/FileTransfer/SSH.php

coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,23 @@ public function register()
5353
*/
5454
public function process(File $phpcsFile, $stackPtr)
5555
{
56-
$tokens = $phpcsFile->getTokens();
57-
$find = Tokens::$methodPrefixes;
58-
$find[T_WHITESPACE] = T_WHITESPACE;
59-
$find[T_READONLY] = T_READONLY;
56+
$tokens = $phpcsFile->getTokens();
57+
$find = ([
58+
T_ABSTRACT => T_ABSTRACT,
59+
T_FINAL => T_FINAL,
60+
T_READONLY => T_READONLY,
61+
T_WHITESPACE => T_WHITESPACE,
62+
] + Tokens::$phpcsCommentTokens);
6063
$name = $tokens[$stackPtr]['content'];
6164
$classCodeStart = $stackPtr;
6265

6366
$previousContent = null;
6467
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
6568
if (isset($find[$tokens[$commentEnd]['code']]) === true) {
69+
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
70+
$classCodeStart = $commentEnd;
71+
}
72+
6673
continue;
6774
}
6875

@@ -78,7 +85,7 @@ public function process(File $phpcsFile, $stackPtr)
7885
}
7986

8087
break;
81-
}
88+
}//end for
8289

8390
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
8491
&& $tokens[$commentEnd]['code'] !== T_COMMENT

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php

Lines changed: 13 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,15 @@ public function process(File $phpcsFile, $stackPtr)
7072
$phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
7173
}
7274

75+
// Ensure the name is not all uppercase.
76+
// @todo We could make this more strict to check if there are more than
77+
// 2 upper case characters in a row, but not decided yet.
78+
// See https://www.drupal.org/project/coder/issues/3497433
79+
if (strtoupper($name) === $name) {
80+
$error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row';
81+
$phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData);
82+
}
83+
7384
}//end process()
7485

7586

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 CorrectClassWithAReallyLongName {}
5+
class INCORRECT_CLASS_NAME {}
6+
class INCORRECTCLASSNAME {}
7+
class incorrectLowercaseClassName {}
8+
9+
interface CorrectInterfaceName {}
10+
interface CorrectInterfaceWithAReallyLongName {}
11+
interface INCORRECT_INTERFACE_NAME {}
12+
interface INCORRECTINTERFACENAME {}
13+
interface incorrectLowercaseInterfaceName {}
14+
15+
trait CorrectTraitName {}
16+
trait CorrectTraitWithAReallyLongName {}
17+
trait INCORRECT_TRAIT_NAME {}
18+
trait INCORRECTTRAITNAME {}
19+
trait incorrectLowercaseTraitName {}
20+
21+
enum CorrectEnumName {}
22+
enum CorrectEnumWithAReallyLongName {}
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ 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+
// Upper case parts are allowed for now.
11+
case FourJSONCase = 4;
12+
case FiveAndAHorseCorrect = 5;
813
}

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,

tests/Drupal/good/good.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,3 +1905,12 @@ public function __construct(
19051905
) {}
19061906

19071907
}
1908+
1909+
/**
1910+
* Doc block is here and an ignore directive is ok.
1911+
*/
1912+
// phpcs:ignore Drupal.NamingConventions.ValidClassName
1913+
enum PUROSELY_WRONG_BUT_OK: int {
1914+
case One = 1;
1915+
case Two = 2;
1916+
}

0 commit comments

Comments
 (0)