MicrodataAdapter   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 170
Duplicated Lines 24.71 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 1
dl 42
loc 170
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A extractImage() 0 6 1
A extractDescription() 17 17 2
A extractKeywords() 11 21 3
B extractBody() 0 38 4
B extractPublishDate() 0 26 4
B extractAuthor() 0 29 3
A extractTitle() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Zrashwani\NewsScrapper\Adapters;
4
5
use \Symfony\Component\DomCrawler\Crawler;
6
7
/**
8
 * Adapter to extract news base on microdata format base on schema.org specifications
9
 * @link http://schema.org/Article schema.org NewsArticle specification
10
 * @author Zeid Rashwani <zrashwani.com>
11
 */
12
class MicrodataAdapter extends AbstractAdapter
13
{
14
15
    /**
16
     * @param Crawler $crawler
17
     * @return string
18
     */
19 View Code Duplication
    public function extractTitle(Crawler $crawler)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
20
    {
21
        $ret = null;
22
23
        $crawler->filterXPath('//*[@itemprop="headline"]')
24
            ->each(
25
                function(Crawler $node) use (&$ret) {
26
                            $ret = trim($node->text());
27
                }
28
            );
29
30
31
        return $ret;
32
    }
33
34
    public function extractImage(Crawler $crawler)
35
    {
36
        
37
        $ret = $this->getSrcByImgSelector($crawler, '//img[@itemprop="image"]');
38
        return $ret;
39
    }
40
41 View Code Duplication
    public function extractDescription(Crawler $crawler)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
42
    {
43
        $ret = null;
44
45
        $crawler->filterXPath('//*[@itemprop="description"]')
46
            ->each(
47
                function(Crawler $node) use (&$ret) {
48
                    if ($node->nodeName() === 'meta') {
49
                        $ret = trim($node->attr('content'));
50
                    } else {
51
                        $ret = trim($node->text());
52
                    }
53
                }
54
            );
55
56
        return $ret;
57
    }
58
59
    /**
60
     * extract keywords out of crawler object
61
     * @param Crawler $crawler
62
     * @return array
63
     */
64
    public function extractKeywords(Crawler $crawler)
65
    {
66
        $ret = array();
67
68
        $crawler->filterXPath('//*[@itemprop="keywords"]')
69
            ->each(
70 View Code Duplication
                function(Crawler $node) use (&$ret) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
71
                    if ($node->nodeName() === 'meta') {
72
                        $keyword_txt = trim($node->attr('content'));
73
                    } else {
74
                        $keyword_txt = trim($node->text());
75
                    }
76
77
                    if (empty($keyword_txt) !== true) {
78
                        $ret = explode(',', $keyword_txt);
79
                    }
80
                }
81
            );
82
83
        return $ret;
84
    }
85
86
    public function extractBody(Crawler $crawler)
87
    {
88
        $ret = '';
89
90
        $crawler->filterXPath('//*[@itemprop="articleBody"]')
91
            ->each(
92
                function(Crawler $node) use (&$ret) {
93
                        $ret .= $node->html();
94
                }
95
            );
96
97
        if (empty($ret) === true) {
98
            $article_types = ['Article', 'NewsArticle', 'Report', 'ScholarlyArticle',
99
                'MedicalScholarlyArticle', 'SocialMediaPosting',
100
                'BlogPosting', 'LiveBlogPosting',
101
                'DiscussionForumPosting', 'TechArticle',
102
                'APIReference'];
103
104
            foreach ($article_types as $article_type) {
105
                $crawler->filterXPath(
106
                    "//*[@itemtype='http://schema.org/$article_type']"
107
                )
108
                    ->each(
109
                        function(Crawler $node) use (&$ret) {
110
                                    $ret .= $node->html();
111
                        }
112
                    );
113
                
114
                if (empty($ret) === false) { //if content found, exit loop
115
                    break;
116
                }
117
            }
118
        }
119
120
        $ret = $this->normalizeHtml($ret);
121
        
122
        return $ret;
123
    }
124
125
    public function extractPublishDate(Crawler $crawler)
126
    {
127
        $date_str = null;
128
129
        $crawler->filterXPath('//*[@itemprop="datePublished"]')
130
            ->each(
131
                function(Crawler $node) use (&$date_str) {
132
                    if ($node->nodeName() === 'meta') {
133
                        $date_str = $node->attr('content');
134
                    } elseif ($node->attr('datetime')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $node->attr('datetime') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
135
                        $date_str = $node->attr('datetime');
136
                    } else {
137
                        $date_str = $node->text();
138
                    }
139
                }
140
            );
141
142
        if (!is_null($date_str)) {
143
            //remove extra unneeded suffix from date
144
            $date_str = str_replace('ET', '', $date_str);
145
            $ret = new \DateTime($date_str);
146
            return $ret->format(\DateTime::ISO8601);
147
        }
148
        
149
        return $date_str; //null
150
    }
151
152
    public function extractAuthor(Crawler $crawler)
153
    {
154
        $ret = null;
155
        $crawler->filterXPath(
156
            '//*[@itemprop="author" '.
157
            'and @itemtype="http://schema.org/Person"]//*[@itemprop="name"]'
158
        )
159
            ->each(
160
                function(Crawler $node) use (&$ret) {
161
                            $ret = $node->text();
162
                }
163
            );
164
165
        if (is_null($ret)) {
166
            $crawler->filterXPath('//*[@itemprop="author"]')
167
                ->each(
168
                    function(Crawler $node) use (&$ret) {
169
                        if ($node->nodeName() === 'meta') {
170
                                $ret = $node->attr('content');
171
                        } else {
172
                            $ret = $node->text();
173
                        }
174
                    }
175
                );
176
        }
177
        $ret = preg_replace('@\s{2,}@', ' ', $ret);
178
        
179
        return $ret;
180
    }
181
}
182