Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ValidClassName): Throw errors on upper case acronyms #255

Open
wants to merge 9 commits into
base: 8.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
}

// Ensure the name is not all uppercase.
// @todo We could make this more strict to check if there are more than
// 2 upper case characters in a row, but not decided yet.
// See https://www.drupal.org/project/coder/issues/3497433
if (strtoupper($name) === $name) {
// Ensure the name does not contain acronyms.
if (preg_match('|[A-Z]{3}|', $name) === 1) {
$error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row';
$phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Drupal/NamingConventions/ValidClassNameUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

class CorrectClassName {}
class CorrectClassWithAReallyLongName {}
class IncorrectJSONClassName {}
class INCORRECT_CLASS_NAME {}
class INCORRECTCLASSNAME {}
class incorrectLowercaseClassName {}

interface CorrectInterfaceName {}
interface CorrectInterfaceWithAReallyLongName {}
interface IncorrectJSONInterfaceName {}
interface INCORRECT_INTERFACE_NAME {}
interface INCORRECTINTERFACENAME {}
interface incorrectLowercaseInterfaceName {}

trait CorrectTraitName {}
trait CorrectTraitWithAReallyLongName {}
trait IncorrectJSONTraitName {}
trait INCORRECT_TRAIT_NAME {}
trait INCORRECTTRAITNAME {}
trait incorrectLowercaseTraitName {}

enum CorrectEnumName {}
enum CorrectEnumWithAReallyLongName {}
enum IncorrectJSONEnumName {}
enum INCORRECT_ENUM_NAME {}
enum INCORRECTENUMNAME {}
enum incorrectLowercaseEnumName {}
22 changes: 13 additions & 9 deletions tests/Drupal/NamingConventions/ValidClassNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ class ValidClassNameUnitTest extends CoderSniffUnitTest
protected function getErrorList(string $testFile): array
{
return [
5 => 2,
6 => 1,
5 => 1,
6 => 2,
7 => 1,
11 => 2,
8 => 1,
12 => 1,
13 => 1,
17 => 2,
18 => 1,
13 => 2,
14 => 1,
15 => 1,
19 => 1,
23 => 2,
24 => 1,
25 => 1,
20 => 2,
21 => 1,
22 => 1,
26 => 1,
27 => 2,
28 => 1,
29 => 1,
];

}//end getErrorList()
Expand Down
16 changes: 15 additions & 1 deletion tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ enum Test: int {
case TWO_TEST = 2;
// Must not contain only upper case.
case THREE = 3;
// Upper case parts are allowed for now.
// Upper case parts are not allowed.
case FourJSONCase = 4;
case FiveAndAHorseCorrect = 5;
}

// Those are all ok.
enum FiscalQuarter {
case Q1;
case Q2;
case Q3;
case Q4;
}

enum Plan {
case A;
case B;
case C;
}
7 changes: 4 additions & 3 deletions tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ class ValidEnumCaseUnitTest extends CoderSniffUnitTest
protected function getErrorList(string $testFile): array
{
return [
5 => 1,
7 => 2,
9 => 1,
5 => 1,
7 => 2,
9 => 1,
11 => 1,
];

}//end getErrorList()
Expand Down
Loading