SpiderVoter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A vote() 0 14 4
A getDefaultAssets() 0 7 1
1
<?php
2
3
namespace Hongliang\Defender\Voter;
4
5
class SpiderVoter extends BaseVoter implements VoterInterface
6
{
7
    public function vote($spider = null)
0 ignored issues
show
Coding Style introduced by
vote 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...
8
    {
9
        if (!isset($_SERVER['HTTP_USER_AGENT'])) {
10
            return false;
11
        }
12
        $spiders = $this->getAssets();
13
        foreach ($spiders as $s) {
14
            if (false !== stripos($_SERVER['HTTP_USER_AGENT'], $s)) {
15
                return true;
16
            }
17
        }
18
19
        return false;
20
    }
21
22
    protected function getDefaultAssets()
23
    {
24
        return [
25
            // 'Baiduspider',
26
            // '360spider',
27
        ];
28
    }
29
}
30