|
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
|
|
|
namespace CodingStandard\Sniffs\Arrays; |
|
16
|
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Files\File; |
|
18
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
19
|
|
|
use PHP_CodeSniffer\Util\Tokens; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* CodingStandard_Sniffs_Array_ArraySniff. |
|
23
|
|
|
* |
|
24
|
|
|
* Checks if the array's are styled in the Drupal way. |
|
25
|
|
|
* - Comma after the last array element |
|
26
|
|
|
* |
|
27
|
|
|
* @category PHP |
|
28
|
|
|
* @package PHP_CodeSniffer |
|
29
|
|
|
* @author Peter Philipp <[email protected]> |
|
30
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
31
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
32
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
33
|
|
|
*/ |
|
34
|
|
|
class ArraySniff implements Sniff |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
40
|
|
|
* |
|
41
|
|
|
* @return integer[] |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function register() |
|
44
|
|
|
{ |
|
45
|
1 |
|
return array(T_ARRAY); |
|
46
|
|
|
}//end register() |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
51
|
|
|
* |
|
52
|
|
|
* @param File $phpcsFile The file being scanned. |
|
53
|
|
|
* @param int $stackPtr The position of the current token in the |
|
54
|
|
|
* stack passed in $tokens. |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
|
61
|
1 |
|
$arrayStart = $tokens[$stackPtr]['parenthesis_opener']; |
|
62
|
1 |
|
$arrayEnd = $tokens[$arrayStart]['parenthesis_closer']; |
|
63
|
|
|
|
|
64
|
1 |
|
if ($arrayStart !== ($stackPtr + 1)) { |
|
65
|
1 |
|
$error = 'There must be no space between the Array keyword and the opening parenthesis'; |
|
66
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword'); |
|
67
|
1 |
|
if ($fix === true) { |
|
68
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
69
|
|
|
|
|
70
|
1 |
|
for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) { |
|
71
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
75
|
1 |
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Check for empty arrays. |
|
79
|
1 |
|
$content = $phpcsFile->findNext(array(T_WHITESPACE), ($arrayStart + 1), ($arrayEnd + 1), true); |
|
80
|
1 |
|
if ($content === $arrayEnd) { |
|
81
|
|
|
// Empty array, but if the brackets aren't together, there's a problem. |
|
82
|
1 |
|
if (($arrayEnd - $arrayStart) !== 1) { |
|
83
|
1 |
|
$error = 'Empty array declaration must have no space between the parentheses'; |
|
84
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray'); |
|
85
|
1 |
|
if ($fix === true) { |
|
86
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
87
|
|
|
|
|
88
|
1 |
|
for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) { |
|
89
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
// We can return here because there is nothing else to check. All code |
|
96
|
|
|
// below can assume that the array is not empty. |
|
97
|
1 |
|
return; |
|
98
|
|
|
} |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$lastItem = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($arrayEnd - 1), $stackPtr, true); |
|
102
|
|
|
|
|
103
|
|
|
// Empty array. |
|
104
|
1 |
|
if ($lastItem === $arrayStart) { |
|
105
|
1 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Inline array. |
|
109
|
1 |
|
$isInlineArray = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']; |
|
110
|
|
|
|
|
111
|
|
|
// Check if the last item in a multiline array has a "closing" comma. |
|
112
|
1 |
|
if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false) { |
|
113
|
1 |
|
$error = 'A comma should follow the last multiline array item. Found: '.$tokens[$lastItem]['content']; |
|
114
|
1 |
|
$fix = $phpcsFile->addFixableWarning($error, $lastItem, 'NoLastComma'); |
|
|
|
|
|
|
115
|
1 |
|
if ($fix === true) { |
|
116
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
117
|
1 |
|
$phpcsFile->fixer->addContent($lastItem, ','); |
|
|
|
|
|
|
118
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
if ($isInlineArray === true) { |
|
125
|
1 |
|
if ($tokens[$lastItem]['code'] === T_COMMA) { |
|
126
|
1 |
|
$error = 'Comma not allowed after last value in single-line array declaration'; |
|
127
|
1 |
|
$fix = $phpcsFile->addFixableWarning($error, $lastItem, 'LastComma'); |
|
|
|
|
|
|
128
|
1 |
|
if ($fix === true) { |
|
129
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
130
|
1 |
|
$phpcsFile->fixer->replaceToken($lastItem, ''); |
|
|
|
|
|
|
131
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Inline array must not have spaces within parenthesis. |
|
138
|
1 |
|
if ($content !== ($arrayStart + 1)) { |
|
139
|
1 |
|
$error = 'Space found after opening parenthesis of Array'; |
|
140
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen'); |
|
141
|
1 |
|
if ($fix === true) { |
|
142
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
143
|
|
|
|
|
144
|
1 |
|
for ($i = ($arrayStart + 1); $i < $content; $i++) { |
|
145
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
149
|
1 |
|
} |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
if ($lastItem !== ($arrayEnd - 1)) { |
|
153
|
1 |
|
$error = 'Space found before closing parenthesis of Array'; |
|
154
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose'); |
|
155
|
1 |
|
if ($fix === true) { |
|
156
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
157
|
|
|
|
|
158
|
1 |
|
for ($i = ($lastItem + 1); $i < $arrayEnd; $i++) { |
|
159
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
160
|
1 |
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
163
|
1 |
|
} |
|
164
|
1 |
|
} |
|
165
|
1 |
|
}//end if |
|
166
|
1 |
|
}//end process() |
|
167
|
|
|
}//end class |
|
168
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.