Test Failed
Pull Request — PHP-8.x (#497)
by
unknown
03:35
created

CleanerManager   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 85
c 2
b 0
f 0
dl 0
loc 220
rs 9.68
wmc 34

8 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCleaners() 0 15 3
A shouldExcludeController() 0 16 4
B cleanControllers() 0 28 9
B cleanProviders() 0 28 8
A clean() 0 6 2
A __construct() 0 9 4
A registerCleanControllerWhiteList() 0 11 3
A registerCleanProviders() 0 3 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Hhxsv5\LaravelS\Illuminate\Cleaners\BaseCleaner;
6
use Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface;
7
use Hhxsv5\LaravelS\Illuminate\Cleaners\ConfigCleaner;
8
use Hhxsv5\LaravelS\Illuminate\Cleaners\ContainerCleaner;
9
use Hhxsv5\LaravelS\Illuminate\Cleaners\CookieCleaner;
10
use Hhxsv5\LaravelS\Illuminate\Cleaners\RequestCleaner;
11
use Illuminate\Container\Container;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container 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...
12
13
class CleanerManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CleanerManager
Loading history...
14
{
15
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
16
     * @var Container
17
     */
18
    protected $currentApp;
19
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
     * @var Container
21
     */
22
    protected $snapshotApp;
23
24
    /**@var ReflectionApp */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
25
    protected $reflectionApp;
26
27
    /**
28
     * All cleaners
29
     * @var CleanerInterface[]
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
30
     */
31
    protected $cleaners = [
32
        ContainerCleaner::class,
33
        ConfigCleaner::class,
34
        CookieCleaner::class,
35
        RequestCleaner::class,
36
    ];
37
38
    /**
39
     * Service providers to be cleaned up
40
     * @var array
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
41
     */
42
    protected $providers = [];
43
44
    /**
45
     * White list of controllers to be destroyed
46
     * @var array<string,bool>
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
47
     */
48
    protected $whiteListControllers = [];
49
50
    /**
51
     * White list of controller prefixes to be excluded
52
     * @var array<string,bool>
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
53
     */
54
    protected $whiteListPrefixes = [];
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @var array
58
     */
59
    protected $config = [];
60
61
    /**
62
     * CleanerManager constructor.
63
     *
64
     * @param Container $currentApp
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
65
     * @param Container $snapshotApp
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     * @param array $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
67
     */
68
    public function __construct(Container $currentApp, Container $snapshotApp, array $config)
69
    {
70
        $this->currentApp = $currentApp;
71
        $this->snapshotApp = $snapshotApp;
72
        $this->reflectionApp = new ReflectionApp($this->currentApp);
73
        $this->config = $config;
74
        $this->registerCleaners(isset($this->config['cleaners']) ? $this->config['cleaners'] : []);
75
        $this->registerCleanProviders(isset($config['register_providers']) ? $config['register_providers'] : []);
76
        $this->registerCleanControllerWhiteList(isset($this->config['destroy_controllers']['excluded_list']) ? $this->config['destroy_controllers']['excluded_list'] : []);
77
    }
78
79
    /**
80
     * Register singleton cleaners to application container.
81
     * @param array $cleaners
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
82
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
83
    protected function registerCleaners(array $cleaners)
84
    {
85
        $this->cleaners = array_unique(array_merge($cleaners, $this->cleaners));
86
        foreach ($this->cleaners as $class) {
87
            $this->currentApp->singleton($class, function () use ($class) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
88
                $cleaner = new $class($this->currentApp, $this->snapshotApp);
89
                if (!($cleaner instanceof BaseCleaner)) {
90
                    throw new \InvalidArgumentException(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
91
                            '%s must extend the abstract class %s',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 28.
Loading history...
92
                            $cleaner,
0 ignored issues
show
Bug introduced by
$cleaner of type object is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

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

92
                            /** @scrutinizer ignore-type */ $cleaner,
Loading history...
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 28.
Loading history...
93
                            BaseCleaner::class
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 28.
Loading history...
94
                        )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
95
                    );
96
                }
97
                return $cleaner;
98
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
99
        }
100
    }
101
102
    /**
103
     * Clean app after request finished.
104
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
105
    public function clean()
106
    {
107
        foreach ($this->cleaners as $class) {
108
            /**@var BaseCleaner $cleaner */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
