Passed
Push — master ( d9a6d1...03a9e3 )
by Biao
04:01
created

CleanerManager::isLumen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\CookieCleaner;
9
use Hhxsv5\LaravelS\Illuminate\Cleaners\RequestCleaner;
10
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...
11
12
class CleanerManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CleanerManager
Loading history...
13
{
14
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
15
     * @var Container
16
     */
17
    protected $currentApp;
18
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
19
     * @var Container
20
     */
21
    protected $snapshotApp;
22
23
    /**@var ReflectionApp */
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...
24
    protected $reflectionApp;
25
26
    /**
27
     * All cleaners
28
     * @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...
29
     */
30
    protected $cleaners = [
31
        ConfigCleaner::class,
32
        CookieCleaner::class,
33
        RequestCleaner::class,
34
    ];
35
36
    /**
37
     * Service providers to be cleaned up
38
     * @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...
39
     */
40
    protected $providers = [];
41
42
    /**
43
     * White list of controllers to be destroyed
44
     * @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...
45
     */
46
    protected $whiteListControllers = [];
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var array
50
     */
51
    protected $config = [];
52
53
    /**
54
     * CleanerManager constructor.
55
     *
56
     * @param Container $currentApp
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
57
     * @param Container $snapshotApp
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @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...
59
     */
60
    public function __construct(Container $currentApp, Container $snapshotApp, array $config)
61
    {
62
        $this->currentApp = $currentApp;
63
        $this->snapshotApp = $snapshotApp;
64
        $this->reflectionApp = new ReflectionApp($this->currentApp);
65
        $this->config = $config;
66
        $this->registerCleaners(isset($this->config['cleaners']) ? $this->config['cleaners'] : []);
67
        $this->registerCleanProviders(isset($config['register_providers']) ? $config['register_providers'] : []);
68
        $this->registerCleanControllerWhiteList(isset($this->config['destroy_controllers']['excluded_list']) ? $this->config['destroy_controllers']['excluded_list'] : []);
69
    }
70
71
    /**
72
     * Register singleton cleaners to application container.
73
     * @param array $cleaners
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
74
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
75
    protected function registerCleaners(array $cleaners)
76
    {
77
        $this->cleaners = array_unique(array_merge($cleaners, $this->cleaners));
78
        foreach ($this->cleaners as $class) {
79
            $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...
80
                $cleaner = new $class($this->currentApp, $this->snapshotApp);
81
                if (!($cleaner instanceof BaseCleaner)) {
82
                    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...
83
                            '%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...
84
                            $cleaner,
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...
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

84
                            /** @scrutinizer ignore-type */ $cleaner,
Loading history...
85
                            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...
86
                        )
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...
87
                    );
88
                }
89
                return $cleaner;
90
            });
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...
91
        }
92
    }
93
94
    /**
95
     * Clean app after request finished.
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    public function clean()
98
    {
99
        foreach ($this->cleaners as $class) {
100
            /**@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...
101
            $cleaner = $this->currentApp->make($class);
102
            $cleaner->clean();
103
        }
104
    }
105
106
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $providers should have a doc-comment as per coding-style.
Loading history...
107
     * Register providers for cleaning.
108
     *
109
     * @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...
110
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
111
    protected function registerCleanProviders(array $providers = [])
112
    {
113
        $this->providers = $providers;
114
    }
115
116
    /**
117
     * Clean Providers.
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    public function cleanProviders()
120
    {
121
        $loadedProviders = $this->reflectionApp->loadedProviders();
122
123
        foreach ($this->providers as $provider) {
124
            if (class_exists($provider, false)) {
125
                if ($this->config['is_lumen']) {
126
                    unset($loadedProviders[get_class(new $provider($this->currentApp))]);
127
                }
128
129
                switch ($this->reflectionApp->registerMethodParameterCount()) {
130
                    case 1:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
131
                        $this->currentApp->register($provider);
132
                        break;
133
                    case 2:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
134
                        $this->currentApp->register($provider, true);
135
                        break;
136
                    case 3:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
137
                        $this->currentApp->register($provider, [], true);
138
                        break;
139
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
140
                        throw new \RuntimeException('The number of parameters of the register method is unknown.');
141
                }
142
            }
143
        }
144
145
        if ($this->config['is_lumen']) {
146
            $this->reflectionApp->setLoadedProviders($loadedProviders);
147
        }
148
    }
149
150
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $controllers should have a doc-comment as per coding-style.
Loading history...
151
     * Register white list of controllers for cleaning.
152
     *
153
     * @param array providers
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
154
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
155
    protected function registerCleanControllerWhiteList(array $controllers = [])
156
    {
157
        $controllers = array_unique($controllers);
158
        $this->whiteListControllers = array_combine($controllers, $controllers);
159
    }
160
161
    /**
162
     * Clean controllers.
163
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
164
    public function cleanControllers()
165
    {
166
        if ($this->config['is_lumen']) {
167
            return;
168
        }
169
170
        if (empty($this->config['destroy_controllers']['enable'])) {
171
            return;
172
        }
173
174
        /**@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...
175
        $route = $this->currentApp['router']->current();
176
        if (!$route) {
0 ignored issues
show
introduced by
$route is of type Illuminate\Routing\Route, thus it always evaluated to true.
Loading history...
177
            return;
178
        }
179
180
        if (isset($route->controller)) { // For Laravel 5.4+
181
            if (empty($this->whiteListControllers) || !isset($this->whiteListControllers[get_class($route->controller)])) {
182
                unset($route->controller);
183
            }
184
        } else {
185
            $reflection = new \ReflectionClass(get_class($route));
186
            if ($reflection->hasProperty('controller')) { // Laravel 5.3
187
                $controller = $reflection->getProperty('controller');
188
                $controller->setAccessible(true);
189
                if (empty($this->whiteListControllers) || (($instance = $controller->getValue($route)) && !isset($this->whiteListControllers[get_class($instance)]))) {
190
                    $controller->setValue($route, null);
191
                }
192
            }
193
        }
194
    }
195
}
196