UriKeywordVoter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A vote() 0 12 4
A setDefaultTarget() 0 6 1
A getDefaultAssets() 0 15 1
1
<?php
2
3
namespace Hongliang\Defender\Voter;
4
5
class UriKeywordVoter extends BaseVoter implements VoterInterface
6
{
7
    public function vote($uri = null)
8
    {
9
        $uri = $uri ?: $this->target;
10
        $keywords = $this->getAssets();
11
        foreach ($keywords as $k) {
12
            if (false !== stripos($uri, $k)) {
13
                return true;
14
            }
15
        }
16
17
        return false;
18
    }
19
20
    protected function setDefaultTarget()
0 ignored issues
show
Coding Style introduced by
setDefaultTarget 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...
21
    {
22
        $this->target = $_SERVER['REQUEST_URI'];
23
24
        return $this;
25
    }
26
27
    protected function getDefaultAssets()
28
    {
29
        return [
30
            'fckedit',
31
            '/administrator/',
32
            '/wp-',
33
            'xmlrpc',
34
            '/filemanager',
35
            '/bbs',
36
            '/convert',
37
            '/product',
38
            '/plus',
39
            'whitelist.pac',
40
        ];
41
    }
42
}
43