Completed
Pull Request — master (#145)
by
unknown
05:35
created

Alexa::getDailyPageViews()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
namespace SEOstats\Services;
3
4
/**
5
 * SEOstats extension for Alexa data.
6
 *
7
 * @package    SEOstats
8
 * @author     Stephan Schmitz <[email protected]>
9
 * @copyright  Copyright (c) 2010 - present Stephan Schmitz
10
 * @license    http://eyecatchup.mit-license.org/  MIT License
11
 * @updated    2013/08/17
12
 */
13
14
use SEOstats\SEOstats as SEOstats;
15
use SEOstats\Config as Config;
16
use SEOstats\Helper as Helper;
17
18
class Alexa extends SEOstats
19
{
20
    /**
21
     * Used for cache
22
     * @var DOMXPath
23
     */
24
    protected static $_xpath = false;
25
26
    protected static $_rankKeys = array(
27
        '1d' => 0,
28
        '7d' => 0,
29
        '1m' => 0,
30
        '3m' => 0,
31
    );
32
33
    /**
34
     * Get yesterday's rank
35
     * @return int
36
     */
37 3 View Code Duplication
    public static function getDailyRank($url = false)
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...
38
    {
39 3
        self::setRankingKeys($url);
40 3
        if (0 == self::$_rankKeys['1d']) {
41 1
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getDailyRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
42
        }
43
44 2
        $xpath = self::_getXPath($url);
45 2
        $nodes = @$xpath->query("//*[@id='rank']/table/tr[" . self::$_rankKeys['1d'] . "]/td[1]");
46
47 2
        return !$nodes->item(0) ? parent::noDataDefaultValue() :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getDailyRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
48 2
            self::retInt( strip_tags($nodes->item(0)->nodeValue) );
49
    }
50
51
    /**
52
     * For backward compatibility
53
     * @deprecated
54
     */
55 1
    public static function getWeekRank($url = false) {
56 1
        return self::getWeeklyRank($url);
57
    }
58
    /**
59
     * Get the average rank over the last 7 days
60
     * @return int
61
     */
62 4 View Code Duplication
    public static function getWeeklyRank($url = false)
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...
63
    {
64 4
        self::setRankingKeys($url);
65 4
        if (0 == self::$_rankKeys['7d']) {
66 1
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getWeeklyRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
67
        }
68
69 3
        $xpath = self::_getXPath($url);
70 3
        $nodes = @$xpath->query("//*[@id='rank']/table/tr[" . self::$_rankKeys['7d'] . "]/td[1]");
71
72 3
        return !$nodes->item(0) ? parent::noDataDefaultValue() :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getWeeklyRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
73 3
            self::retInt( strip_tags($nodes->item(0)->nodeValue) );
74
    }
75
76
    /**
77
     * For backward compatibility
78
     * @deprecated
79
     */
80 1
    public static function getMonthRank($url = false) {
81 1
        return self::getMonthlyRank($url);
82
    }
83
    /**
84
     * Get the average rank over the last month
85
     * @return int
86
     */
87 4 View Code Duplication
    public static function getMonthlyRank($url = false)
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...
88
    {
89 4
        self::setRankingKeys($url);
90 4
        if (0 == self::$_rankKeys['1m']) {
91 1
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getMonthlyRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
92
        }
93
94 3
        $xpath = self::_getXPath($url);
95 3
        $nodes = @$xpath->query("//*[@id='rank']/table/tr[" . self::$_rankKeys['1m'] . "]/td[1]");
96
97 3
        return !$nodes->item(0) ? parent::noDataDefaultValue() :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getMonthlyRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
98 3
            self::retInt( strip_tags($nodes->item(0)->nodeValue) );
99
    }
100
101
    /**
102
     * For backward compatibility
103
     * @deprecated
104
     */
105 2
    public static function getQuarterRank($url = false) {
106 2
        return self::getGlobalRank($url);
107
    }
108
    /**
109
     * Get the average rank over the last 3 months
110
     * @return int
111
     */
112 4 View Code Duplication
    public static function getGlobalRank($url = false)
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...
113
    {
114
        /*
115
        self::setRankingKeys($url);
116
        if (0 == self::$_rankKeys['3m']) {
117
            return parent::noDataDefaultValue();
118
        }
119
        */
120
121 4
        $xpath = self::_getXPath($url);
122
123
        $xpathQueryList = array(
124 4
            "//*[@id='traffic-rank-content']/div/span[2]/div[1]/span/span/div/strong",
125
            "//*[@id='traffic-rank-content']/div/span[2]/div[1]/span/span/div/strong/a"
126 4
        );
127
128 4
        return static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList);
129
    }
130
131
    /**
132
     * Get the average rank over the week
133
     * @return int
134
     */
135 11
    public static function setRankingKeys($url = false)
136
    {
137 11
        $xpath = self::_getXPath($url);
138 11
        $nodes = @$xpath->query("//*[@id='rank']/table/tr");
139
140 11
        if (5 == $nodes->length) {
141 6
            self::$_rankKeys = array(
142 6
                '1d' => 2,
143 6
                '7d' => 3,
144 6
                '1m' => 4,
145 6
                '3m' => 5,
146 6
            );
147 6
        }
148 5
        else if (4 == $nodes->length) {
149 1
            self::$_rankKeys = array(
150 1
                '1d' => 0,
151 1
                '7d' => 2,
152 1
                '1m' => 3,
153 1
                '3m' => 4,
154 1
            );
155 1
        }
156 4
        else if (3 == $nodes->length) {
157 1
            self::$_rankKeys = array(
158 1
                '1d' => 0,
159 1
                '7d' => 0,
160 1
                '1m' => 2,
161 1
                '3m' => 3,
162 1
            );
163 1
        }
164 3
        else if (2 == $nodes->length) {
165 3
            self::$_rankKeys = array(
166 3
                '1d' => 0,
167 3
                '7d' => 0,
168 3
                '1m' => 0,
169 3
                '3m' => 2,
170 3
            );
171 3
        }
172 11
    }
173
174 3
    public static function getCountryRank($url = false)
175
    {
176 3
        $xpath = self::_getXPath($url);
177 3
        $node1 = self::parseDomByXpaths($xpath, array(
178 3
            "//*[@id='traffic-rank-content']/div/span[2]/div[2]/span/span/h4/a",
179 3
            "//*[@id='traffic-rank-content']/div/span[2]/div[2]/span/span/h4/strong/a",
180 3
        ));
181
182 3
        $node2 = self::parseDomByXpaths($xpath, array(
183 3
            "//*[@id='traffic-rank-content']/div/span[2]/div[2]/span/span/div/strong/a",
184 3
            "//*[@id='traffic-rank-content']/div/span[2]/div[2]/span/span/div/strong",
185 3
        ));
186
187 3
        $node3 = self::parseDomByXpaths($xpath, array(
188 3
            "//*[@id='traffic-rank-content']/div/span[2]/div[2]/span/span/h4/a/@href",
189 3
            "//*[@id='traffic-rank-content']/div/span[2]/div[2]/span/span/h4/strong/a/@href",
190 3
        ));
191
192 3
        if (!is_null($node2) && $node2->item(0)) {
193 3
            $rank = self::retInt(strip_tags($node2->item(0)->nodeValue));
194 3
            $country_code = str_replace("/topsites/countries/", "", $node3->item(0)->nodeValue);
195 3
            if ($node1->item(0) && 0 != $rank) {
196
                return array(
197 3
                    'rank' => $rank,
198 3
                    'country' => $node1->item(0)->nodeValue,
199 3
                    'country_code' => $country_code,
200 3
                );
201
            }
202
        }
203
204
        return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getCountryRank()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
205
    }
206
207 1 View Code Duplication
    public static function getBacklinkCount($url = false)
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...
208
    {
209 1
        $xpath = self::_getXPath($url);
210
211
        $queryList = array(
212 1
            "//section[@id='linksin-panel-content']/div/span/div/span",
213
            "//*[@id='linksin_div']/section/div/div[1]/span"
214 1
        );
215
216 1
        return static::parseDomByXpathsToInteger($xpath, $queryList);
217
    }
218
219 2 View Code Duplication
    public static function getPageLoadTime($url = false)
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...
220
    {
221 2
        $xpath = self::_getXPath($url);
222
223
        $queryList = array(
224 2
            "//section[@class='row-fluid panel-wrapper '][9]/section/p",
225
            "//*[@id='section-load']/div/section/p"
226 2
        );
227
228 2
        return static::parseDomByXpathsWithoutTags($xpath, $queryList);
229
    }
230
231
    /**
232
     * @author Meraj Ahmad Siddiqui <[email protected]>
233
     * Bounce Rate from alexa
234
     */
235
236 View Code Duplication
    public static function getBounceRate($url = false)
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...
237
    {
238
        $xpath = self::_getXPath($url);
239 13
240
        $xpathQueryList = array(
241 13
            "//*[@id='engagement-content']/span[1]/span/span/div/strong",
242 13
            "//*[@id='engagement-content']/span[1]/span/span/div/strong/a"
243
        );
244
245 13
        return static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList); 
246 7
    }
