Completed
Push — master ( 5ae376...016aee )
by H
10s
created

SpiderVoter::vote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 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
        $spiders = $this->getAssets();
10
        foreach ($spiders as $s) {
11
            if (false !== strpos($_SERVER['HTTP_USER_AGENT'], $s)) {
12
                return true;
13
            }
14
        }
15
        return false;
16
    }
17
18
    protected function getDefaultAssets()
19
    {
20
        return [
21
            // 'Baiduspider',
22
            // '360spider',
23
        ];
24
    }
25
}
26