Mozscape   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 91.8%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 23
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 122
ccs 56
cts 61
cp 0.918
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageAuthority() 0 6 2
A getDomainAuthority() 0 6 2
A getEquityLinkCount() 0 6 2
A getLinkCount() 0 6 2
A getMozRank() 0 6 2
A getMozRankRaw() 0 6 2
A _hmacsha1() 0 10 2
A _hmacsha1Rebuild() 0 16 2
B getCols() 0 25 6
A _getUrlSafeSignature() 0 7 1
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
    // A normalized 100-point score representing the likelihood
22
    // of the URL to rank well in search engine results.
23 2
    public static function getPageAuthority($url = false)
24
    {
25 2
        $data = static::getCols('34359738368', $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...
26 2
        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...
27 2
            $data['upa'];
28
    }
29
30
    // A normalized 100-point score representing the likelihood
31
    // of the domain of the URL to rank well in search engine results.
32 2
    public static function getDomainAuthority($url = false)
33
    {
34 2
        $data = static::getCols('68719476736', Helper\Url::parseHost($url));
35 2
        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...
36 2
            $data['pda'];
37
    }
38
39
    // The number of external equity links to the URL.
40
    // http://apiwiki.moz.com/glossary#equity
41 2
    public static function getEquityLinkCount($url = false)
42
    {
43 2
        $data = static::getCols('2048', $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...
44 2
        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...
45 2
            $data['uid'];
46
    }
47
48
    // The number of links (equity or nonequity or not, internal or external) to the URL.
49 2
    public static function getLinkCount($url = false)
50
    {
51 2
        $data = static::getCols('2048', $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...
52 2
        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...
53 2
            $data['uid'];
54
    }
55
56
    // The normalized 10-point MozRank score of the URL.
57 2
    public static function getMozRank($url = false)
58
    {
59 2
        $data = static::getCols('16384', $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...
60 2
        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...
61 2
            $data['umrp'];
62
    }
63
64
    // The raw MozRank score of the URL.
65 2
    public static function getMozRankRaw($url = false)
66
    {
67 2
        $data = static::getCols('16384', $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...
68 2
        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...
69 2
            number_format($data['umrr'], 16);
70
    }
71
72
    /**
73
     * Return Link metrics from the (free) Mozscape (f.k.a. Seomoz) API.
74
     *
75
     * @access        public
76
     * @param   cols  string     The bit flags you want returned.
77
     * @param   url   string     The URL to get metrics for.
78
     */
79 5
    public static function getCols($cols, $url = false)
80
    {
81 5
        if ('' == Config\ApiKeys::getMozscapeAccessId() ||
82 5
            '' == Config\ApiKeys::getMozscapeSecretKey()) {
83
            throw new E('In order to use the Mozscape API, you must obtain
84
                and set an API key first (see SEOstats\Config\ApiKeys.php).');
85
            exit(0);
0 ignored issues
show
Unused Code introduced by
die(0); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
86
        }
87
88 5
        $expires = time() + 300;
89
90 5
        $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL,
91 5
            urlencode(Helper\Url::parseHost(parent::getUrl($url))),
0 ignored issues
show
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...
Bug introduced by
It seems like $url defined by parameter $url on line 79 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...
92 5
            $cols,
93 5
            Config\ApiKeys::getMozscapeAccessId(),
94 5
            $expires,
95 5
            urlencode(self::_getUrlSafeSignature($expires))
96 5
        );
97
98 5
        $ret = static::_getPage($apiEndpoint);
99
100 5
        return (!$ret || empty($ret) || '{}' == (string)$ret)
101 5
                ? 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...
102 5
                : Helper\Json::decode($ret, true);
103
    }
104
105 6
    private static function _getUrlSafeSignature($expires)
106
    {
107 6
        $data = Config\ApiKeys::getMozscapeAccessId() . "\n{$expires}";
108 6
        $sig  = self::_hmacsha1($data, Config\ApiKeys::getMozscapeSecretKey());
109
110 6
        return base64_encode($sig);
111
    }
112
113 7
    private static function _hmacsha1($data, $key)
114
    {
115
        // Use PHP's built in functionality if available
116
        // (~20% faster than the custom implementation below).
117 7
        if (function_exists('hash_hmac')) {
118 7
            return hash_hmac('sha1', $data, $key, true);
119
        }
120
121
        return self::_hmacsha1Rebuild($data, $key);
122
    }
123
124 1
    private static function _hmacsha1Rebuild($data, $key)
125
    {
126 1
        $blocksize = 64;
127 1
        $hashfunc  = 'sha1';
128
129 1
        if (strlen($key) > $blocksize) {
130
            $key = pack('H*', $hashfunc($key));
131
        }
132
133 1
        $key  = str_pad($key, $blocksize, chr(0x00));
134 1
        $ipad = str_repeat(chr(0x36), $blocksize);
135 1
        $opad = str_repeat(chr(0x5c), $blocksize);
136 1
        $hmac = pack('H*', $hashfunc(($key^$opad) .
137 1
                    pack('H*', $hashfunc(($key^$ipad) . $data))));
138 1
        return $hmac;
139
    }
140
}
141