1 | <?php |
||
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 | 1 | public function 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 | 1 | public function process(File $phpcsFile, $stackPtr) |
|
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 | 1 | protected function checkSpacing(File $phpcsFile, $stackPtr, $before) |
|
75 | { |
||
76 | 1 | if ($before === true) { |
|
77 | 1 | $stackPtrDiff = -1; |
|
78 | 1 | $errorWord = 'prefix'; |
|
79 | 1 | $errorCode = 'Before'; |
|
80 | 1 | } else { |
|
81 | 1 | $stackPtrDiff = 1; |
|
82 | 1 | $errorWord = 'follow'; |
|
83 | 1 | $errorCode = 'After'; |
|
84 | } |
||
85 | |||
86 | 1 | $tokens = $phpcsFile->getTokens(); |
|
87 | 1 | $tokenData = $tokens[($stackPtr + $stackPtrDiff)]; |
|
88 | |||
89 | 1 | if ($tokenData['code'] !== T_WHITESPACE) { |
|
90 | 1 | $error = 'Whitespace must '.$errorWord.' the item assignment operator =>'; |
|
91 | 1 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpacing'.$errorCode); |
|
92 | 1 | if ($fix === true) { |
|
93 | 1 | $phpcsFile->fixer->beginChangeset(); |
|
94 | |||
95 | 1 | if ($before === true) { |
|
96 | 1 | $phpcsFile->fixer->addContentBefore($stackPtr, ' '); |
|
97 | 1 | } else { |
|
98 | 1 | $phpcsFile->fixer->addContent($stackPtr, ' '); |
|
99 | } |
||
100 | |||
101 | 1 | $phpcsFile->fixer->endChangeset(); |
|
102 | 1 | } |
|
103 | |||
104 | 1 | return; |
|
105 | } |
||
106 | |||
107 | 1 | if (isset($tokenData['orig_content']) === true) { |
|
108 | $content = $tokenData['orig_content']; |
||
109 | } else { |
||
110 | 1 | $content = $tokenData['content']; |
|
111 | } |
||
112 | |||
113 | 1 | if ($this->hasOnlySpaces($content) === false) { |
|
114 | 1 | $error = 'Spaces must be used to '.$errorWord.' the item assignment operator =>'; |
|
115 | 1 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MixedWhitespace'.$errorCode); |
|
116 | 1 | if ($fix === true) { |
|
117 | 1 | $phpcsFile->fixer->beginChangeset(); |
|
118 | 1 | $phpcsFile->fixer->replaceToken( |
|
119 | 1 | ($stackPtr + $stackPtrDiff), |
|
120 | 1 | str_repeat(' ', strlen($content)) |
|
121 | 1 | ); |
|
122 | 1 | $phpcsFile->fixer->endChangeset(); |
|
123 | 1 | } |
|
124 | 1 | } |
|
125 | 1 | }//end checkSpacing() |
|
126 | |||
127 | |||
128 | /** |
||
129 | * Detects, that string contains only spaces. |
||
130 | * |
||
131 | * @param string $string String. |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | 1 | protected function hasOnlySpaces($string) |
|
139 | }//end class |
||
140 |