Conditions | 21 |
Paths | 292 |
Total Lines | 135 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
70 | public function process(File $phpcsFile, $stackPtr): void |
||
71 | { |
||
72 | $tokens = $phpcsFile->getTokens(); |
||
73 | $current = $tokens[$stackPtr]; |
||
74 | |||
75 | if (T_ARRAY === $current['code']) { |
||
76 | $start = $current['parenthesis_opener']; |
||
77 | $end = $current['parenthesis_closer']; |
||
78 | } else { |
||
79 | $start = $current['bracket_opener']; |
||
80 | $end = $current['bracket_closer']; |
||
81 | } |
||
82 | |||
83 | if ($tokens[$start]['line'] === $tokens[$end]['line']) { |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | // phpcs:disable |
||
88 | /** @var array<int> $assignments */ |
||
89 | $assignments = []; |
||
90 | // phpcs:enable |
||
91 | $keyEndColumn = -1; |
||
92 | $lastLine = -1; |
||
93 | |||
94 | for ($i = ($start + 1); $i < $end; $i++) { |
||
95 | $current = $tokens[$i]; |
||
96 | $previous = $tokens[($i - 1)]; |
||
97 | |||
98 | // Skip nested arrays. |
||
99 | if (true === \in_array($current['code'], $this->arrayTokens, true)) { |
||
100 | $i = T_ARRAY === $current['code'] ? ($current['parenthesis_closer'] + 1) : ($current['bracket_closer'] + 1); |
||
101 | |||
102 | continue; |
||
103 | } |
||
104 | |||
105 | // Skip closures in array. |
||
106 | if (T_CLOSURE === $current['code']) { |
||
107 | $i = ($current['scope_closer'] + 1); |
||
108 | |||
109 | continue; |
||
110 | } |
||
111 | |||
112 | $i = (int) $i; |
||
113 | |||
114 | if (T_DOUBLE_ARROW !== $current['code']) { |
||
115 | continue; |
||
116 | } |
||
117 | |||
118 | $assignments[] = $i; |
||
119 | $column = $previous['column']; |
||
120 | $line = $current['line']; |
||
121 | |||
122 | if ($lastLine === $line) { |
||
123 | $previousComma = $this->getPreviousComma($phpcsFile, $i, $start); |
||
124 | |||
125 | $msg = 'only one "=>" assignments per line is allowed in a multi line array'; |
||
126 | |||
127 | if (false !== $previousComma) { |
||
128 | $fixable = $phpcsFile->addFixableError($msg, $i, 'OneAssignmentPerLine'); |
||
129 | |||
130 | if (true === $fixable) { |
||
131 | $phpcsFile->fixer->beginChangeset(); |
||
132 | $phpcsFile->fixer->addNewline((int) $previousComma); |
||
133 | $phpcsFile->fixer->endChangeset(); |
||
134 | } |
||
135 | } else { |
||
136 | // Remove current and previous '=>' from array for further processing. |
||
137 | \array_pop($assignments); |
||
138 | \array_pop($assignments); |
||
139 | $phpcsFile->addError($msg, $i, 'OneAssignmentPerLine'); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | $hasKeyInLine = false; |
||
144 | |||
145 | $j = ($i - 1); |
||
146 | |||
147 | while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) { |
||
148 | if (false === \in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true)) { |
||
149 | $hasKeyInLine = true; |
||
150 | } |
||
151 | |||
152 | $j--; |
||
153 | } |
||
154 | |||
155 | if (false === $hasKeyInLine) { |
||
156 | $fixable = $phpcsFile->addFixableError( |
||
157 | 'in arrays, keys and "=>" must be on the same line', |
||
158 | $i, |
||
159 | 'KeyAndValueNotOnSameLine' |
||
160 | ); |
||
161 | |||
162 | if (true === $fixable) { |
||
163 | $phpcsFile->fixer->beginChangeset(); |
||
164 | $phpcsFile->fixer->replaceToken($j, ''); |
||
165 | $phpcsFile->fixer->endChangeset(); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | if ($column > $keyEndColumn) { |
||
170 | $keyEndColumn = $column; |
||
171 | } |
||
172 | |||
173 | $lastLine = $line; |
||
174 | } |
||
175 | |||
176 | $doubleArrowStartColumn = ($keyEndColumn + 1); |
||
177 | |||
178 | foreach ($assignments as $ptr) { |
||
179 | $current = $tokens[$ptr]; |
||
180 | $column = $current['column']; |
||
181 | |||
182 | $beforeArrowPtr = ($ptr - 1); |
||
183 | $currentIndent = \strlen($tokens[$beforeArrowPtr]['content']); |
||
184 | $correctIndent = ($currentIndent - $column + $doubleArrowStartColumn); |
||
185 | |||
186 | if ($column === $doubleArrowStartColumn) { |
||
187 | continue; |
||
188 | } |
||
189 | |||
190 | $fixable = $phpcsFile->addFixableError("each \"=>\" assignments must be aligned; current indentation before \"=>\" are {$currentIndent} space(s), must be {$correctIndent} space(s)", $ptr, 'AssignmentsNotAligned'); |
||
191 | |||
192 | if (false === $fixable) { |
||
193 | continue; |
||
194 | } |
||
195 | |||
196 | $phpcsFile->fixer->beginChangeset(); |
||
197 | |||
198 | if (T_WHITESPACE === $tokens[$beforeArrowPtr]['code']) { |
||
199 | $phpcsFile->fixer->replaceToken($beforeArrowPtr, \str_repeat(' ', $correctIndent)); |
||
200 | } else { |
||
201 | $phpcsFile->fixer->addContent($beforeArrowPtr, \str_repeat(' ', $correctIndent)); |
||
202 | } |
||
203 | |||
204 | $phpcsFile->fixer->endChangeset(); |
||
205 | } |
||
242 |