Conditions | 15 |
Paths | 37 |
Total Lines | 58 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
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 |
||
8 | public function process(&$article) { |
||
9 | if (strpos($article["link"], "penny-arcade.com") !== FALSE && strpos($article["title"], "Comic:") !== FALSE) { |
||
10 | |||
11 | $doc = new DOMDocument(); |
||
12 | |||
13 | if ($doc->loadHTML(fetch_file_contents($article["link"]))) { |
||
14 | $xpath = new DOMXPath($doc); |
||
15 | $basenode = $xpath->query('(//div[@id="comicFrame"])')->item(0); |
||
16 | |||
17 | if ($basenode) { |
||
18 | $article["content"] = $doc->saveHTML($basenode); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | return true; |
||
23 | } |
||
24 | |||
25 | if (strpos($article["link"], "penny-arcade.com") !== FALSE && strpos($article["title"], "News Post:") !== FALSE) { |
||
26 | $doc = new DOMDocument(); |
||
27 | |||
28 | if ($doc->loadHTML(fetch_file_contents($article["link"]))) { |
||
29 | $xpath = new DOMXPath($doc); |
||
30 | $entries = $xpath->query('(//div[@class="post"])'); |
||
31 | |||
32 | $basenode = false; |
||
33 | |||
34 | foreach ($entries as $entry) { |
||
35 | $basenode = $entry; |
||
36 | } |
||
37 | |||
38 | $meta = $xpath->query('(//div[@class="meta"])')->item(0); |
||
39 | if ($meta->parentNode) { $meta->parentNode->removeChild($meta); } |
||
40 | |||
41 | $header = $xpath->query('(//div[@class="postBody"]/h2)')->item(0); |
||
42 | if ($header->parentNode) { $header->parentNode->removeChild($header); } |
||
43 | |||
44 | $header = $xpath->query('(//div[@class="postBody"]/div[@class="comicPost"])')->item(0); |
||
45 | if ($header->parentNode) { $header->parentNode->removeChild($header); } |
||
46 | |||
47 | $avatar = $xpath->query('(//div[@class="avatar"]//img)')->item(0); |
||
48 | |||
49 | if ($basenode) |
||
50 | $basenode->insertBefore($avatar, $basenode->firstChild); |
||
51 | |||
52 | $uninteresting = $xpath->query('(//div[@class="avatar"])'); |
||
53 | foreach ($uninteresting as $i) { |
||
54 | $i->parentNode->removeChild($i); |
||
55 | } |
||
56 | |||
57 | if ($basenode){ |
||
58 | $article["content"] = $doc->saveHTML($basenode); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | return true; |
||
63 | } |
||
64 | |||
65 | return false; |
||
66 | } |
||
68 |