Completed
Pull Request — master (#89)
by Mark
02:51 queued 41s
created

CrawlerDetect::getCrawlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Crawler Detect - the web crawler detection library.
5
 *
6
 * (c) Mark Beech <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Jaybizzle\CrawlerDetect;
13
14
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers;
15
use Jaybizzle\CrawlerDetect\Fixtures\Exclusions;
16
17
class CrawlerDetect
18
{
19
    /**
20
     * The user agent.
21
     *
22
     * @var null
23
     */
24
    protected $userAgent = null;
25
26
    /**
27
     * Headers that contain a user agent.
28
     *
29
     * @var array
30
     */
31
    protected $httpHeaders = array();
32
33
    /**
34
     * Store regex matches.
35
     *
36
     * @var array
37
     */
38
    protected $matches = array();
39
40
    /**
41
     * All possible HTTP headers that represent the
42
     * User-Agent string.
43
     *
44
     * @var array
45
     */
46
    protected static $uaHttpHeaders = array(
47
        // The default User-Agent string.
48
        'HTTP_USER_AGENT',
49
        // Header can occur on devices using Opera Mini.
50
        'HTTP_X_OPERAMINI_PHONE_UA',
51
        // Vodafone specific header: http://www.seoprinciple.com/mobile-web-community-still-angry-at-vodafone/24/
52
        'HTTP_X_DEVICE_USER_AGENT',
53
        'HTTP_X_ORIGINAL_USER_AGENT',
54
        'HTTP_X_SKYFIRE_PHONE',
55
        'HTTP_X_BOLT_PHONE_UA',
56
        'HTTP_DEVICE_STOCK_UA',
57
        'HTTP_X_UCBROWSER_DEVICE_UA',
58
    );
59
60
    /**
61
     * Class constructor.
62
     */
63
    public function __construct(array $headers = null, $userAgent = null)
64
    {
65
        $this->setHttpHeaders($headers);
66
        $this->setUserAgent($userAgent);
67
        $this->crawlers = new Crawlers();
0 ignored issues
show
Bug introduced by
The property crawlers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
        $this->exclusions = new Exclusions();
0 ignored issues
show
Bug introduced by
The property exclusions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
    }
70
71
    /**
72
     * Set HTTP headers.
73
     *
74
     * @param array $httpHeaders
75
     */
76
    public function setHttpHeaders($httpHeaders = null)
0 ignored issues
show
Coding Style introduced by
setHttpHeaders uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
77
    {
78
        // use global _SERVER if $httpHeaders aren't defined
79
        if (!is_array($httpHeaders) || !count($httpHeaders)) {
80
            $httpHeaders = $_SERVER;
81
        }
82
        // clear existing headers
83
        $this->httpHeaders = array();
84
        // Only save HTTP headers. In PHP land, that means only _SERVER vars that
85
        // start with HTTP_.
86
        foreach ($httpHeaders as $key => $value) {
87
            if (substr($key, 0, 5) === 'HTTP_') {
88
                $this->httpHeaders[$key] = $value;
89
            }
90
        }
91
    }
92
93
    /**
94
     * Return user agent headers.
95
     *
96
     * @return array
97
     */
98
    public function getUaHttpHeaders()
99
    {
100
        return self::$uaHttpHeaders;
101
    }
102
103
    /**
104
     * Set the user agent.
105
     *
106
     * @param string $userAgent
107
     */
108
    public function setUserAgent($userAgent = null)
109
    {
110
        if (false === empty($userAgent)) {
111
            return $this->userAgent = $userAgent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userAgent of type string is incompatible with the declared type null of property $userAgent.

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...
112
        } else {
113
            $this->userAgent = null;
114
            foreach ($this->getUaHttpHeaders() as $altHeader) {
115
                if (false === empty($this->httpHeaders[$altHeader])) { // @todo: should use getHttpHeader(), but it would be slow.
116
                    $this->userAgent .= $this->httpHeaders[$altHeader].' ';
117
                }
118
            }
119
120
            return $this->userAgent = (!empty($this->userAgent) ? trim($this->userAgent) : null);
0 ignored issues
show
Documentation Bug introduced by
It seems like !empty($this->userAgent)...this->userAgent) : null can also be of type string. However, the property $userAgent is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
121
        }
122
    }
123
124
    /**
125
     * Build the user agent regex.
126
     *
127
     * @return string
128
     */
129
    public function getRegex()
130
    {
131
        return '('.implode('|', $this->crawlers->getAll()).')';
132
    }
133
134
    /**
135
     * Build the replacement regex.
136
     *
137
     * @return string
138
     */
139
    public function getIgnored()
140
    {
141
        return '('.implode('|', $this->exclusions->getAll()).')';
142
    }
143
144
    /**
145
     * Check user agent string against the regex.
146
     *
147
     * @param string $userAgent
148
     *
149
     * @return bool
150
     */
151
    public function isCrawler($userAgent = null)
152
    {
153
        $agent = is_null($userAgent) ? $this->userAgent : $userAgent;
154
155
        $agent = preg_replace('/'.$this->getIgnored().'/i', '', $agent);
156
157
        if (trim($agent) === false) {
158
            return false;
159
        } else {
160
            $result = preg_match('/'.$this->getRegex().'/i', trim($agent), $matches);
161
        }
162
163
        if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
164
            $this->matches = $matches;
165
        }
166
167
        return (bool) $result;
168
    }
169
170
    /**
171
     * Return the matches.
172
     *
173
     * @return string
174
     */
175
    public function getMatches()
176
    {
177
        return $this->matches[0];
178
    }
179
}
180