Conditions | 25 |
Paths | > 20000 |
Total Lines | 143 |
Code Lines | 85 |
Lines | 68 |
Ratio | 47.55 % |
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 |
||
33 | public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
||
34 | { |
||
35 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
||
36 | // Nothing to print. |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | // The length of the word ERROR or WARNING; used for padding. |
||
41 | if ($report['warnings'] > 0) { |
||
42 | $typeLength = 7; |
||
43 | } else { |
||
44 | $typeLength = 5; |
||
45 | } |
||
46 | |||
47 | // Work out the max line number length for formatting. |
||
48 | $maxLineNumLength = max(array_map('strlen', array_keys($report['messages']))); |
||
49 | |||
50 | // The padding that all lines will require that are |
||
51 | // printing an error message overflow. |
||
52 | $paddingLine2 = str_repeat(' ', ($maxLineNumLength + 1)); |
||
53 | $paddingLine2 .= ' | '; |
||
54 | $paddingLine2 .= str_repeat(' ', $typeLength); |
||
55 | $paddingLine2 .= ' | '; |
||
56 | if ($report['fixable'] > 0) { |
||
57 | $paddingLine2 .= ' '; |
||
58 | } |
||
59 | |||
60 | $paddingLength = strlen($paddingLine2); |
||
61 | |||
62 | // Make sure the report width isn't too big. |
||
63 | $maxErrorLength = 0; |
||
64 | View Code Duplication | foreach ($report['messages'] as $line => $lineErrors) { |
|
|
|||
65 | foreach ($lineErrors as $column => $colErrors) { |
||
66 | foreach ($colErrors as $error) { |
||
67 | $length = strlen($error['message']); |
||
68 | if ($showSources === true) { |
||
69 | $length += (strlen($error['source']) + 3); |
||
70 | } |
||
71 | |||
72 | $maxErrorLength = max($maxErrorLength, ($length + 1)); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | $file = $report['filename']; |
||
78 | $fileLength = strlen($file); |
||
79 | $maxWidth = max(($fileLength + 6), ($maxErrorLength + $paddingLength)); |
||
80 | $width = min($width, $maxWidth); |
||
81 | if ($width < 70) { |
||
82 | $width = 70; |
||
83 | } |
||
84 | |||
85 | echo PHP_EOL."\033[1mFILE: "; |
||
86 | View Code Duplication | if ($fileLength <= ($width - 6)) { |
|
87 | echo $file; |
||
88 | } else { |
||
89 | echo '...'.substr($file, ($fileLength - ($width - 6))); |
||
90 | } |
||
91 | |||
92 | echo "\033[0m".PHP_EOL; |
||
93 | echo str_repeat('-', $width).PHP_EOL; |
||
94 | |||
95 | echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; |
||
96 | if ($report['errors'] !== 1) { |
||
97 | echo 'S'; |
||
98 | } |
||
99 | |||
100 | View Code Duplication | if ($report['warnings'] > 0) { |
|
101 | echo ' AND '.$report['warnings'].' WARNING'; |
||
102 | if ($report['warnings'] !== 1) { |
||
103 | echo 'S'; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | echo ' AFFECTING '.count($report['messages']).' LINE'; |
||
108 | if (count($report['messages']) !== 1) { |
||
109 | echo 'S'; |
||
110 | } |
||
111 | |||
112 | echo "\033[0m".PHP_EOL; |
||
113 | echo str_repeat('-', $width).PHP_EOL; |
||
114 | |||
115 | // The maximum amount of space an error message can use. |
||
116 | $maxErrorSpace = ($width - $paddingLength - 1); |
||
117 | if ($showSources === true) { |
||
118 | // Account for the chars used to print colors. |
||
119 | $maxErrorSpace += 8; |
||
120 | } |
||
121 | |||
122 | foreach ($report['messages'] as $line => $lineErrors) { |
||
123 | View Code Duplication | foreach ($lineErrors as $column => $colErrors) { |
|
124 | foreach ($colErrors as $error) { |
||
125 | $message = $error['message']; |
||
126 | $message = str_replace("\n", "\n".$paddingLine2, $message); |
||
127 | if ($showSources === true) { |
||
128 | $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; |
||
129 | } |
||
130 | |||
131 | // The padding that goes on the front of the line. |
||
132 | $padding = ($maxLineNumLength - strlen($line)); |
||
133 | $errorMsg = wordwrap( |
||
134 | $message, |
||
135 | $maxErrorSpace, |
||
136 | PHP_EOL.$paddingLine2 |
||
137 | ); |
||
138 | |||
139 | echo ' '.str_repeat(' ', $padding).$line.' | '; |
||
140 | if ($error['type'] === 'ERROR') { |
||
141 | echo "\033[31mERROR\033[0m"; |
||
142 | if ($report['warnings'] > 0) { |
||
143 | echo ' '; |
||
144 | } |
||
145 | } else { |
||
146 | echo "\033[33mWARNING\033[0m"; |
||
147 | } |
||
148 | |||
149 | echo ' | '; |
||
150 | if ($report['fixable'] > 0) { |
||
151 | echo '['; |
||
152 | if ($error['fixable'] === true) { |
||
153 | echo 'x'; |
||
154 | } else { |
||
155 | echo ' '; |
||
156 | } |
||
157 | |||
158 | echo '] '; |
||
159 | } |
||
160 | |||
161 | echo $errorMsg.PHP_EOL; |
||
162 | }//end foreach |
||
163 | }//end foreach |
||
164 | }//end foreach |
||
165 | |||
166 | echo str_repeat('-', $width).PHP_EOL; |
||
167 | View Code Duplication | if ($report['fixable'] > 0) { |
|
168 | echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; |
||
169 | echo str_repeat('-', $width).PHP_EOL; |
||
170 | } |
||
171 | |||
172 | echo PHP_EOL; |
||
173 | return true; |
||
174 | |||
175 | }//end generateFileReport() |
||
176 | |||
219 |
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.