Completed
Push — master ( 90cf98...92b59c )
by H
02:10
created

UriKeywordVoter::setDefaultTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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 = strtolower($uri ?: $this->target);
10
        $keywords = $this->getAssets();
11
        foreach ($keywords as $k) {
12
            if (false !== strpos($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
            '/filemanager',
34
            '/bbs',
35
            '/convert',
36
            '/product',
37
            '/plus',
38
        ];
39
    }
40
41
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
    public function setAssets($assets)
43
    {
44
        // skip checks for performance
45
        // if (!is_array($assets)) {
46
        //     throw new \Exception('Assets of '.static::class.' must be an array');
47
        // }
48
        // if (count($assets) != count($assets, COUNT_RECURSIVE)) {
49
        //     throw new \Exception('Assets of '.static::class.' must be a one-dimensional array');
50
        // }
51
52
        $this->assets = $assets;
53
54
        return $this;
55
    }
56
    */
57
}
58