Completed
Pull Request — master (#189)
by Mark
20:14
created

AgentDetector::compileRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Detectors;
13
14
use Jaybizzle\CrawlerDetect\Fixtures\Headers;
15
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers;
16
use Jaybizzle\CrawlerDetect\Fixtures\Exclusions;
17
18
class AgentDetector
19
{
20
    /**
21
     * The user agent.
22
     *
23
     * @var null
24
     */
25
    protected $userAgent = null;
26
27
    /**
28
     * Headers that contain a user agent.
29
     *
30
     * @var array
31
     */
32
    protected $httpHeaders = array();
33
34
    /**
35
     * Store regex matches.
36
     *
37
     * @var array
38
     */
39
    protected $matches = array();
40
41
    /**
42
     * Crawlers object.
43
     *
44
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Crawlers
45
     */
46
    protected $crawlers;
47
48
    /**
49
     * Exclusions object.
50
     *
51
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Exclusions
52
     */
53
    protected $exclusions;
54
55
    /**
56
     * Headers object.
57
     *
58
     * @var \Jaybizzle\CrawlerDetect\Fixtures\Headers
59
     */
60
    protected $uaHttpHeaders;
61
62
    /**
63
     * The compiled regex string.
64
     *
65
     * @var string
66
     */
67
    protected $compiledRegex;
68
69
    /**
70
     * The compiled exclusions regex string.
71
     *
72
     * @var string
73
     */
74
    protected $compiledExclusions;
75
76
    public function __construct($userAgent)
77
    {
78
        $this->userAgent = $userAgent;
79
80
        $this->crawlers = new Crawlers();
81
        $this->exclusions = new Exclusions();
82
        $this->uaHttpHeaders = new Headers();
83
84
        $this->compiledRegex = $this->compileRegex($this->crawlers->getAll());
85
        $this->compiledExclusions = $this->compileRegex($this->exclusions->getAll());
86
    }
87
88
    /**
89
     * Compile the regex patterns into one regex string.
90
     *
91
     * @param array
92
     * 
93
     * @return string
94
     */
95
    public function compileRegex($patterns)
96
    {
97
        return '('.implode('|', $patterns).')';
98
    }
99
100
    /**
101
     * Set HTTP headers.
102
     *
103
     * @param array|null $httpHeaders
0 ignored issues
show
Bug introduced by
There is no parameter named $httpHeaders. 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...
104
     */
105
    public function setHttpHeaders()
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...
106
    {
107
        $httpHeaders = $_SERVER;
108
109
        // Only save HTTP headers. In PHP land, that means
110
        // only _SERVER vars that start with HTTP_.
111
        foreach ($httpHeaders as $key => $value) {
112
            if (strpos($key, 'HTTP_') === 0) {
113
                $this->httpHeaders[$key] = $value;
114
            }
115
        }
116
    }
117
118
    /**
119
     * Return user agent headers.
120
     *
121
     * @return array
122
     */
123
    public function getUaHttpHeaders()
124
    {
125
        return $this->uaHttpHeaders->getAll();
126
    }
127
128
    /**
129
     * Set the user agent.
130
     *
131
     * @return void
132
     */
133
    public function setUserAgent()
134
    {
135
        foreach ($this->getUaHttpHeaders() as $altHeader) {
136
            if (false === empty($this->httpHeaders[$altHeader])) { // @todo: should use getHttpHeader(), but it would be slow.
137
                $this->userAgent .= $this->httpHeaders[$altHeader].' ';
138
            }
139
        }
140
141
        $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...
142
    }
143
144
    /**
145
     * Perform the check.
146
     * 
147
     * @param  string|null $userAgent
148
     * @return bool
149
     */
150
    public function check($userAgent = null)
151
    {
152
        if (is_null($this->userAgent) && ! is_null($userAgent)) {
153
            $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...
154
        }
155
156
        if (is_null($this->userAgent)) {
157
            $this->setHttpHeaders();
158
            $this->setUserAgent();
159
        }
160
161
        $agent = preg_replace('/'.$this->compiledExclusions.'/i', '', $this->userAgent);
0 ignored issues
show
Unused Code introduced by
$agent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
163
        if (strlen(trim($this->userAgent)) == 0) {
164
            return false;
165
        }
166
167
        $result = preg_match('/'.$this->compiledRegex.'/i', trim($this->userAgent), $matches);
168
169
        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...
170
            $this->matches = $matches;
171
        }
172
173
        return (bool) $result;
174
    }
175
}
176