247 6
    /**
248 5
     * @author Meraj Ahmad Siddiqui <[email protected]>
249 4
     * Search Visits  
250 3
     */
251 2 View Code Duplication
    public static function getSearchVisits($url = false)
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...
252 2
    {
253
        $xpath = self::_getXPath($url);
254 13
255 11
        $xpathQueryList = array(
256
            "//*[@id='keyword-content']/span[1]/span/span/div/strong",
257 11
            "//*[@id='keyword-content']/span[1]/span/span/div/strong/a"
258
        );
259
260
        return static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList); 
261
    }
262
263 22
    /**
264 22
     * @author Meraj Ahmad Siddiqui <[email protected]>
265 22
     * Get daily page views
266 1
     */
267 View Code Duplication
    public static function getDailyPageViews($url = false)
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...
268
    {
269 22
        $xpath = self::_getXPath($url);
270 22
271 22
        $xpathQueryList = array(
272
            "//*[@id='engagement-content']/span[2]/span/span/div/strong",
273 22
            "//*[@id='engagement-content']/span[2]/span/span/div/strong/a"
274
        );
275 22
276
        return static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList); 
277
    }
278 2
279
    /**
280 2
     * @author Meraj Ahmad Siddiqui <[email protected]>
281 2
     * Get daily page time on site
282 2
     */
