Passed
Push — 2.x ( f88ed3...8d739d )
by Terry
02:02
created

ComponentTrait::getComponent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
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
     * Set a commponent.
31
     *
32
     * @param ComponentProvider $instance
33
     *
34
     * @return void
35
     */
36
    public function setComponent(ComponentProvider $instance): void
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Kernel\ComponentProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
    {
38
        $class = $this->getClassName($instance);
0 ignored issues
show
Bug introduced by
It seems like getClassName() 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

38
        /** @scrutinizer ignore-call */ 
39
        $class = $this->getClassName($instance);
Loading history...
39
        $this->component[$class] = $instance;
40
    }
41
42
    /**
43
     * Get a component instance from component's container.
44
     *
45
     * @param string $name The component's class name.
46
     *
47
     * @return ComponentInterface|null
48
     */
49
    public function getComponent(string $name)
50
    {
51
        if (!isset($this->component[$name])) {
52
            return null;
53
        }
54
55
        return $this->component[$name];
56
    }
57
58
    /*
59
    |--------------------------------------------------------------------------
60
    | Stage in Kernel
61
    |--------------------------------------------------------------------------
62
    | The below methods are used in "process" method in Kernel.
63
    */
64
65
    /**
66
     * Initialize components.
67
     *
68
     * @return void
69
     */
70
    protected function initComponents()
71
    {
72
        foreach (array_keys($this->component) as $name) {
73
            $this->component[$name]->setIp($this->ip);
74
            $this->component[$name]->setRdns($this->rdns);
75
76
            // Apply global strict mode to all components by `strictMode()` if nesscessary.
77
            if (isset($this->strictMode)) {
78
                $this->component[$name]->setStrict($this->strictMode);
79
            }
80
        }
81
    }
82
83
    /**
84
     * Check if current IP is trusted or not.
85
     *
86
     * @return bool
87
     */
88
    protected function isTrustedBot()
89
    {
90
        if ($this->getComponent('TrustedBot')) {
91
92
            // We want to put all the allowed robot into the rule list, so that the checking of IP's resolved hostname 
93
            // is no more needed for that IP.
94
            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

94
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isAllowed()) {
Loading history...
95
96
                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

96
                if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isGoogle()) {
Loading history...
97
                    // Add current IP into allowed list, because it is from real Google domain.
98
                    $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

98
                    $this->/** @scrutinizer ignore-call */ 
99
                           action(
Loading history...
99
                        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...
100
                        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...
101
                    );
102
103
                } 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

103
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isBing()) {
Loading history...
104
                    // Add current IP into allowed list, because it is from real Bing domain.
105
                    $this->action(
106
                        self::ACTION_ALLOW,
107
                        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...
108
                    );
109
110
                } 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

110
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isYahoo()) {
Loading history...
111
                    // Add current IP into allowed list, because it is from real Yahoo domain.
112
                    $this->action(
113
                        self::ACTION_ALLOW,
114
                        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...
115
                    );
116
117
                } else {
118
                    // Add current IP into allowed list, because you trust it.
119
                    // You have already defined it in the settings.
120
                    $this->action(
121
                        self::ACTION_ALLOW,
122
                        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...
123
                    );
124
                }
125
                // Allowed robots not join to our traffic handler.
126
                $this->result = self::RESPONSE_ALLOW;
0 ignored issues
show
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...
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...
127
                return true;
128
            }
129
        }
130
        return false;
131
    }
132
133
    /**
134
     * Check whether the IP is fake search engine or not.
135
     * The method "isTrustedBot()" must be executed before this method.
136
     *
137
     * @return bool
138
     */
139
    protected function isFakeRobot(): bool
140
    {
141
        if ($this->getComponent('TrustedBot')) {
142
            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

142
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isFakeRobot()) {
Loading history...
143
                $this->action(
144
                    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...
145
                    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...
146
                );
147
                $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...
148
                return true;
149
            }
150
        }
151
        return false;
152
    }
153
154
    /**
155
     * Check whether the IP is handled by IP compoent or not.
156
     *
157
     * @return bool
158
     */
159
    protected function isIpComponent(): bool
160
    {
161
        if ($this->getComponent('Ip')) {
162
163
            $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

163
            $result = $this->getComponent('Ip')->/** @scrutinizer ignore-call */ check();
Loading history...
164
            $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...
165
166
            if (!empty($result)) {
167
168
                switch ($result['status']) {
169
170
                    case 'allow':
171
                        $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...
172
                        $reasonCode = $result['code'];
173
                        break;
174
    
175
                    case 'deny':
176
                        $actionCode = self::ACTION_DENY;
177
                        $reasonCode = $result['code']; 
178
                        break;
179
                }
180
181
                $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...
182
183
                // $resultCode = $actionCode
184
                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

184
                return $this->result = $this->/** @scrutinizer ignore-call */ sessionHandler($actionCode);
Loading history...
185
                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...
186
            }
187
        }
188
        return false;
189
    }
190
}
191