|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodingStandard_Sniffs_Formatting_ItemAssignmentSniff. |
|
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\Formatting; |
|
16
|
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Files\File; |
|
18
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CodingStandard_Sniffs_Formatting_ItemAssignmentSniff. |
|
22
|
|
|
* |
|
23
|
|
|
* Checks if the item assignment operator (=>) has |
|
24
|
|
|
* - a space before and after |
|
25
|
|
|
* |
|
26
|
|
|
* @category PHP |
|
27
|
|
|
* @package PHP_CodeSniffer |
|
28
|
|
|
* @author Peter Philipp <[email protected]> |
|
29
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
30
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
31
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
32
|
|
|
*/ |
|
33
|
|
|
class ItemAssignmentSniff implements Sniff |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
39
|
|
|
* |
|
40
|
|
|
* @return integer[] |
|
41
|
|
|
*/ |
|
42
|
|
|
public function register() |
|
43
|
|
|
{ |
|
44
|
|
|
return array(T_DOUBLE_ARROW); |
|
45
|
|
|
}//end register() |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
50
|
|
|
* |
|
51
|
|
|
* @param File $phpcsFile The file being scanned. |
|
52
|
|
|
* @param int $stackPtr The position of the current token in the |
|
53
|
|
|
* stack passed in $tokens. |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function process(File $phpcsFile, $stackPtr) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->checkSpacing($phpcsFile, $stackPtr, true); |
|
60
|
|
|
$this->checkSpacing($phpcsFile, $stackPtr, false); |
|
61
|
|
|
}//end process() |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Checks spacing at given position. |
|
66
|
|
|
* |
|
67
|
|
|
* @param File $phpcsFile The file being scanned. |
|
68
|
|
|
* @param int $stackPtr The position of the current token in the |
|
69
|
|
|
* stack passed in $tokens. |
|
70
|
|
|
* @param bool $before Determines direction in which to check spacing. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function checkSpacing(File $phpcsFile, $stackPtr, $before) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($before === true) { |
|
77
|
|
|
$stackPtrDiff = -1; |
|
78
|
|
|
$errorWord = 'prefix'; |
|
79
|
|
|
$errorCode = 'Before'; |
|
80
|
|
|
} else { |
|
81
|
|
|
$stackPtrDiff = 1; |
|
82
|
|
|
$errorWord = 'follow'; |
|
83
|
|
|
$errorCode = 'After'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
87
|
|
|
$tokenData = $tokens[($stackPtr + $stackPtrDiff)]; |
|
88
|
|
|
|
|
89
|
|
|
if ($tokenData['code'] !== T_WHITESPACE) { |
|
90
|
|
|
$error = 'Whitespace must '.$errorWord.' the item assignment operator =>'; |
|
91
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpacing'.$errorCode); |
|
92
|
|
|
if ($fix === true) { |
|
93
|
|
|
$phpcsFile->fixer->beginChangeset(); |
|
94
|
|
|
|
|
95
|
|
|
if ($before === true) { |
|
96
|
|
|
$phpcsFile->fixer->addContentBefore($stackPtr, ' '); |
|
97
|
|
|
} else { |
|
98
|
|
|
$phpcsFile->fixer->addContent($stackPtr, ' '); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$phpcsFile->fixer->endChangeset(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (isset($tokenData['orig_content']) === true) { |
|
108
|
|
|
$content = $tokenData['orig_content']; |
|
109
|
|
|
} else { |
|
110
|
|
|
$content = $tokenData['content']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($this->hasOnlySpaces($content) === false) { |
|
114
|
|
|
$error = 'Spaces must be used to '.$errorWord.' the item assignment operator =>'; |
|
115
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MixedWhitespace'.$errorCode); |
|
116
|
|
|
if ($fix === true) { |
|
117
|
|
|
$phpcsFile->fixer->beginChangeset(); |
|
118
|
|
|
$phpcsFile->fixer->replaceToken( |
|
119
|
|
|
($stackPtr + $stackPtrDiff), |
|
120
|
|
|
str_repeat(' ', strlen($content)) |
|
121
|
|
|
); |
|
122
|
|
|
$phpcsFile->fixer->endChangeset(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
}//end checkSpacing() |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Detects, that string contains only spaces. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $string String. |
|
132
|
|
|
* |
|
133
|
|
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function hasOnlySpaces($string) |
|
136
|
|
|
{ |
|
137
|
|
|
return substr_count($string, ' ') === strlen($string); |
|
138
|
|
|
}//end hasOnlySpaces() |
|
139
|
|
|
}//end class |
|
140
|
|
|
|