| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 188 |
| 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:
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 |
||
| 83 | public function generate( |
||
| 84 | $cachedData, |
||
| 85 | $totalFiles, |
||
| 86 | $totalErrors, |
||
| 87 | $totalWarnings, |
||
| 88 | $totalFixable, |
||
| 89 | $showSources=false, |
||
| 90 | $width=80, |
||
| 91 | $interactive=false, |
||
| 92 | $toScreen=true |
||
| 93 | ) { |
||
| 94 | $lines = explode(PHP_EOL, $cachedData); |
||
| 95 | array_pop($lines); |
||
| 96 | |||
| 97 | if (empty($lines) === true) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | $sources = []; |
||
| 102 | $maxLength = 0; |
||
| 103 | |||
| 104 | foreach ($lines as $line) { |
||
| 105 | $parts = explode('>>', $line); |
||
| 106 | $source = $parts[0]; |
||
| 107 | $fixable = (bool) $parts[1]; |
||
| 108 | $count = $parts[2]; |
||
| 109 | |||
| 110 | if (isset($sources[$source]) === false) { |
||
| 111 | if ($showSources === true) { |
||
| 112 | $parts = null; |
||
| 113 | $sniff = $source; |
||
| 114 | } else { |
||
| 115 | $parts = explode('.', $source); |
||
| 116 | if ($parts[0] === 'Internal') { |
||
| 117 | $parts[2] = $parts[1]; |
||
| 118 | $parts[1] = ''; |
||
| 119 | } |
||
| 120 | |||
| 121 | $parts[1] = $this->makeFriendlyName($parts[1]); |
||
| 122 | |||
| 123 | $sniff = $this->makeFriendlyName($parts[2]); |
||
| 124 | if (isset($parts[3]) === true) { |
||
| 125 | $name = $this->makeFriendlyName($parts[3]); |
||
| 126 | $name[0] = strtolower($name[0]); |
||
| 127 | $sniff .= ' '.$name; |
||
| 128 | unset($parts[3]); |
||
| 129 | } |
||
| 130 | |||
| 131 | $parts[2] = $sniff; |
||
| 132 | }//end if |
||
| 133 | |||
| 134 | $maxLength = max($maxLength, strlen($sniff)); |
||
| 135 | |||
| 136 | $sources[$source] = [ |
||
| 137 | 'count' => $count, |
||
| 138 | 'fixable' => $fixable, |
||
| 139 | 'parts' => $parts, |
||
| 140 | ]; |
||
| 141 | } else { |
||
| 142 | $sources[$source]['count'] += $count; |
||
| 143 | }//end if |
||
| 144 | |||
| 145 | $fileLen = strlen($parts[0]); |
||
| 146 | $reportFiles[$parts[0]] = [ |
||
| 147 | 'errors' => $parts[1], |
||
| 148 | 'warnings' => $parts[2], |
||
| 149 | 'strlen' => $fileLen, |
||
| 150 | ]; |
||
| 151 | }//end foreach |
||
| 152 | |||
| 153 | if ($showSources === true) { |
||
| 154 | $width = min($width, ($maxLength + 11)); |
||
| 155 | } else { |
||
| 156 | $width = min($width, ($maxLength + 41)); |
||
| 157 | } |
||
| 158 | |||
| 159 | $width = max($width, 70); |
||
| 160 | |||
| 161 | asort($sources); |
||
| 162 | $sources = array_reverse($sources); |
||
| 163 | |||
| 164 | echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL; |
||
| 165 | echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
||
| 166 | if ($showSources === true) { |
||
| 167 | if ($totalFixable > 0) { |
||
| 168 | echo ' SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL; |
||
| 169 | } else { |
||
| 170 | echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL; |
||
| 171 | } |
||
| 172 | } else { |
||
| 173 | if ($totalFixable > 0) { |
||
| 174 | echo ' STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL; |
||
| 175 | } else { |
||
| 176 | echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | echo "\033[0m".str_repeat('-', $width).PHP_EOL; |
||
| 181 | |||
| 182 | $fixableSources = 0; |
||
| 183 | |||
| 184 | if ($showSources === true) { |
||
| 185 | $maxSniffWidth = ($width - 7); |
||
| 186 | } else { |
||
| 187 | $maxSniffWidth = ($width - 37); |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($totalFixable > 0) { |
||
| 191 | $maxSniffWidth -= 4; |
||
| 192 | } |
||
| 193 | |||
| 194 | foreach ($sources as $source => $sourceData) { |
||
| 195 | if ($totalFixable > 0) { |
||
| 196 | echo '['; |
||
| 197 | if ($sourceData['fixable'] === true) { |
||
| 198 | echo 'x'; |
||
| 199 | $fixableSources++; |
||
| 200 | } else { |
||
| 201 | echo ' '; |
||
| 202 | } |
||
| 203 | |||
| 204 | echo '] '; |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($showSources === true) { |
||
| 208 | if (strlen($source) > $maxSniffWidth) { |
||
| 209 | $source = substr($source, 0, $maxSniffWidth); |
||
| 210 | } |
||
| 211 | |||
| 212 | echo $source; |
||
| 213 | if ($totalFixable > 0) { |
||
| 214 | echo str_repeat(' ', ($width - 9 - strlen($source))); |
||
| 215 | } else { |
||
| 216 | echo str_repeat(' ', ($width - 5 - strlen($source))); |
||
| 217 | } |
||
| 218 | } else { |
||
| 219 | $parts = $sourceData['parts']; |
||
| 220 | |||
| 221 | if (strlen($parts[0]) > 8) { |
||
| 222 | $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1)); |
||
| 223 | } |
||
| 224 | |||
| 225 | echo $parts[0].str_repeat(' ', (10 - strlen($parts[0]))); |
||
| 226 | |||
| 227 | $category = $parts[1]; |
||
| 228 | if (strlen($category) > 18) { |
||
| 229 | $category = substr($category, 0, ((strlen($category) - 18) * -1)); |
||
| 230 | } |
||
| 231 | |||
| 232 | echo $category.str_repeat(' ', (20 - strlen($category))); |
||
| 233 | |||
| 234 | $sniff = $parts[2]; |
||
| 235 | if (strlen($sniff) > $maxSniffWidth) { |
||
| 236 | $sniff = substr($sniff, 0, $maxSniffWidth); |
||
| 237 | } |
||
| 238 | |||
| 239 | if ($totalFixable > 0) { |
||
| 240 | echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff))); |
||
| 241 | } else { |
||
| 242 | echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff))); |
||
| 243 | } |
||
| 244 | }//end if |
||
| 245 | |||
| 246 | echo $sourceData['count'].PHP_EOL; |
||
| 247 | }//end foreach |
||
| 248 | |||
| 249 | echo str_repeat('-', $width).PHP_EOL; |
||
| 250 | echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION'; |
||
| 251 | if (($totalErrors + $totalWarnings) > 1) { |
||
| 252 | echo 'S'; |
||
| 253 | } |
||
| 254 | |||
| 255 | echo ' WERE FOUND IN '.count($sources).' SOURCE'; |
||
| 256 | if (count($sources) !== 1) { |
||
| 257 | echo 'S'; |
||
| 258 | } |
||
| 259 | |||
| 260 | echo "\033[0m"; |
||
| 261 | |||
| 262 | if ($totalFixable > 0) { |
||
| 263 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
||
| 264 | echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
||
| 265 | } |
||
| 266 | |||
| 267 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
||
| 268 | |||
| 269 | if ($toScreen === true && $interactive === false) { |
||
| 270 | Timing::printRunTime(); |
||
| 271 | } |
||
| 338 |