Conditions | 17 |
Paths | 2500 |
Total Lines | 125 |
Code Lines | 83 |
Lines | 76 |
Ratio | 60.8 % |
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 |
||
118 | protected function printCodeComparisonBlock(\DOMNode $node) |
||
119 | { |
||
120 | $codeBlocks = $node->getElementsByTagName('code'); |
||
121 | $first = trim($codeBlocks->item(0)->nodeValue); |
||
122 | $firstTitle = $codeBlocks->item(0)->getAttribute('title'); |
||
123 | |||
124 | $firstTitleLines = array(); |
||
125 | $tempTitle = ''; |
||
126 | $words = explode(' ', $firstTitle); |
||
127 | |||
128 | View Code Duplication | foreach ($words as $word) { |
|
129 | if (strlen($tempTitle.$word) >= 45) { |
||
130 | if (strlen($tempTitle.$word) === 45) { |
||
131 | // Adding the extra space will push us to the edge |
||
132 | // so we are done. |
||
133 | $firstTitleLines[] = $tempTitle.$word; |
||
134 | $tempTitle = ''; |
||
135 | } else if (strlen($tempTitle.$word) === 46) { |
||
136 | // We are already at the edge, so we are done. |
||
137 | $firstTitleLines[] = $tempTitle.$word; |
||
138 | $tempTitle = ''; |
||
139 | } else { |
||
140 | $firstTitleLines[] = $tempTitle; |
||
141 | $tempTitle = $word; |
||
142 | } |
||
143 | } else { |
||
144 | $tempTitle .= $word.' '; |
||
145 | } |
||
146 | }//end foreach |
||
147 | |||
148 | if ($tempTitle !== '') { |
||
149 | $firstTitleLines[] = $tempTitle; |
||
150 | } |
||
151 | |||
152 | $first = str_replace('<em>', '', $first); |
||
153 | $first = str_replace('</em>', '', $first); |
||
154 | $firstLines = explode("\n", $first); |
||
155 | |||
156 | $second = trim($codeBlocks->item(1)->nodeValue); |
||
157 | $secondTitle = $codeBlocks->item(1)->getAttribute('title'); |
||
158 | |||
159 | $secondTitleLines = array(); |
||
160 | $tempTitle = ''; |
||
161 | $words = explode(' ', $secondTitle); |
||
162 | |||
163 | View Code Duplication | foreach ($words as $word) { |
|
164 | if (strlen($tempTitle.$word) >= 45) { |
||
165 | if (strlen($tempTitle.$word) === 45) { |
||
166 | // Adding the extra space will push us to the edge |
||
167 | // so we are done. |
||
168 | $secondTitleLines[] = $tempTitle.$word; |
||
169 | $tempTitle = ''; |
||
170 | } else if (strlen($tempTitle.$word) === 46) { |
||
171 | // We are already at the edge, so we are done. |
||
172 | $secondTitleLines[] = $tempTitle.$word; |
||
173 | $tempTitle = ''; |
||
174 | } else { |
||
175 | $secondTitleLines[] = $tempTitle; |
||
176 | $tempTitle = $word; |
||
177 | } |
||
178 | } else { |
||
179 | $tempTitle .= $word.' '; |
||
180 | } |
||
181 | }//end foreach |
||
182 | |||
183 | if ($tempTitle !== '') { |
||
184 | $secondTitleLines[] = $tempTitle; |
||
185 | } |
||
186 | |||
187 | $second = str_replace('<em>', '', $second); |
||
188 | $second = str_replace('</em>', '', $second); |
||
189 | $secondLines = explode("\n", $second); |
||
190 | |||
191 | $maxCodeLines = max(count($firstLines), count($secondLines)); |
||
192 | $maxTitleLines = max(count($firstTitleLines), count($secondTitleLines)); |
||
193 | |||
194 | echo str_repeat('-', 41); |
||
195 | echo ' CODE COMPARISON '; |
||
196 | echo str_repeat('-', 42).PHP_EOL; |
||
197 | |||
198 | View Code Duplication | for ($i = 0; $i < $maxTitleLines; $i++) { |
|
199 | if (isset($firstTitleLines[$i]) === true) { |
||
200 | $firstLineText = $firstTitleLines[$i]; |
||
201 | } else { |
||
202 | $firstLineText = ''; |
||
203 | } |
||
204 | |||
205 | if (isset($secondTitleLines[$i]) === true) { |
||
206 | $secondLineText = $secondTitleLines[$i]; |
||
207 | } else { |
||
208 | $secondLineText = ''; |
||
209 | } |
||
210 | |||
211 | echo '| '; |
||
212 | echo $firstLineText.str_repeat(' ', (46 - strlen($firstLineText))); |
||
213 | echo ' | '; |
||
214 | echo $secondLineText.str_repeat(' ', (47 - strlen($secondLineText))); |
||
215 | echo ' |'.PHP_EOL; |
||
216 | }//end for |
||
217 | |||
218 | echo str_repeat('-', 100).PHP_EOL; |
||
219 | |||
220 | View Code Duplication | for ($i = 0; $i < $maxCodeLines; $i++) { |
|
221 | if (isset($firstLines[$i]) === true) { |
||
222 | $firstLineText = $firstLines[$i]; |
||
223 | } else { |
||
224 | $firstLineText = ''; |
||
225 | } |
||
226 | |||
227 | if (isset($secondLines[$i]) === true) { |
||
228 | $secondLineText = $secondLines[$i]; |
||
229 | } else { |
||
230 | $secondLineText = ''; |
||
231 | } |
||
232 | |||
233 | echo '| '; |
||
234 | echo $firstLineText.str_repeat(' ', (47 - strlen($firstLineText))); |
||
235 | echo '| '; |
||
236 | echo $secondLineText.str_repeat(' ', (48 - strlen($secondLineText))); |
||
237 | echo '|'.PHP_EOL; |
||
238 | }//end for |
||
239 | |||
240 | echo str_repeat('-', 100).PHP_EOL.PHP_EOL; |
||
241 | |||
242 | }//end printCodeComparisonBlock() |
||
243 | |||
246 |
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.