Passed
Pull Request — master (#5)
by Cody
03:17
created

Af_Comics_Pa::process()   C

Complexity

Conditions 15
Paths 37

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 32
c 0
b 0
f 0
nc 37
nop 1
dl 0
loc 58
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
class Af_Comics_Pa extends Af_ComicFilter {
3
4
	public function supported() {
5
		return array("Penny Arcade");
6
	}
7
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
	}
67
}
68