Conditions | 13 |
Paths | 577 |
Total Lines | 95 |
Code Lines | 63 |
Lines | 4 |
Ratio | 4.21 % |
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 |
||
65 | public function generate( |
||
66 | $cachedData, |
||
67 | $totalFiles, |
||
68 | $totalErrors, |
||
69 | $totalWarnings, |
||
70 | $totalFixable, |
||
71 | $showSources=false, |
||
72 | $width=80, |
||
73 | $interactive=false, |
||
74 | $toScreen=true |
||
75 | ) { |
||
76 | $lines = explode(PHP_EOL, $cachedData); |
||
77 | array_pop($lines); |
||
78 | |||
79 | if (empty($lines) === true) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | $reportFiles = array(); |
||
84 | $maxLength = 0; |
||
85 | |||
86 | foreach ($lines as $line) { |
||
87 | $parts = explode('>>', $line); |
||
88 | $fileLen = strlen($parts[0]); |
||
89 | $reportFiles[$parts[0]] = array( |
||
90 | 'errors' => $parts[1], |
||
91 | 'warnings' => $parts[2], |
||
92 | 'strlen' => $fileLen, |
||
93 | ); |
||
94 | |||
95 | $maxLength = max($maxLength, $fileLen); |
||
96 | } |
||
97 | |||
98 | $width = min($width, ($maxLength + 21)); |
||
99 | $width = max($width, 70); |
||
100 | |||
101 | echo PHP_EOL."\033[1m".'PHP CODE SNIFFER REPORT SUMMARY'."\033[0m".PHP_EOL; |
||
102 | echo str_repeat('-', $width).PHP_EOL; |
||
103 | echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'ERRORS WARNINGS'."\033[0m".PHP_EOL; |
||
104 | echo str_repeat('-', $width).PHP_EOL; |
||
105 | |||
106 | foreach ($reportFiles as $file => $data) { |
||
107 | $padding = ($width - 18 - $data['strlen']); |
||
108 | if ($padding < 0) { |
||
109 | $file = '...'.substr($file, (($padding * -1) + 3)); |
||
110 | $padding = 0; |
||
111 | } |
||
112 | |||
113 | echo $file.str_repeat(' ', $padding).' '; |
||
114 | if ($data['errors'] !== 0) { |
||
115 | echo "\033[31m".$data['errors']."\033[0m"; |
||
116 | echo str_repeat(' ', (8 - strlen((string) $data['errors']))); |
||
117 | } else { |
||
118 | echo '0 '; |
||
119 | } |
||
120 | |||
121 | if ($data['warnings'] !== 0) { |
||
122 | echo "\033[33m".$data['warnings']."\033[0m"; |
||
123 | } else { |
||
124 | echo '0'; |
||
125 | } |
||
126 | |||
127 | echo PHP_EOL; |
||
128 | }//end foreach |
||
129 | |||
130 | echo str_repeat('-', $width).PHP_EOL; |
||
131 | echo "\033[1mA TOTAL OF $totalErrors ERROR"; |
||
132 | if ($totalErrors !== 1) { |
||
133 | echo 'S'; |
||
134 | } |
||
135 | |||
136 | echo ' AND '.$totalWarnings.' WARNING'; |
||
137 | if ($totalWarnings !== 1) { |
||
138 | echo 'S'; |
||
139 | } |
||
140 | |||
141 | echo ' WERE FOUND IN '.$totalFiles.' FILE'; |
||
142 | if ($totalFiles !== 1) { |
||
143 | echo 'S'; |
||
144 | } |
||
145 | |||
146 | echo "\033[0m"; |
||
147 | |||
148 | View Code Duplication | if ($totalFixable > 0) { |
|
|
|||
149 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
||
150 | echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
||
151 | } |
||
152 | |||
153 | echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
||
154 | |||
155 | if ($toScreen === true && $interactive === false) { |
||
156 | Util\Timing::printRunTime(); |
||
157 | } |
||
158 | |||
159 | }//end generate() |
||
160 | |||
163 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.