Passed
Push — 2.x ( ed3db8...f88ed3 )
by Terry
01:57
created

ComponentTrait::isIpComponent()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
nc 5
nop 0
dl 0
loc 30
rs 9.3888
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Kernel;
14
15
use Shieldon\Firewall\Component\ComponentInterface;
16
17
/*
18
 * @since 1.0.0
19
 */
20
trait ComponentTrait
21
{
22
    /**
23
     * Container for Shieldon components.
24
     *
25
     * @var array
26
     */
27
    public $component = [];
28
29
    /**
30
     * Get a component instance from component's container.
31
     *
32
     * @param string $name The component's class name.
33
     *
34
     * @return ComponentInterface|null
35
     */
36
    abstract function getComponent(string $name);
37
38
    /**
39
     * Initialize components.
40
     *
41
     * @return void
42
     */
43
    protected function initComponents()
44
    {
45
        foreach (array_keys($this->component) as $name) {
46
            $this->component[$name]->setIp($this->ip);
47
            $this->component[$name]->setRdns($this->rdns);
48
49
            // Apply global strict mode to all components by `strictMode()` if nesscessary.
50
            if (isset($this->strictMode)) {
51
                $this->component[$name]->setStrict($this->strictMode);
52
            }
53
        }
54
    }
55
56
    /**
57
     * Check if current IP is trusted or not.
58
     *
59
     * @return bool
60
     */
61
    protected function isTrustedBot()
62
    {
63
        if ($this->getComponent('TrustedBot')) {
64
65
            // We want to put all the allowed robot into the rule list, so that the checking of IP's resolved hostname 
66
            // is no more needed for that IP.
67
            if ($this->getComponent('TrustedBot')->isAllowed()) {
0 ignored issues
show
Bug introduced by
The method isAllowed() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\Ip or Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isAllowed()) {
Loading history...
68
69
                if ($this->getComponent('TrustedBot')->isGoogle()) {
0 ignored issues
show
Bug introduced by
The method isGoogle() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
                if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isGoogle()) {
Loading history...
70
                    // Add current IP into allowed list, because it is from real Google domain.
71
                    $this->action(
0 ignored issues
show
Bug introduced by
It seems like action() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                    $this->/** @scrutinizer ignore-call */ 
72
                           action(
Loading history...
72
                        self::ACTION_ALLOW,
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...nentTrait::ACTION_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
                        self::REASON_IS_GOOGLE
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...Trait::REASON_IS_GOOGLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
74
                    );
75
76
                } elseif ($this->getComponent('TrustedBot')->isBing()) {
0 ignored issues
show
Bug introduced by
The method isBing() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isBing()) {
Loading history...
77
                    // Add current IP into allowed list, because it is from real Bing domain.
78
                    $this->action(
79
                        self::ACTION_ALLOW,
80
                        self::REASON_IS_BING
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...ntTrait::REASON_IS_BING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
                    );
82
83
                } elseif ($this->getComponent('TrustedBot')->isYahoo()) {
0 ignored issues
show
Bug introduced by
The method isYahoo() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isYahoo()) {
Loading history...
84
                    // Add current IP into allowed list, because it is from real Yahoo domain.
85
                    $this->action(
86
                        self::ACTION_ALLOW,
87
                        self::REASON_IS_YAHOO
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...tTrait::REASON_IS_YAHOO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
                    );
89
90
                } else {
91
                    // Add current IP into allowed list, because you trust it.
92
                    // You have already defined it in the settings.
93
                    $this->action(
94
                        self::ACTION_ALLOW,
95
                        self::REASON_IS_SEARCH_ENGINE
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...REASON_IS_SEARCH_ENGINE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
96
                    );
97
                }
98
                // Allowed robots not join to our traffic handler.
99
                $this->result = self::RESPONSE_ALLOW;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The constant Shieldon\Firewall\Kernel...ntTrait::RESPONSE_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
100
                return true;
101
            }
102
        }
103
        return false;
104
    }
105
106
    /**
107
     * Check whether the IP is fake search engine or not.
108
     * The method "isTrustedBot()" must be executed before this method.
109
     *
110
     * @return bool
111
     */
112
    protected function isFakeRobot(): bool
113
    {
114
        if ($this->getComponent('TrustedBot')) {
115
            if ($this->getComponent('TrustedBot')->isFakeRobot()) {
0 ignored issues
show
Bug introduced by
The method isFakeRobot() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isFakeRobot()) {
Loading history...
116
                $this->action(
117
                    self::ACTION_DENY,
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...onentTrait::ACTION_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
118
                    self::REASON_COMPONENT_TRUSTED_ROBOT
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...COMPONENT_TRUSTED_ROBOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
119
                );
120
                $this->result = self::RESPONSE_DENY;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...entTrait::RESPONSE_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
121
                return true;
122
            }
123
        }
124
        return false;
125
    }
126
127
    /**
128
     * Check whether the IP is handled by IP compoent or not.
129
     *
130
     * @return bool
131
     */
132
    protected function isIpComponent(): bool
133
    {
134
        if ($this->getComponent('Ip')) {
135
136
            $result = $this->getComponent('Ip')->check();
0 ignored issues
show
Bug introduced by
The method check() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\Ip. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
            $result = $this->getComponent('Ip')->/** @scrutinizer ignore-call */ check();
Loading history...
137
            $actionCode = self::ACTION_DENY;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...onentTrait::ACTION_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
138
139
            if (!empty($result)) {
140
141
                switch ($result['status']) {
142
143
                    case 'allow':
144
                        $actionCode = self::ACTION_ALLOW;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...nentTrait::ACTION_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
145
                        $reasonCode = $result['code'];
146
                        break;
147
    
148
                    case 'deny':
149
                        $actionCode = self::ACTION_DENY;
150
                        $reasonCode = $result['code']; 
151
                        break;
152
                }
153
154
                $this->action($actionCode, $reasonCode);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $reasonCode does not seem to be defined for all execution paths leading up to this point.
Loading history...
155
156
                // $resultCode = $actionCode
157
                return $this->result = $this->sessionHandler($actionCode);
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like sessionHandler() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
                return $this->result = $this->/** @scrutinizer ignore-call */ sessionHandler($actionCode);
Loading history...
158
                return true;
0 ignored issues
show
Unused Code introduced by
return true is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
159
            }
160
        }
161
        return false;
162
    }
163
}
164