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

Mozscape::getCols()   C

Complexity

Conditions 12
Paths 101

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 114.7523

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 0
loc 54
ccs 5
cts 47
cp 0.1064
rs 6.7451
cc 12
eloc 39
nc 101
nop 1
crap 114.7523

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
    protected static $lastLoadedPage;
24
    // A normalized 100-point score representing the likelihood
25
    // of the URL to rank well in search engine results.
26 2
    public static function getPageAuthority($url = false)
27
    {
28 2
        $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
33
    // A normalized 100-point score representing the likelihood
34
    // of the domain of the URL to rank well in search engine results.
35 2
    public static function getDomainAuthority($url = false)
36
    {
37 2
        $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
42
    // The number of external equity links to the URL.
43
    // http://apiwiki.moz.com/glossary#equity
44 2
    public static function getEquityLinkCount($url = false)
45
    {
46 2
        $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
    }
50
51
    // The number of links (equity or nonequity or not, internal or external) to the URL.
52 2
    public static function getLinkCount($url = false)
53
    {
54 2
        $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
    }
58
59
    // The normalized 10-point MozRank score of the URL.
60 2
    public static function getMozRank($url = false)
61
    {
62 2
        $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
    }
66
67
    // The raw MozRank score of the URL.
68 2
    public static function getMozRankRaw($url = false)
69
    {
70 2
        $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
     * @param   cols  string     The bit flags you want returned.
80
     * @param   url   string     The URL to get metrics for.
81
     * @return mixed
82
     */
83 5
    public static function getCols($url = false)
84
    {
85 5
        if ('' == Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') ||
86
            '' == Config\ApiKeys::get('MOZSCAPE_SECRET_KEY')
87 5
        ) {
88 5
            throw new E('In order to use the Mozscape API, you must obtain
89 5
                and set an API key first (see SEOstats\Config\ApiKeys.php).');
90
        }
91
92
        $verbose = getenv('SEOSTATS_VERBOSE');
93
        $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
        if ($verbose) {
95
            print "[SEOSTATS] Request for url $url\n";
96
            print "[SEOSTATS] Request host $host\n";
97
        }
98
99
        if (static::$lastLoadedDomain == $host) {
100
            if ($verbose) {
101
                print "[SEOSTATS] Return value:\n";
102
                print_r(static::$lastLoadedPage);
103
                print "\n";
104
            }
105
            return static::$lastLoadedPage;
106
        }
107
108
        $expires = time() + 300;
109
        $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL,
110
            urlencode($host),
111
            Config\ApiKeys::get('MOZSCAPE_ACCESS_ID'),
112
            $expires,
113
            urlencode(self::_getUrlSafeSignature($expires))
114
        );
115
        if ($verbose) {
116
            print "[SEOSTATS] Endpoint url:\n";
117
            print_r($apiEndpoint);
118
            print "\n";
119
        }
120
        $ret = static::_getPage($apiEndpoint);
121
        if ($verbose) {
122
            print "[SEOSTATS] Raw responce:\n";
123
            print_r($ret);
124
            print "\n";
125
        }
126
        static::$lastLoadedDomain = $host;
127
        static::$lastLoadedPage = (!$ret || empty($ret) || '{}' == (string)$ret)
128
            ? 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...
129
            : Helper\Json::decode($ret, true);
130
        if ($verbose) {
131
            print "[SEOSTATS] Return value:\n";
132
            print_r(static::$lastLoadedPage);
133
            print "\n";
134
        }
135
        return static::$lastLoadedPage;
136
    }
137
138 1
    private static function _getUrlSafeSignature($expires)
139
    {
140 1
        $data = Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') . "\n{$expires}";
141 1
        $sig = self::_hmacsha1($data, Config\ApiKeys::get('MOZSCAPE_SECRET_KEY'));
142
143 1
        return base64_encode($sig);
144
    }
145
146 2
    private static function _hmacsha1($data, $key)
147
    {
148
        // Use PHP's built in functionality if available
149
        // (~20% faster than the custom implementation below).
150 2
        if (function_exists('hash_hmac')) {
151 2
            return hash_hmac('sha1', $data, $key, true);
152
        }
153
154
        return self::_hmacsha1Rebuild($data, $key);
155
    }
156
157 1
    private static function _hmacsha1Rebuild($data, $key)
158
    {
159 1
        $blocksize = 64;
160 1
        $hashfunc = 'sha1';
161
162 1
        if (strlen($key) > $blocksize) {
163
            $key = pack('H*', $hashfunc($key));
164
        }
165
166 1
        $key = str_pad($key, $blocksize, chr(0x00));
167 1
        $ipad = str_repeat(chr(0x36), $blocksize);
168 1
        $opad = str_repeat(chr(0x5c), $blocksize);
169 1
        $hmac = pack('H*', $hashfunc(($key ^ $opad) .
170 1
            pack('H*', $hashfunc(($key ^ $ipad) . $data))));
171 1
        return $hmac;
172
    }
173
}
174