Completed
Pull Request — master (#112)
by
unknown
03:22
created

Google   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 68.28%

Importance

Changes 18
Bugs 2 Features 3
Metric Value
wmc 14
c 18
b 2
f 3
lcom 1
cbo 5
dl 14
loc 112
ccs 28
cts 41
cp 0.6828
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageRank() 0 13 3
A getSiteindexTotal() 7 7 1
A getBacklinksTotal() 7 7 1
A getSearchResultsTotal() 0 12 2
A getPagespeedAnalysis() 0 15 2
A getPagespeedScore() 0 13 4
A getSerps() 0 4 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
namespace SEOstats\Services;
3
4
/**
5
 * SEOstats extension for Google 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/12/17
12
 */
13
14
use SEOstats\Common\SEOstatsException as E;
15
use SEOstats\SEOstats as SEOstats;
16
use SEOstats\Config as Config;
17
use SEOstats\Helper as Helper;
18
19
class Google extends SEOstats
20
{
21
    /**
22
     *  Gets the Google Pagerank
23
     *
24
     * @param    string $url String, containing the query URL.
25
     * @return   integer           Returns the Google PageRank.
26
     */
27 2
    public static function getPageRank($url = false)
28
    {
29
        // Composer autoloads classes out of the SEOstats namespace.
30
        // The custom autolader, however, does not. So we need to include it first.
31 2
        if (!class_exists('\GTB_PageRank')) {
32
            require_once realpath(__DIR__ . '/3rdparty/GTB_PageRank.php');
33
        }
34
35 2
        $gtb = new \GTB_PageRank(parent::getUrl($url));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of getPageRank()). 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...
Bug introduced by
It seems like $url defined by parameter $url on line 27 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
36 2
        $result = $gtb->getPageRank();
37
38 2
        return $result != "" ? $result : static::noDataDefaultValue();
39
    }
40
41
    /**
42
     *  Returns the total amount of results for a Google 'site:'-search for the object URL.
43
     *
44
     * @param    string $url String, containing the query URL.
45
     * @return   integer           Returns the total site-search result count.
46
     */
47 1 View Code Duplication
    public static function getSiteindexTotal($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...
48
    {
49 1
        $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 getSiteindexTotal()). 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...
Bug introduced by
It seems like $url defined by parent::getUrl($url) on line 49 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50 1
        $query = urlencode("site:{$url}");
51
52 1
        return self::getSearchResultsTotal($query);
53
    }
54
55
    /**
56
     *  Returns the total amount of results for a Google 'link:'-search for the object URL.
57
     *
58
     * @param    string $url String, containing the query URL.
59
     * @return   integer           Returns the total link-search result count.
60
     */
61 1 View Code Duplication
    public static function getBacklinksTotal($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...
62
    {
63 1
        $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 getBacklinksTotal()). 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...
Bug introduced by
It seems like $url defined by parent::getUrl($url) on line 63 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64 1
        $query = urlencode("link:{$url}");
65
66 1
        return self::getSearchResultsTotal($query);
67
    }
68
69
    /**
70
     *  Returns total amount of results for any Google search,
71
     *  requesting the deprecated Websearch API.
72
     *
73
     * @param    string $url String, containing the query URL.
74
     * @return   integer           Returns the total search result count.
75
     */
76 3
    public static function getSearchResultsTotal($url = false)
77
    {
78 3
        $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 getSearchResultsTotal()). 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...
Bug introduced by
It seems like $url defined by parent::getUrl($url) on line 78 can also be of type string; however, SEOstats\SEOstats::getUrl() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79 3
        $url = sprintf(Config\Services::GOOGLE_APISEARCH_URL, 1, $url);
80
81 3
        $ret = static::_getPage($url);
82
83 3
        $obj = Helper\Json::decode($ret);
84 3
        return !isset($obj->responseData->cursor->estimatedResultCount)
85 3
            ? parent::noDataDefaultValue()
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getSearchResultsTotal()). 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...
86 3
            : intval($obj->responseData->cursor->estimatedResultCount);
87
    }
88
89 4
    public static function getPagespeedAnalysis($url = false, $strategy = 'desktop')
90
    {
91 4
        if ('' == Config\ApiKeys::get('GOOGLE_SIMPLE_API_ACCESS_KEY')) {
92 4
            throw new E('In order to use the PageSpeed API, you must obtain
93 4
                and set an API key first (see SEOstats\Config\ApiKeys.php).');
94
        }
95
96
        $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 getPagespeedAnalysis()). 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...
97
        $url = sprintf(Config\Services::GOOGLE_PAGESPEED_URL,
98
            $url, $strategy, Config\ApiKeys::get('GOOGLE_SIMPLE_API_ACCESS_KEY'));
99
100
        $ret = static::_getPage($url);
101
102
        return Helper\Json::decode($ret);
103
    }
104
105 2
    public static function getPagespeedScore($url = false, $strategy = 'desktop')
106
    {
107 2
        $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 getPagespeedScore()). 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...
108 2
        $ret = self::getPagespeedAnalysis($url, $strategy);
0 ignored issues
show
Documentation introduced by
$url is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
110
        // Check if $ret->score exists for backwards compatibility with v1.
111
        if (!isset($ret->score)) {
112
            return !isset($ret->ruleGroups->SPEED->score) || !$ret->ruleGroups->SPEED->score ? parent::noDataDefaultValue() :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getPagespeedScore()). 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...
113
                intval($ret->ruleGroups->SPEED->score);
114
        } else {
115
            return $ret->score;
116
        }
117
    }
118
119
    /**
120
     * Returns array, containing detailed results for any Google search.
121
     *
122
     * @param     string $query String, containing the search query.
123
     * @param     string $tld String, containing the desired Google top level domain.
0 ignored issues
show
Bug introduced by
There is no parameter named $tld. 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...
124
     * @return    array             Returns array, containing the keys 'URL', 'Title' and 'Description'.
125
     */
126
    public static function getSerps($query, $maxResults = 100, $domain = false)
127
    {
128
        return Google\Search::getSerps($query, $maxResults, $domain);
129
    }
130
}
131