Issues (1019)

src/Illuminate/CleanerManager.php (48 issues)

1
<?php
2
0 ignored issues
show
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
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
Missing doc comment for class CleanerManager
Loading history...
14
{
15
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
16
     * @var Container
17
     */
18
    protected $currentApp;
19
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
20
     * @var Container
21
     */
22
    protected $snapshotApp;
23
24
    /**@var ReflectionApp */
0 ignored issues
show
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
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
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
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
47
     */
48
    protected $whiteListControllers = [];
49
50
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
51
     * @var array
52
     */
53
    protected $config = [];
54
55
    /**
56
     * CleanerManager constructor.
57
     *
58
     * @param Container $currentApp
0 ignored issues
show
Missing parameter comment
Loading history...
59
     * @param Container $snapshotApp
0 ignored issues
show
Missing parameter comment
Loading history...
60
     * @param array $config
0 ignored issues
show
Expected 5 spaces after parameter type; 1 found
Loading history...
Missing parameter comment
Loading history...
61
     */
62
    public function __construct(Container $currentApp, Container $snapshotApp, array $config)
63
    {
64
        $this->currentApp = $currentApp;
65
        $this->snapshotApp = $snapshotApp;
66
        $this->reflectionApp = new ReflectionApp($this->currentApp);
67
        $this->config = $config;
68
        $this->registerCleaners(isset($this->config['cleaners']) ? $this->config['cleaners'] : []);
69
        $this->registerCleanProviders(isset($config['register_providers']) ? $config['register_providers'] : []);
70
        $this->registerCleanControllerWhiteList(isset($this->config['destroy_controllers']['excluded_list']) ? $this->config['destroy_controllers']['excluded_list'] : []);
71
    }
72
73
    /**
74
     * Register singleton cleaners to application container.
75
     * @param array $cleaners
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
Missing parameter comment
Loading history...
76
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
77
    protected function registerCleaners(array $cleaners)
78
    {
79
        $this->cleaners = array_unique(array_merge($cleaners, $this->cleaners));
80
        foreach ($this->cleaners as $class) {
81
            $this->currentApp->singleton($class, function () use ($class) {
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
82
                $cleaner = new $class($this->currentApp, $this->snapshotApp);
83
                if (!($cleaner instanceof BaseCleaner)) {
84
                    throw new \InvalidArgumentException(sprintf(
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
85
                            '%s must extend the abstract class %s',
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 28.
Loading history...
86
                            $cleaner,
0 ignored issues
show
$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

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