Conditions | 34 |
Paths | 1296 |
Total Lines | 80 |
Code Lines | 60 |
Lines | 14 |
Ratio | 17.5 % |
Changes | 5 | ||
Bugs | 1 | Features | 1 |
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 |
||
211 | private static function iterateSentencesOverNode(\DOMNode $node, &$sentence, array &$sentences) |
||
212 | { |
||
213 | if ($node instanceof \DOMText) { |
||
214 | $sentence .= preg_replace("/[\\t\\n\\f\\r ]+/im", " ", $node->wholeText); |
||
215 | |||
216 | return 0; |
||
217 | } |
||
218 | if ($node instanceof \DOMDocumentType) { |
||
219 | return 0; |
||
220 | } |
||
221 | |||
222 | $name = strtolower($node->nodeName); |
||
223 | if (preg_match('/^h(\d+)$/', $name)) { |
||
224 | $name = "hx"; |
||
225 | } |
||
226 | |||
227 | switch ($name) { |
||
228 | case "hr": |
||
229 | case "style": |
||
230 | case "head": |
||
231 | case "title": |
||
232 | case "meta": |
||
233 | case "script": |
||
234 | case "pre": |
||
235 | return 0; |
||
236 | |||
237 | case "p": |
||
238 | case "hx": |
||
239 | case "th": |
||
240 | case "td": |
||
241 | case "li": |
||
242 | case "label": |
||
243 | View Code Duplication | case "button": |
|
244 | $sentence = trim($sentence); |
||
245 | if (strlen($sentence) > 0) { |
||
246 | $sentences[] = $sentence; |
||
247 | } |
||
248 | $sentence = ""; |
||
249 | break; |
||
250 | |||
251 | case "br": |
||
252 | $sentence .= " "; |
||
253 | break; |
||
254 | default: |
||
255 | break; |
||
256 | } |
||
257 | |||
258 | $childs = $node->childNodes; |
||
259 | foreach ($childs as $child) { |
||
260 | static::iterateSentencesOverNode($child, $sentence, $sentences); |
||
261 | } |
||
262 | |||
263 | switch ($name) { |
||
264 | case "style": |
||
265 | case "head": |
||
266 | case "title": |
||
267 | case "meta": |
||
268 | case "script": |
||
269 | return ""; |
||
270 | |||
271 | case "p": |
||
272 | case "hx": |
||
273 | case "th": |
||
274 | case "td": |
||
275 | case "li": |
||
276 | case "label": |
||
277 | View Code Duplication | case "button": |
|
278 | $sentence = trim($sentence); |
||
279 | if (strlen($sentence) > 0) { |
||
280 | $sentences[] = $sentence; |
||
281 | } |
||
282 | $sentence = ""; |
||
283 | break; |
||
284 | |||
285 | default: |
||
286 | break; |
||
287 | } |
||
288 | |||
289 | return 0; |
||
290 | } |
||
291 | } |
||
292 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: