Conditions | 21 |
Paths | 292 |
Total Lines | 125 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
61 | public function process(File $phpcsFile, $stackPtr): void |
||
62 | { |
||
63 | $tokens = $phpcsFile->getTokens(); |
||
64 | $current = $tokens[$stackPtr]; |
||
65 | |||
66 | if ($current['code'] === T_ARRAY) { |
||
67 | $start = $current['parenthesis_opener']; |
||
68 | $end = $current['parenthesis_closer']; |
||
69 | } else { |
||
70 | $start = $current['bracket_opener']; |
||
71 | $end = $current['bracket_closer']; |
||
72 | } |
||
73 | |||
74 | if ($tokens[$start]['line'] === $tokens[$end]['line']) { |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | $assignments = []; |
||
79 | $keyEndColumn = -1; |
||
80 | $lastLine = -1; |
||
81 | |||
82 | for ($i = ($start + 1); $i < $end; $i++) { |
||
83 | $current = $tokens[$i]; |
||
84 | $previous = $tokens[($i - 1)]; |
||
85 | |||
86 | // Skip nested arrays. |
||
87 | if (\in_array($current['code'], $this->arrayTokens, true) === true) { |
||
88 | if ($current['code'] === T_ARRAY) { |
||
89 | $i = ($current['parenthesis_closer'] + 1); |
||
90 | } else { |
||
91 | $i = ($current['bracket_closer'] + 1); |
||
92 | } |
||
93 | |||
94 | continue; |
||
95 | } |
||
96 | |||
97 | // Skip closures in array. |
||
98 | if ($current['code'] === T_CLOSURE) { |
||
99 | $i = ($current['scope_closer'] + 1); |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | if ($current['code'] === T_DOUBLE_ARROW) { |
||
104 | $assignments[] = $i; |
||
105 | $column = $previous['column']; |
||
106 | $line = $current['line']; |
||
107 | |||
108 | if ($lastLine === $line) { |
||
109 | $previousComma = $this->getPreviousComma($phpcsFile, $i, $start); |
||
110 | |||
111 | $msg = 'only one "=>" assignments per line is allowed in a multi line array'; |
||
112 | |||
113 | if ($previousComma !== false) { |
||
114 | $fixable = $phpcsFile->addFixableError($msg, $i, 'OneAssignmentPerLine'); |
||
115 | |||
116 | if ($fixable === true) { |
||
117 | $phpcsFile->fixer->beginChangeset(); |
||
118 | $phpcsFile->fixer->addNewline((int) $previousComma); |
||
119 | $phpcsFile->fixer->endChangeset(); |
||
120 | } |
||
121 | } else { |
||
122 | // Remove current and previous '=>' from array for further processing. |
||
123 | array_pop($assignments); |
||
124 | array_pop($assignments); |
||
125 | $phpcsFile->addError($msg, $i, 'OneAssignmentPerLine'); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | $hasKeyInLine = false; |
||
130 | |||
131 | $j = ((int) $i - 1); |
||
132 | while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) { |
||
133 | if (\in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true) === false) { |
||
134 | $hasKeyInLine = true; |
||
135 | } |
||
136 | |||
137 | $j--; |
||
138 | } |
||
139 | |||
140 | if ($hasKeyInLine === false) { |
||
141 | $fixable = $phpcsFile->addFixableError( |
||
142 | 'in arrays, keys and "=>" must be on the same line', |
||
143 | $i, |
||
144 | 'KeyAndValueNotOnSameLine' |
||
145 | ); |
||
146 | |||
147 | if ($fixable === true) { |
||
148 | $phpcsFile->fixer->beginChangeset(); |
||
149 | $phpcsFile->fixer->replaceToken($j, ''); |
||
150 | $phpcsFile->fixer->endChangeset(); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | if ($column > $keyEndColumn) { |
||
155 | $keyEndColumn = $column; |
||
156 | } |
||
157 | |||
158 | $lastLine = $line; |
||
159 | }//end if |
||
160 | }//end for |
||
161 | |||
162 | $doubleArrowStartColumn = ($keyEndColumn + 1); |
||
163 | |||
164 | foreach ($assignments as $ptr) { |
||
165 | $current = $tokens[$ptr]; |
||
166 | $column = $current['column']; |
||
167 | |||
168 | $beforeArrowPtr = ((int) $ptr - 1); |
||
169 | $currentIndent = \strlen($tokens[$beforeArrowPtr]['content']); |
||
170 | $correctIndent = (int) ($currentIndent - $column + $doubleArrowStartColumn); |
||
171 | if ($column !== $doubleArrowStartColumn) { |
||
172 | $fixable = $phpcsFile->addFixableError("each \"=>\" assignments must be aligned; current indentation before \"=>\" are $currentIndent space(s), must be $correctIndent space(s)", (int) $ptr, 'AssignmentsNotAligned'); |
||
173 | |||
174 | if ($fixable === false) { |
||
175 | continue; |
||
176 | } |
||
177 | |||
178 | $phpcsFile->fixer->beginChangeset(); |
||
179 | if ($tokens[$beforeArrowPtr]['code'] === T_WHITESPACE) { |
||
180 | $phpcsFile->fixer->replaceToken($beforeArrowPtr, str_repeat(' ', $correctIndent)); |
||
181 | } else { |
||
182 | $phpcsFile->fixer->addContent($beforeArrowPtr, str_repeat(' ', $correctIndent)); |
||
183 | } |
||
184 | |||
185 | $phpcsFile->fixer->endChangeset(); |
||
186 | } |
||
227 |