Skip to content

Commit 9cbbd5b

Browse files
committed
experimental fixing
1 parent 4f54791 commit 9cbbd5b

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
6363
// Make sure the first letter is a capital.
6464
if (preg_match('|^[A-Z]|', $name) === 0) {
6565
$error = '%s name must use UpperCamel naming and begin with a capital letter';
66-
$phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData);
66+
$this->processUpperLowerCase($className, $phpcsFile, 'StartWithCapital', $error, $errorData);
6767
}
6868

6969
// Search for underscores.
@@ -75,10 +75,44 @@ public function process(File $phpcsFile, $stackPtr)
7575
// Ensure the name does not contain acronyms.
7676
if (preg_match('|[A-Z]{3}|', $name) === 1) {
7777
$error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row';
78-
$phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData);
78+
$this->processUpperLowerCase($className, $phpcsFile, 'NoUpperAcronyms', $error, $errorData);
7979
}
8080

8181
}//end process()
8282

8383

84+
protected function processUpperLowerCase(int $stackPtr, File $phpcsFile, string $errorCode, string $errorMessage, array $errorData): void
85+
{
86+
$fix = $phpcsFile->addFixableError($errorMessage, $stackPtr, $errorCode, $errorData);
87+
if ($fix === false) {
88+
return;
89+
}
90+
91+
$tokens = $phpcsFile->getTokens();
92+
$name = ucfirst($tokens[$stackPtr]['content']);
93+
$upperCaseStarted = false;
94+
for ($i = 0; $i < strlen($name); $i++) {
95+
if ($upperCaseStarted === true
96+
&& ctype_upper($name[$i]) === true
97+
&& isset($name[($i + 1)])
98+
&& (ctype_upper($name[($i + 1)]) === true || $name[($i + 1)] === '_')
99+
) {
100+
$name[$i] = strtolower($name[$i]);
101+
} else {
102+
if (ctype_upper($name[$i]) === true) {
103+
$upperCaseStarted = true;
104+
} else {
105+
$upperCaseStarted = false;
106+
}
107+
}
108+
}
109+
110+
$name[(strlen($name) - 1)] = strtolower($name[(strlen($name) - 1)]);
111+
$phpcsFile->fixer->replaceToken($stackPtr, $name);
112+
113+
// @todo Can we move files?
114+
115+
}//end processUpperLowerCase()
116+
117+
84118
}//end class

0 commit comments

Comments
 (0)