Completed
Push — master ( 91fa89...e21a82 )
by H
01:43
created

IpRangeVoter::vote()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 15
nc 14
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Hongliang\Defender\Voter;
4
5
use Hongliang\Defender\Utility\IpTools;
6
7
class IpRangeVoter extends BaseVoter implements VoterInterface
8
{
9
    public function vote($ip = null)
10
    {
11
        $ip = $ip ?: $this->target;
12
        if (!$ip) {
13
            return false;
14
        }
15
        // convert ip to number
16
        $long = ip2long($ip);
17
18
        // get bad ip ranges
19
        $ips = $this->getAssets();
20
        if ($this->assetsBuilt) {
21
            foreach ($ips as $ipRange) {
22
                if ($long <= $ipRange[1] && $long >= $ipRange[0]) {
23
                    return true;
24
                }
25
            }
26
        } else {
27
            foreach ($ips as $ipRange) {
28
                if ($long <= ip2long($ipRange[1]) && $long >= ip2long($ipRange[0])) {
29
                    return true;
30
                }
31
            }
32
        }
33
34
        return false;
35
    }
36
37
    protected function setDefaultTarget()
38
    {
39
        $this->target = IpTools::getClientIp();
40
41
        return $this;
42
    }
43
44
    protected function getDefaultAssets()
45
    {
46
        return [
47
            ['59.62.0.0', '59.63.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
48
            ['59.172.0.0', '59.173.255.255'], // CHINANET Hubei province network
49
            ['61.146.178.0', '61.146.178.255'], // MAINT-CHINANET-GD
50
            ['62.210.0.0', '62.210.255.255'], // IE-POOL-BUSINESS-HOSTING Franch
51
            ['86.123.0.0', '86.123.255.255'], // RDS Residential ROMANIA
52
            ['89.122.0.0', '89.122.255.255'], // Romtelecom Data Network Romania
53
            ['106.4.0.0', '106.7.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
54
            ['115.148.0.0', '115.254.255.255'], // many attempts, just block them all
55
            ['117.21.0.0', '117.21.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
56
            ['117.41.0.0', '117.41.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
57
            ['146.148.128.0', '146.148.255.255'], // GCHAO LLC
58
            ['148.251.0.0', '148.251.255.255'], // Hetzner Online GmbH
59
            ['178.239.165.0', '178.239.165.255'], // COBALT-NETWORKS English
60
            ['182.96.0.0', '182.111.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
61
            ['218.64.0.0', '218.65.127.255'], // CHINANET JIANGXI PROVINCE NETWORK
62
            ['202.75.32.0', '202.75.63.254'], // TM VADS DC Hosting
63
            ['222.208.0.0', '222.215.255.255'], // CHINANET Sichuan province network
64
        ];
65
    }
66
67
    protected $assetsBuilt = false;
68
69
    public function getAssetsBuilt()
70
    {
71
        return $this->assetsBuilt;
72
    }
73
74
    public function setAssetsBuilt($assetsBuilt)
75
    {
76
        $this->assetsBuilt = $assetsBuilt;
77
78
        return $this;
79
    }
80
81
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
82
    public function setAssets($assets)
83
    {
84
        if (!is_array($assets)) {
85
            throw new \Exception(static::class.' assets must be an array');
86
        }
87
        $this->assets = $assets;
88
89
        return $this;
90
    }
91
    */
92
}
93