| 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 |
||
| 138 | protected function printCodeComparisonBlock($node) |
||
| 139 | { |
||
| 140 | $codeBlocks = $node->getElementsByTagName('code'); |
||
| 141 | $first = trim($codeBlocks->item(0)->nodeValue); |
||
| 142 | $firstTitle = $codeBlocks->item(0)->getAttribute('title'); |
||
| 143 | |||
| 144 | $firstTitleLines = array(); |
||
| 145 | $tempTitle = ''; |
||
| 146 | $words = explode(' ', $firstTitle); |
||
| 147 | |||
| 148 | foreach ($words as $word) { |
||
| 149 | if (strlen($tempTitle.$word) >= 45) { |
||
| 150 | if (strlen($tempTitle.$word) === 45) { |
||
| 151 | // Adding the extra space will push us to the edge |
||
| 152 | // so we are done. |
||
| 153 | $firstTitleLines[] = $tempTitle.$word; |
||
| 154 | $tempTitle = ''; |
||
| 155 | } else if (strlen($tempTitle.$word) === 46) { |
||
| 156 | // We are already at the edge, so we are done. |
||
| 157 | $firstTitleLines[] = $tempTitle.$word; |
||
| 158 | $tempTitle = ''; |
||
| 159 | } else { |
||
| 160 | $firstTitleLines[] = $tempTitle; |
||
| 161 | $tempTitle = $word; |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | $tempTitle .= $word.' '; |
||
| 165 | } |
||
| 166 | }//end foreach |
||
| 167 | |||
| 168 | if ($tempTitle !== '') { |
||
| 169 | $firstTitleLines[] = $tempTitle; |
||
| 170 | } |
||
| 171 | |||
| 172 | $first = str_replace('<em>', '', $first); |
||
| 173 | $first = str_replace('</em>', '', $first); |
||
| 174 | $firstLines = explode("\n", $first); |
||
| 175 | |||
| 176 | $second = trim($codeBlocks->item(1)->nodeValue); |
||
| 177 | $secondTitle = $codeBlocks->item(1)->getAttribute('title'); |
||
| 178 | |||
| 179 | $secondTitleLines = array(); |
||
| 180 | $tempTitle = ''; |
||
| 181 | $words = explode(' ', $secondTitle); |
||
| 182 | |||
| 183 | foreach ($words as $word) { |
||
| 184 | if (strlen($tempTitle.$word) >= 45) { |
||
| 185 | if (strlen($tempTitle.$word) === 45) { |
||
| 186 | // Adding the extra space will push us to the edge |
||
| 187 | // so we are done. |
||
| 188 | $secondTitleLines[] = $tempTitle.$word; |
||
| 189 | $tempTitle = ''; |
||
| 190 | } else if (strlen($tempTitle.$word) === 46) { |
||
| 191 | // We are already at the edge, so we are done. |
||
| 192 | $secondTitleLines[] = $tempTitle.$word; |
||
| 193 | $tempTitle = ''; |
||
| 194 | } else { |
||
| 195 | $secondTitleLines[] = $tempTitle; |
||
| 196 | $tempTitle = $word; |
||
| 197 | } |
||
| 198 | } else { |
||
| 199 | $tempTitle .= $word.' '; |
||
| 200 | } |
||
| 201 | }//end foreach |
||
| 202 | |||
| 203 | if ($tempTitle !== '') { |
||
| 204 | $secondTitleLines[] = $tempTitle; |
||
| 205 | } |
||
| 206 | |||
| 207 | $second = str_replace('<em>', '', $second); |
||
| 208 | $second = str_replace('</em>', '', $second); |
||
| 209 | $secondLines = explode("\n", $second); |
||
| 210 | |||
| 211 | $maxCodeLines = max(count($firstLines), count($secondLines)); |
||
| 212 | $maxTitleLines = max(count($firstTitleLines), count($secondTitleLines)); |
||
| 213 | |||
| 214 | echo str_repeat('-', 41); |
||
| 215 | echo ' CODE COMPARISON '; |
||
| 216 | echo str_repeat('-', 42).PHP_EOL; |
||
| 217 | |||
| 218 | for ($i = 0; $i < $maxTitleLines; $i++) { |
||
| 219 | if (isset($firstTitleLines[$i]) === true) { |
||
| 220 | $firstLineText = $firstTitleLines[$i]; |
||
| 221 | } else { |
||
| 222 | $firstLineText = ''; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (isset($secondTitleLines[$i]) === true) { |
||
| 226 | $secondLineText = $secondTitleLines[$i]; |
||
| 227 | } else { |
||
| 228 | $secondLineText = ''; |
||
| 229 | } |
||
| 230 | |||
| 231 | echo '| '; |
||
| 232 | echo $firstLineText.str_repeat(' ', (46 - strlen($firstLineText))); |
||
| 233 | echo ' | '; |
||
| 234 | echo $secondLineText.str_repeat(' ', (47 - strlen($secondLineText))); |
||
| 235 | echo ' |'.PHP_EOL; |
||
| 236 | }//end for |
||
| 237 | |||
| 238 | echo str_repeat('-', 100).PHP_EOL; |
||
| 239 | |||
| 240 | for ($i = 0; $i < $maxCodeLines; $i++) { |
||
| 241 | if (isset($firstLines[$i]) === true) { |
||
| 242 | $firstLineText = $firstLines[$i]; |
||
| 243 | } else { |
||
| 244 | $firstLineText = ''; |
||
| 245 | } |
||
| 246 | |||
| 247 | if (isset($secondLines[$i]) === true) { |
||
| 248 | $secondLineText = $secondLines[$i]; |
||
| 249 | } else { |
||
| 250 | $secondLineText = ''; |
||
| 251 | } |
||
| 252 | |||
| 253 | echo '| '; |
||
| 254 | echo $firstLineText.str_repeat(' ', (47 - strlen($firstLineText))); |
||
| 255 | echo '| '; |
||
| 256 | echo $secondLineText.str_repeat(' ', (48 - strlen($secondLineText))); |
||
| 257 | echo '|'.PHP_EOL; |
||
| 258 | }//end for |
||
| 259 | |||
| 260 | echo str_repeat('-', 100).PHP_EOL.PHP_EOL; |
||
| 261 | |||
| 262 | }//end printCodeComparisonBlock() |
||
| 263 | |||
| 266 |