283 2
284 View Code Duplication
    public static function getDailyTimeOnSite($url = false)
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...
285
    {
286 24
        $xpath = self::_getXPath($url);
287
288 24
        $xpathQueryList = array(
289 24
            "//*[@id='engagement-content']/span[3]/span/span/div/strong",
290 24
            "//*[@id='engagement-content']/span[3]/span/span/div/strong/a"
291
        );
292
293
        return static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList); 
294
    }
295
296
    /**
297 10
     * @author Meraj Ahmad Siddiqui <[email protected]>
298
     * Get top keywords (max 5 listed )
299 10
     */
300 10
301
    public static function getKeyWords($url = false)
302 10
    {
303 10
        $xpath = self::_getXPath($url);
304
        $res_data = [];
305 5
        for($i = 1; $i < 6; $i++ ) {
306
            $xpathQueryList = array(
307
                "//*[@id='keyword-content']/span[2]/table/tbody/tr[$i]/td[1]"
308
             );
309
            $xpathQueryList1 = array(
310
                "//*[@id='keyword-content']/span[2]/table/tbody/tr[$i]/td[2]"
311
             );
312
            $key = static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList);
313
            $val = static::parseDomByXpathsToIntegerWithoutTags($xpath, $xpathQueryList1);
314 7
315
            $res_data[$i]["kewords"] = str_replace("&nbsp", "", trim(explode(".", $key)[1]));
316 7
            $res_data[$i]["traffic"] = $val;
317
            if(strcmp(trim($key), "n.a.")==0){
318 7
                unset($res_data[$i]);
319
            }
320
        }
