Conditions | 24 |
Paths | > 20000 |
Total Lines | 142 |
Code Lines | 89 |
Lines | 42 |
Ratio | 29.58 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
129 | public function generate( |
||
130 | $cachedData, |
||
131 | $totalFiles, |
||
132 | $totalErrors, |
||
133 | $totalWarnings, |
||
134 | $totalFixable, |
||
135 | $showSources=false, |
||
136 | $width=80, |
||
137 | $toScreen=true |
||
138 | ) { |
||
139 | if (empty($this->_sourceCache) === true) { |
||
140 | // Nothing to show. |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | // Make sure the report width isn't too big. |
||
145 | $maxLength = 0; |
||
146 | foreach ($this->_sourceCache as $source => $data) { |
||
147 | $maxLength = max($maxLength, $data['strlen']); |
||
148 | } |
||
149 | |||
150 | if ($showSources === true) { |
||
151 | $width = min($width, ($maxLength + 11)); |
||
152 | } else { |
||
153 | $width = min($width, ($maxLength + 41)); |
||
154 | } |
||
155 | |||
156 | $width = max($width, 70); |
||
157 | |||
158 | asort($this->_sourceCache); |
||
159 | $this->_sourceCache = array_reverse($this->_sourceCache); |
||
160 | |||
161 | echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL; |
||
162 | echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
||
163 | if ($showSources === true) { |
||
164 | View Code Duplication | if ($totalFixable > 0) { |
|
165 | echo ' SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL; |
||
166 | } else { |
||
167 | echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL; |
||
168 | } |
||
169 | View Code Duplication | } else { |
|
170 | if ($totalFixable > 0) { |
||
171 | echo ' STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL; |
||
172 | } else { |
||
173 | echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | echo "\033[0m".str_repeat('-', $width).PHP_EOL; |
||
178 | |||
179 | $fixableSources = 0; |
||
180 | |||
181 | View Code Duplication | if ($showSources === true) { |
|
182 | $maxSniffWidth = ($width - 7); |
||
183 | } else { |
||
184 | $maxSniffWidth = ($width - 37); |
||
185 | } |
||
186 | |||
187 | if ($totalFixable > 0) { |
||
188 | $maxSniffWidth -= 4; |
||
189 | } |
||
190 | |||
191 | foreach ($this->_sourceCache as $source => $sourceData) { |
||
192 | View Code Duplication | if ($totalFixable > 0) { |
|
193 | echo '['; |
||
194 | if ($sourceData['fixable'] === true) { |
||
195 | echo 'x'; |
||
196 | $fixableSources++; |
||
197 | } else { |
||
198 | echo ' '; |
||
199 | } |
||
200 | |||
201 | echo '] '; |
||
202 | } |
||
203 | |||
204 | if ($showSources === true) { |
||
205 | if ($sourceData['strlen'] > $maxSniffWidth) { |
||
206 | $source = substr($source, 0, $maxSniffWidth); |
||
207 | } |
||
208 | |||
209 | echo $source; |
||
210 | View Code Duplication | if ($totalFixable > 0) { |
|
211 | echo str_repeat(' ', ($width - 9 - strlen($source))); |
||
212 | } else { |
||
213 | echo str_repeat(' ', ($width - 5 - strlen($source))); |
||
214 | } |
||
215 | } else { |
||
216 | $parts = $sourceData['parts']; |
||
217 | |||
218 | if (strlen($parts[0]) > 8) { |
||
219 | $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1)); |
||
220 | } |
||
221 | |||
222 | echo $parts[0].str_repeat(' ', (10 - strlen($parts[0]))); |
||
223 | |||
224 | $category = $parts[1]; |
||
225 | if (strlen($category) > 18) { |
||
226 | $category = substr($category, 0, ((strlen($category) - 18) * -1)); |
||
227 | } |
||
228 | |||
229 | echo $category.str_repeat(' ', (20 - strlen($category))); |
||
230 | |||
231 | $sniff = $parts[2]; |
||
232 | if (strlen($sniff) > $maxSniffWidth) { |
||
233 | $sniff = substr($sniff, 0, $maxSniffWidth); |
||
234 | } |
||
235 | |||
236 | View Code Duplication | if ($totalFixable > 0) { |
|
237 | echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff))); |
||
238 | } else { |
||
239 | echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff))); |
||
240 | } |
||
241 | }//end if |
||
242 | |||
243 | echo $sourceData['count'].PHP_EOL; |
||
244 | }//end foreach |
||
245 | |||
246 | echo str_repeat('-', $width).PHP_EOL; |
||
247 | echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION'; |
||
248 | if (($totalErrors + $totalWarnings) > 1) { |
||
249 | echo 'S'; |
||
250 | } |
||
251 | |||
252 | echo ' WERE FOUND IN '.count($this->_sourceCache).' SOURCE'; |
||
253 | if (count($this->_sourceCache) !== 1) { |
||
254 | echo 'S'; |
||
255 | } |
||
256 | |||
257 | echo "\033[0m"; |
||
258 | |||
259 | View Code Duplication | if ($totalFixable > 0) { |
|
260 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
||
261 | echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
||
262 | } |
||
263 | |||
264 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
||
265 | |||
266 | if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) { |
||
267 | PHP_CodeSniffer_Reporting::printRunTime(); |
||
268 | } |
||
269 | |||
270 | }//end generate() |
||
271 | |||
335 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.