-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathValidClassNameSniff.php
More file actions
118 lines (97 loc) · 3.66 KB
/
ValidClassNameSniff.php
File metadata and controls
118 lines (97 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* \Drupal\Sniffs\NamingConventions\ValidClassNameSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* \Drupal\Sniffs\NamingConventions\ValidClassNameSniff.
*
* Ensures class, enum, interface and trait names start with a capital letter
* and do not use _ separators.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ValidClassNameSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_CLASS,
T_ENUM,
T_INTERFACE,
T_TRAIT,
];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$className = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = trim($tokens[$className]['content']);
$errorData = [ucfirst($tokens[$stackPtr]['content'])];
// Make sure the first letter is a capital.
if (preg_match('|^[A-Z]|', $name) === 0) {
$error = '%s name must use UpperCamel naming and begin with a capital letter';
$this->processUpperLowerCase($className, $phpcsFile, 'StartWithCapital', $error, $errorData);
}
// Search for underscores.
if (strpos($name, '_') !== false) {
$error = '%s name must use UpperCamel naming without underscores';
$phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
}
// 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';
$this->processUpperLowerCase($className, $phpcsFile, 'NoUpperAcronyms', $error, $errorData);
}
}//end process()
protected function processUpperLowerCase(int $stackPtr, File $phpcsFile, string $errorCode, string $errorMessage, array $errorData): void
{
$fix = $phpcsFile->addFixableError($errorMessage, $stackPtr, $errorCode, $errorData);
if ($fix === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$name = ucfirst($tokens[$stackPtr]['content']);
$upperCaseStarted = false;
for ($i = 0; $i < strlen($name); $i++) {
if ($upperCaseStarted === true
&& ctype_upper($name[$i]) === true
&& isset($name[($i + 1)])
&& (ctype_upper($name[($i + 1)]) === true || $name[($i + 1)] === '_')
) {
$name[$i] = strtolower($name[$i]);
} else {
if (ctype_upper($name[$i]) === true) {
$upperCaseStarted = true;
} else {
$upperCaseStarted = false;
}
}
}
$name[(strlen($name) - 1)] = strtolower($name[(strlen($name) - 1)]);
$phpcsFile->fixer->replaceToken($stackPtr, $name);
// @todo Can we move files?
}//end processUpperLowerCase()
}//end class