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

CrawlerDetect::setUserAgent()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 7
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;
13
14
use Jaybizzle\CrawlerDetect\Detectors\IpDetector;
15
use Jaybizzle\CrawlerDetect\Detectors\AgentDetector;
16
17
class CrawlerDetect
18
{
19
    /**
20
     * Check agent setting.
21
     *
22
     * @var null|bool
23
     */
24
    protected $checkAgent = null;
25
26
    /**
27
     * Check IP setting.
28
     * 
29
     * @var null|bool
30
     */
31
    protected $checkIp = null;
32
33
    /**
34
     * Array of detectors.
35
     * 
36
     * @var array
37
     */
38
    protected $detectors = array();
39
40
    /**
41
     * Check the user agent.
42
     * 
43
     * @return $this
44
     */
45
    public function agent($userAgent = null)
46
    {
47
        $this->checkAgent = true;
48
49
        if (! isset($detectors['agent'])) {
0 ignored issues
show
Bug introduced by
The variable $detectors seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
50
            $this->detectors['agent'] = new AgentDetector($userAgent);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * Check the IP address.
58
     * 
59
     * @return $this
60
     */
61
    public function ip($userIp)
62
    {
63
        $this->checkIp = true;
64
65
        if (! isset($detectors['ip'])) {
0 ignored issues
show
Bug introduced by
The variable $detectors seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
66
            $this->detectors['ip'] = new IpDetector($userIp);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Check user agent string against the regex.
74
     *
75
     * @param string|null $userAgent
0 ignored issues
show
Bug introduced by
There is no parameter named $userAgent. 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...
76
     *
77
     * @return bool
78
     */
79
    public function isCrawler()
80
    {
81
        if ($this->checkAgent && ! $this->checkIp) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->checkIp of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
82
            return $this->detectors['agent']->check();
83
        }
84
85
        if ($this->checkIp && ! $this->checkAgent) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->checkAgent of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
86
            return $this->detectors['ip']->check();
87
        }
88
89
        if (is_null($this->checkIp) && is_null($this->checkAgent)) {
90
            $this->ip()->agent();
0 ignored issues
show
Bug introduced by
The call to ip() misses a required argument $userIp.

This check looks for function calls that miss required arguments.

Loading history...
91
92
            return $this->detectors['agent']->check() && $this->detectors['ip']->check();
93
        }
94
    }
95
96
    /**
97
     * Return the matches.
98
     *
99
     * @return string|null
100
     */
101
    public function getMatches()
102
    {
103
        return isset($this->matches[0]) ? $this->matches[0] : null;
0 ignored issues
show
Bug introduced by
The property matches 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...
104
    }
105
}
106