Completed
Pull Request — master (#112)
by
unknown
07:06
created

Mozscape::getCols()   C

Complexity

Conditions 12
Paths 101

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12.5007

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 55
ccs 28
cts 33
cp 0.8485
rs 6.7602
cc 12
eloc 39
nc 101
nop 1
crap 12.5007

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
namespace SEOstats\Services;
3
4
/**
5
 * SEOstats extension for Mozscape (f.k.a. Seomoz) metrics.
6
 *
7
 * @package    SEOstats
8
 * @author     Stephan Schmitz &lt;[email protected]&gt;
9
 * @copyright  Copyright (c) 2010 - present Stephan Schmitz
10
 * @license    http://eyecatchup.mit-license.org/  MIT License
11
 * @updated    2013/12/11
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 Mozscape extends SEOstats
20
{
21
22
    protected static $lastLoadedDomain;
23 2
    protected static $lastLoadedPage;
24
    // A normalized 100-point score representing the likelihood
25 2
    // of the URL to rank well in search engine results.
26 2
    public static function getPageAuthority($url = false)
27 2
    {
28
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

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...
29
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getPageAuthority()). 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...
30
            $data['upa'];
31
    }
32 2
33
    // A normalized 100-point score representing the likelihood
34 2
    // of the domain of the URL to rank well in search engine results.
35 2
    public static function getDomainAuthority($url = false)
36 2
    {
37
        $data = static::getCols(Helper\Url::parseHost($url));
38
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getDomainAuthority()). 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...
39
            $data['pda'];
40
    }
41 2
42
    // The number of external equity links to the URL.
43 2
    // http://apiwiki.moz.com/glossary#equity
44 2
    public static function getEquityLinkCount($url = false)
45 2
    {
46
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

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...
47
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getEquityLinkCount()). 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
            $data['uid'];
49 2
    }
50
51 2
    // The number of links (equity or nonequity or not, internal or external) to the URL.
52 2
    public static function getLinkCount($url = false)
53 2
    {
54
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

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...
55
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getLinkCount()). 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...
56
            $data['uid'];
57 2
    }
58
59 2
    // The normalized 10-point MozRank score of the URL.
60 2
    public static function getMozRank($url = false)
61 2
    {
62
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

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...
63
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getMozRank()). 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...
64
            $data['umrp'];
65 2
    }
66
67 2
    // The raw MozRank score of the URL.
68 2
    public static function getMozRankRaw($url = false)
69 2
    {
70
        $data = static::getCols($url);
0 ignored issues
show
Documentation introduced by
$url is of type boolean, but the function expects a false|string.

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...
71
        return (parent::noDataDefaultValue() == $data) ? $data :
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getMozRankRaw()). 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...
72
            number_format($data['umrr'], 16);
73
    }
74
75
    /**
76
     * Return Link metrics from the (free) Mozscape (f.k.a. Seomoz) API.
77
     *
78
     * @access        public
79 5
     * @param   cols  string     The bit flags you want returned.
80
     * @param   url   string     The URL to get metrics for.
81 5
     * @return mixed
82 5
     */
83
    public static function getCols($url = false)
84
    {
85
        if ('' == Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') ||
86
            '' == Config\ApiKeys::get('MOZSCAPE_SECRET_KEY')
87
        ) {
88 5
            throw new E('In order to use the Mozscape API, you must obtain
89
                and set an API key first (see SEOstats\Config\ApiKeys.php).');
90 5
        }
91 5
92 5
        $verbose = getenv('SEOSTATS_VERBOSE');
93 5
        $host = Helper\Url::parseHost(parent::getUrl($url));
0 ignored issues
show
Bug introduced by
It seems like $url defined by parameter $url on line 83 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...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getUrl() instead of getCols()). 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...
94 5
        if ($verbose) {
95 5
            print "Request for url $url\n";
96 5
            print "Request host $host\n";
97
        }
98 5
99
        if (static::$lastLoadedDomain == $host) {
100 5
            if ($verbose) {
101 5
                print "Return value:\n";
102 5
                print_r(static::$lastLoadedPage);
103
                print "\n";
104
            }
105 6
            return static::$lastLoadedPage;
106
        }
107 6
108 6
        static::$lastLoadedDomain = $host;
109
110 6
        $expires = time() + 300;
111
        $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL,
112
            urlencode($host),
113 7
            Config\ApiKeys::get('MOZSCAPE_ACCESS_ID'),
114
            $expires,
115
            urlencode(self::_getUrlSafeSignature($expires))
116
        );
117 7
        if ($verbose) {
118 7
            print "Endpoint url:\n";
119
            print_r($apiEndpoint);
120
            print "\n";
121
        }
122
        $ret = static::_getPage($apiEndpoint);
123
        if ($verbose) {
124 1
            print "Raw responce:\n";
125
            print_r($ret);
126 1
            print "\n";
127 1
        }
128
        static::$lastLoadedPage = (!$ret || empty($ret) || '{}' == (string)$ret)
129 1
            ? parent::noDataDefaultValue()
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (noDataDefaultValue() instead of getCols()). 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...
130
            : Helper\Json::decode($ret, true);
131
        if ($verbose) {
132
            print "Return value:\n";
133 1
            print_r(static::$lastLoadedPage);
134 1
            print "\n";
135 1
        }
136 1
        return static::$lastLoadedPage;
137 1
    }
138 1
139
    private static function _getUrlSafeSignature($expires)
140
    {
141
        $data = Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') . "\n{$expires}";
142
        $sig = self::_hmacsha1($data, Config\ApiKeys::get('MOZSCAPE_SECRET_KEY'));
143
144
        return base64_encode($sig);
145
    }
146
147
    private static function _hmacsha1($data, $key)
148
    {
149
        // Use PHP's built in functionality if available
150
        // (~20% faster than the custom implementation below).
151
        if (function_exists('hash_hmac')) {
152
            return hash_hmac('sha1', $data, $key, true);
153
        }
154
155
        return self::_hmacsha1Rebuild($data, $key);
156
    }
157
158
    private static function _hmacsha1Rebuild($data, $key)
159
    {
160
        $blocksize = 64;
161
        $hashfunc = 'sha1';
162
163
        if (strlen($key) > $blocksize) {
164
            $key = pack('H*', $hashfunc($key));
165
        }
166
167
        $key = str_pad($key, $blocksize, chr(0x00));
168
        $ipad = str_repeat(chr(0x36), $blocksize);
169
        $opad = str_repeat(chr(0x5c), $blocksize);
170
        $hmac = pack('H*', $hashfunc(($key ^ $opad) .
171
            pack('H*', $hashfunc(($key ^ $ipad) . $data))));
172
        return $hmac;
173
    }
174
}
175