| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 152 |
| Code Lines | 97 |
| Lines | 23 |
| Ratio | 15.13 % |
| 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 |
||
| 168 | public function generate( |
||
| 169 | $cachedData, |
||
| 170 | $totalFiles, |
||
| 171 | $totalErrors, |
||
| 172 | $totalWarnings, |
||
| 173 | $totalFixable, |
||
| 174 | $showSources=false, |
||
| 175 | $width=80, |
||
| 176 | $toScreen=true |
||
| 177 | ) { |
||
| 178 | $errorsShown = ($totalErrors + $totalWarnings); |
||
| 179 | if ($errorsShown === 0) { |
||
| 180 | // Nothing to show. |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Make sure the report width isn't too big. |
||
| 185 | $maxLength = 0; |
||
| 186 | foreach ($this->_authorCache as $author => $count) { |
||
| 187 | $maxLength = max($maxLength, strlen($author)); |
||
| 188 | if ($showSources === true && isset($this->_sourceCache[$author]) === true) { |
||
| 189 | foreach ($this->_sourceCache[$author] as $source => $sourceData) { |
||
| 190 | if ($source === 'count') { |
||
| 191 | continue; |
||
| 192 | } |
||
| 193 | |||
| 194 | $maxLength = max($maxLength, (strlen($source) + 9)); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | $width = min($width, ($maxLength + 30)); |
||
| 200 | $width = max($width, 70); |
||
| 201 | arsort($this->_authorCache); |
||
| 202 | |||
| 203 | echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; |
||
| 204 | echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
||
| 205 | if ($showSources === true) { |
||
| 206 | echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
||
| 207 | echo str_repeat('-', $width).PHP_EOL; |
||
| 208 | } else { |
||
| 209 | echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
||
| 210 | echo str_repeat('-', $width).PHP_EOL; |
||
| 211 | } |
||
| 212 | |||
| 213 | echo "\033[0m"; |
||
| 214 | |||
| 215 | if ($showSources === true) { |
||
| 216 | $maxSniffWidth = ($width - 15); |
||
| 217 | |||
| 218 | if ($totalFixable > 0) { |
||
| 219 | $maxSniffWidth -= 4; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | $fixableSources = 0; |
||
| 224 | |||
| 225 | foreach ($this->_authorCache as $author => $count) { |
||
| 226 | if ($this->_praiseCache[$author]['good'] === 0) { |
||
| 227 | $percent = 0; |
||
| 228 | } else { |
||
| 229 | $total = ($this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good']); |
||
| 230 | $percent = round(($this->_praiseCache[$author]['bad'] / $total * 100), 2); |
||
| 231 | } |
||
| 232 | |||
| 233 | $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; |
||
| 234 | $authorPercent = '('.$percent.')'; |
||
| 235 | $line = str_repeat(' ', (6 - strlen($count))).$count; |
||
| 236 | $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; |
||
| 237 | $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; |
||
| 238 | $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; |
||
| 239 | |||
| 240 | if ($showSources === true) { |
||
| 241 | $line = "\033[1m$line\033[0m"; |
||
| 242 | } |
||
| 243 | |||
| 244 | echo $line.PHP_EOL; |
||
| 245 | |||
| 246 | if ($showSources === true && isset($this->_sourceCache[$author]) === true) { |
||
| 247 | $errors = $this->_sourceCache[$author]; |
||
| 248 | asort($errors); |
||
| 249 | $errors = array_reverse($errors); |
||
| 250 | |||
| 251 | foreach ($errors as $source => $sourceData) { |
||
| 252 | if ($source === 'count') { |
||
| 253 | continue; |
||
| 254 | } |
||
| 255 | |||
| 256 | $count = $sourceData['count']; |
||
| 257 | |||
| 258 | $srcLength = strlen($source); |
||
| 259 | if ($srcLength > $maxSniffWidth) { |
||
| 260 | $source = substr($source, 0, $maxSniffWidth); |
||
| 261 | } |
||
| 262 | |||
| 263 | $line = str_repeat(' ', (5 - strlen($count))).$count; |
||
| 264 | |||
| 265 | echo ' '; |
||
| 266 | if ($totalFixable > 0) { |
||
| 267 | echo '['; |
||
| 268 | if ($sourceData['fixable'] === true) { |
||
| 269 | echo 'x'; |
||
| 270 | $fixableSources++; |
||
| 271 | } else { |
||
| 272 | echo ' '; |
||
| 273 | } |
||
| 274 | |||
| 275 | echo '] '; |
||
| 276 | } |
||
| 277 | |||
| 278 | echo $source; |
||
| 279 | if ($totalFixable > 0) { |
||
| 280 | echo str_repeat(' ', ($width - 18 - strlen($source))); |
||
| 281 | } else { |
||
| 282 | echo str_repeat(' ', ($width - 14 - strlen($source))); |
||
| 283 | } |
||
| 284 | |||
| 285 | echo $line.PHP_EOL; |
||
| 286 | }//end foreach |
||
| 287 | }//end if |
||
| 288 | }//end foreach |
||
| 289 | |||
| 290 | echo str_repeat('-', $width).PHP_EOL; |
||
| 291 | echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; |
||
| 292 | if ($errorsShown !== 1) { |
||
| 293 | echo 'S'; |
||
| 294 | } |
||
| 295 | |||
| 296 | echo ' WERE COMMITTED BY '.count($this->_authorCache).' AUTHOR'; |
||
| 297 | if (count($this->_authorCache) !== 1) { |
||
| 298 | echo 'S'; |
||
| 299 | } |
||
| 300 | |||
| 301 | echo "\033[0m"; |
||
| 302 | |||
| 303 | if ($totalFixable > 0) { |
||
| 304 | if ($showSources === true) { |
||
| 305 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
||
| 306 | echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
||
| 307 | } else { |
||
| 308 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
||
| 309 | echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
||
| 314 | |||
| 315 | if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) { |
||
| 316 | PHP_CodeSniffer_Reporting::printRunTime(); |
||
| 317 | } |
||
| 318 | |||
| 319 | }//end generate() |
||
| 320 | |||
| 343 |