1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodingStandard_Sniffs_Array_ArraySniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Peter Philipp <[email protected]> |
10
|
|
|
* @author Alexander Obuhovich <[email protected]> |
11
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
12
|
|
|
* @link https://github.com/aik099/CodingStandard |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CodingStandard_Sniffs_Array_ArraySniff. |
17
|
|
|
* |
18
|
|
|
* Checks if the array's are styled in the Drupal way. |
19
|
|
|
* - Comma after the last array element |
20
|
|
|
* |
21
|
|
|
* @category PHP |
22
|
|
|
* @package PHP_CodeSniffer |
23
|
|
|
* @author Peter Philipp <[email protected]> |
24
|
|
|
* @author Alexander Obuhovich <[email protected]> |
25
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
26
|
|
|
* @link https://github.com/aik099/CodingStandard |
27
|
|
|
*/ |
28
|
|
|
class CodingStandard_Sniffs_Array_ArraySniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns an array of tokens this test wants to listen for. |
34
|
|
|
* |
35
|
|
|
* @return integer[] |
36
|
|
|
*/ |
37
|
|
|
public function register() |
38
|
|
|
{ |
39
|
|
|
return array(T_ARRAY); |
40
|
|
|
|
41
|
|
|
}//end register() |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Processes this test, when one of its tokens is encountered. |
46
|
|
|
* |
47
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
48
|
|
|
* @param int $stackPtr The position of the current token in the |
49
|
|
|
* stack passed in $tokens. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
54
|
|
|
{ |
55
|
|
|
$tokens = $phpcsFile->getTokens(); |
56
|
|
|
$arrayStart = $tokens[$stackPtr]['parenthesis_opener']; |
57
|
|
|
$arrayEnd = $tokens[$arrayStart]['parenthesis_closer']; |
58
|
|
|
|
59
|
|
|
if ($arrayStart !== ($stackPtr + 1)) { |
60
|
|
|
$error = 'There must be no space between the Array keyword and the opening parenthesis'; |
61
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword'); |
62
|
|
|
if ($fix === true) { |
63
|
|
|
$phpcsFile->fixer->beginChangeset(); |
64
|
|
|
|
65
|
|
|
for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) { |
66
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$phpcsFile->fixer->endChangeset(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Check for empty arrays. |
74
|
|
|
$content = $phpcsFile->findNext(array(T_WHITESPACE), ($arrayStart + 1), ($arrayEnd + 1), true); |
75
|
|
|
if ($content === $arrayEnd) { |
76
|
|
|
// Empty array, but if the brackets aren't together, there's a problem. |
77
|
|
|
if (($arrayEnd - $arrayStart) !== 1) { |
78
|
|
|
$error = 'Empty array declaration must have no space between the parentheses'; |
79
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray'); |
80
|
|
|
if ($fix === true) { |
81
|
|
|
$phpcsFile->fixer->beginChangeset(); |
82
|
|
|
|
83
|
|
|
for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) { |
84
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$phpcsFile->fixer->endChangeset(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// We can return here because there is nothing else to check. All code |
91
|
|
|
// below can assume that the array is not empty. |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$lastItem = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($arrayEnd - 1), $stackPtr, true); |
97
|
|
|
|
98
|
|
|
// Empty array. |
99
|
|
|
if ($lastItem === $arrayStart) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Inline array. |
104
|
|
|
$isInlineArray = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']; |
105
|
|
|
|
106
|
|
|
// Check if the last item in a multiline array has a "closing" comma. |
107
|
|
|
if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false) { |
108
|
|
|
$error = 'A comma should follow the last multiline array item. Found: '.$tokens[$lastItem]['content']; |
109
|
|
|
$fix = $phpcsFile->addFixableWarning($error, $lastItem, 'NoLastComma'); |
|
|
|
|
110
|
|
|
if ($fix === true) { |
111
|
|
|
$phpcsFile->fixer->beginChangeset(); |
112
|
|
|
$phpcsFile->fixer->addContent($lastItem, ','); |
|
|
|
|
113
|
|
|
$phpcsFile->fixer->endChangeset(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($isInlineArray === true) { |
120
|
|
|
if ($tokens[$lastItem]['code'] === T_COMMA) { |
121
|
|
|
$error = 'Comma not allowed after last value in single-line array declaration'; |
122
|
|
|
$fix = $phpcsFile->addFixableWarning($error, $lastItem, 'LastComma'); |
|
|
|
|
123
|
|
|
if ($fix === true) { |
124
|
|
|
$phpcsFile->fixer->beginChangeset(); |
125
|
|
|
$phpcsFile->fixer->replaceToken($lastItem, ''); |
|
|
|
|
126
|
|
|
$phpcsFile->fixer->endChangeset(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Inline array must not have spaces within parenthesis. |
133
|
|
|
if ($content !== ($arrayStart + 1)) { |
134
|
|
|
$error = 'Space found after opening parenthesis of Array'; |
135
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen'); |
136
|
|
|
if ($fix === true) { |
137
|
|
|
$phpcsFile->fixer->beginChangeset(); |
138
|
|
|
|
139
|
|
|
for ($i = ($arrayStart + 1); $i < $content; $i++) { |
140
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$phpcsFile->fixer->endChangeset(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($lastItem !== ($arrayEnd - 1)) { |
148
|
|
|
$error = 'Space found before closing parenthesis of Array'; |
149
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose'); |
150
|
|
|
if ($fix === true) { |
151
|
|
|
$phpcsFile->fixer->beginChangeset(); |
152
|
|
|
|
153
|
|
|
for ($i = ($lastItem + 1); $i < $arrayEnd; $i++) { |
154
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$phpcsFile->fixer->endChangeset(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
}//end if |
161
|
|
|
|
162
|
|
|
}//end process() |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
}//end class |
166
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.