321
322
        return  $res_data;
323
    }
324
325 1
    /**
326
     * @author Meraj Ahmad Siddiqui <[email protected]>
327 1
     * Get top Visitor countries (max 5 listed )
328
     */
329 1
    public static function getVisitorCountries($url = false)
330
    {
331
        $xpath = self::_getXPath($url);
332 1
        $res_data = [];
333
        for($i = 1; $i < 6; $i++ ) {
334
            $country = array(
335
                "//*[@id='visitors-content']/div[2]/span[2]/table/tbody/tr[$i]/td[1]/a"
336
             );
337
            $traffic = array(
338
                "//*[@id='visitors-content']/div[2]/span[2]/table/tbody/tr[$i]/td[2]"
339 2
             );
340
            $rank = array(
341
                "//*[@id='visitors-content']/div[2]/span[2]/table/tbody/tr[$i]/td[3]"
342 2
             );
343
344 2
            $country = static::parseDomByXpathsToIntegerWithoutTags($xpath, $country);
345
            $traffic = static::parseDomByXpathsToIntegerWithoutTags($xpath, $traffic);
346
            $rank = static::parseDomByXpathsToIntegerWithoutTags($xpath, $rank);
347
            $res_data[$i]["country"] = $country;
348 2
            $res_data[$i]["traffic"] = $traffic;
349
            $res_data[$i]["rank in country"] = $rank;
350
            if(strcmp(trim($country), "n.a.")==0){
351
                unset($res_data[$i]);
352
            }
353
        }
354
355 4
        return  $res_data;
356
    }
357 4
    /**
358
     * @access        public
359 4
     * @param         integer    $type      Specifies the graph type. Valid values are 1 to 6.
360
     * @param         integer    $width     Specifies the graph width (in px).
0 ignored issues
show
Bug introduced by
There is no parameter named $width. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
361
     * @param         integer    $height    Specifies the graph height (in px).
0 ignored issues
show
Bug introduced by
There is no parameter named $height. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
362
     * @param         integer    $period    Specifies the displayed time period. Valid values are 1 to 12.
363 4
     * @return        string                Returns a string, containing the HTML code of an image, showing Alexa Statistics as Graph.
364
     */
365
    public static function getTrafficGraph($type = 1, $url = false, $w = 660, $h = 330, $period = 1, $html = true)
366 1
    {
367
        $url    = self::getUrl($url);
368
        $domain = Helper\Url::parseHost($url);
369
370
        switch($type) {
371
            case 1: $gtype = 't'; break;
372
            case 2: $gtype = 'p'; break;
373
            case 3: $gtype = 'u'; break;
374
            case 4: $gtype = 's'; break;
375
            case 5: $gtype = 'b'; break;
376
            case 6: $gtype = 'q'; break;
377
            default: break;
378
        }
379
380
        $imgUrl = sprintf(Config\Services::ALEXA_GRAPH_URL, $gtype, $w, $h, $period, $domain);
0 ignored issues
show
Bug introduced by
The variable $gtype does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
381
        $imgTag = '<img src="%s" width="%s" height="%s" alt="Alexa Statistics Graph for %s"/>';
382
383
        return !$html ? $imgUrl : sprintf($imgTag, $imgUrl, $w, $h, $domain);
384
    }
385
386
    /**
387
     * @return DOMXPath
388
     */
389
    protected static function _getXPath($url) {
390
        $url = parent::getUrl($url);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of _getXPath()). Are you sure this is correct? If so, you might want to change this to $this->getUrl().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
391
        if (parent::getLastLoadedUrl() == $url && self::$_xpath) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getLastLoadedUrl() instead of _getXPath()). Are you sure this is correct? If so, you might want to change this to $this->getLastLoadedUrl().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