109
            $cleaner = $this->currentApp->make($class);
110
            $cleaner->clean();
111
        }
112
    }
113
114
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $providers should have a doc-comment as per coding-style.
Loading history...
115
     * Register providers for cleaning.
116
     *
117
     * @param array providers
0 ignored issues
show
Bug introduced by
The type Hhxsv5\LaravelS\Illuminate\providers 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...
Coding Style Documentation introduced by
Missing parameter name
Loading history...
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    protected function registerCleanProviders(array $providers = [])
120
    {
121
        $this->providers = $providers;
122
    }
123
124
    /**
125
     * Clean Providers.
126
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
127
    public function cleanProviders()
128
    {
129
        $loadedProviders = $this->reflectionApp->loadedProviders();
130
131
        foreach ($this->providers as $provider) {
132
            if (class_exists($provider)) {
133
                if ($this->config['is_lumen']) {
134
                    unset($loadedProviders[get_class(new $provider($this->currentApp))]);
135
                }
136
137
                switch ($this->reflectionApp->registerMethodParameterCount()) {
138
                    case 1:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
139
                        $this->currentApp->register($provider);
140
                        break;
141
                    case 2:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
142
                        $this->currentApp->register($provider, true);
143
                        break;
144
                    case 3:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
145
                        $this->currentApp->register($provider, [], true);
146
                        break;
147
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
148
                        throw new \RuntimeException('The number of parameters of the register method is unknown.');
149
                }
150
            }
151
        }
152
153
        if ($this->config['is_lumen']) {
154
            $this->reflectionApp->setLoadedProviders($loadedProviders);
155
        }
156
    }
157
158
    /**
159
     * Register white list of controllers for cleaning.
160
     *
161
     * @param array $controllers
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
162
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
163
    protected function registerCleanControllerWhiteList(array $controllers = [])
164
    {
165
        $this->whiteListControllers = [];
166
        $this->whiteListPrefixes = [];
167
168
        foreach ($controllers as $controller) {
169
            if (str_ends_with($controller, '*')) {
170
                $prefix = substr($controller, 0, -1);
171
                $this->whiteListPrefixes[$prefix] = true; // 使用关联数组
172
            } else {
173
                $this->whiteListControllers[$controller] = true; // 统一使用 true 作为值
174
            }
175
        }
176
    }
177
178
    /**
179
     * Check if controller should be excluded from cleaning
180
     *
181
     * @param string $controllerClass
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
182
     * @return bool
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
183
     */
184
    protected function shouldExcludeController(string $controllerClass): bool
185
    {
186
        // 1. 精确匹配检查(O(1))
187
        if (isset($this->whiteListControllers[$controllerClass])) {
188
            return true;
189
        }
190
191
        // 2. 前缀匹配检查
192
        // 使用关联数组后,可以优化前缀匹配的性能
193
        foreach ($this->whiteListPrefixes as $prefix => $_) {
194
            if (strncmp($controllerClass, $prefix, strlen($prefix)) === 0) {
195
                return true;
196
            }
197
        }
198
199
        return false;
200
    }
201
202
    /**
203
     * Clean controllers.
204
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
205
    public function cleanControllers()
206
    {
207
        if ($this->config['is_lumen']) {
208
            return;
209
        }
210
211
        if (empty($this->config['destroy_controllers']['enable'])) {
212
            return;
213
        }
214
215
        /**@var \Illuminate\Routing\Route $route */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
216
        $route = $this->currentApp['router']->current();
217
        if (!$route) {
0 ignored issues
show
introduced by
$route is of type Illuminate\Routing\Route, thus it always evaluated to true.
Loading history...
218
            return;
219
        }
220
221
        if (isset($route->controller)) { // For Laravel 5.4+
222
            if (!$this->shouldExcludeController(get_class($route->controller))) {
223
                unset($route->controller);
224
            }
225
        } else {
226
            $reflection = new \ReflectionClass(get_class($route));
227
            if ($reflection->hasProperty('controller')) { // Laravel 5.3
228
                $controller = $reflection->getProperty('controller');
229
                $controller->setAccessible(true);
230
                $instance = $controller->getValue($route);
231
                if ($instance && !$this->shouldExcludeController(get_class($instance))) {
232
                    $controller->setValue($route, null);
233
                }
234
            }
235
        }
236
    }
237
}
238