392
            return self::$_xpath;
393
        }
394
395
        $html  = static::_getAlexaPage($url);
396
        $doc   = parent::_getDOMDocument($html);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (_getDOMDocument() instead of _getXPath()). Are you sure this is correct? If so, you might want to change this to $this->_getDOMDocument().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
397
        $xpath = parent::_getDOMXPath($doc);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (_getDOMXPath() instead of _getXPath()). Are you sure this is correct? If so, you might want to change this to $this->_getDOMXPath().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
398
399
        self::$_xpath = $xpath;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xpath of type object<DOMXPath> is incompatible with the declared type object<SEOstats\Services\DOMXPath> of property $_xpath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
400
401
        return $xpath;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $xpath; (DOMXPath) is incompatible with the return type documented by SEOstats\Services\Alexa::_getXPath of type SEOstats\Services\DOMXPath.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
402
    }
403
404
    protected static function _getAlexaPage($url)
405
    {
406
        $domain  = Helper\Url::parseHost($url);
407
        $dataUrl = sprintf(Config\Services::ALEXA_SITEINFO_URL, $domain);
408
        $html    = static::_getPage($dataUrl);
409
        return $html;
410
    }
411
412
    protected static function retInt($str)
413
    {
414
        $strim = trim(str_replace(',', '', $str));
415
        $intStr = 0 < strlen($strim) ? $strim : '0';
416
        return $intStr;
417
    }
418
419
    /**
420
     *
421
     * @return mixed nodeValue
422
     */
423
    protected static function parseDomByXpaths($xpathDom, $xpathQueryList) {
424
425
        foreach ( $xpathQueryList as $query ) {
426
            $nodes = @$xpathDom->query($query);
427
428
            if ( $nodes->length != 0 ) {
429
                return $nodes;
430
            }
431
        }
432
433
        return null;
434
    }
435
436
    /**
437
     *
438
     * @return mixed nodeValue
439
     */
440
    protected static function parseDomByXpathsGetValue($xpathDom, $xpathQueryList)
441
    {
442
        $nodes = static::parseDomByXpaths($xpathDom, $xpathQueryList);
443
444
        return ($nodes) ? $nodes->item(0)->nodeValue : null;
445
    }
446
447
    /**
448
     *
449
     * @return mixed nodeValue
450
     */
451 View Code Duplication
    protected static function parseDomByXpathsToInteger($xpathDom, $xpathQueryList)
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...
452
    {
453
        $nodeValue = static::parseDomByXpathsGetValue($xpathDom, $xpathQueryList);
454
455
        if ($nodeValue === null) {
456
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of parseDomByXpathsToInteger()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
457
        }
458
        return self::retInt( $nodeValue );
459
    }
460
461
    /**
462
     *
463
     * @return mixed nodeValue
464
     */
465 View Code Duplication
    protected static function parseDomByXpathsWithoutTags($xpathDom, $xpathQueryList)
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...
466
    {
467
468
        $nodeValue = static::parseDomByXpathsGetValue($xpathDom, $xpathQueryList);
469
470
        if ($nodeValue === null) {
471
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of parseDomByXpathsWithoutTags()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
472
        }
473
474
        return strip_tags($nodeValue);
475
    }
476
477
    /**
478
     *
479
     * @return mixed nodeValue
480
     */
481 View Code Duplication
    protected static function parseDomByXpathsToIntegerWithoutTags($xpathDom, $xpathQueryList)
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...
482
    {
483
        $nodeValue = static::parseDomByXpathsGetValue($xpathDom, $xpathQueryList);
484
485
        if ($nodeValue === null) {
486
            return parent::noDataDefaultValue();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of parseDomByXpathsToIntegerWithoutTags()). Are you sure this is correct? If so, you might want to change this to $this->noDataDefaultValue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
487
        }
488
489
        return self::retInt(strip_tags($nodeValue));
490
    }
491
}
492