Total Complexity | 41 |
Total Lines | 234 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Laravel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Laravel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Laravel |
||
13 | { |
||
14 | /**@var Container */ |
||
15 | protected $app; |
||
16 | |||
17 | /**@var Container */ |
||
18 | protected $snapshotApp; |
||
19 | |||
20 | /**@var ReflectionApp */ |
||
21 | protected $reflectionApp; |
||
22 | |||
23 | /**@var HttpKernel */ |
||
24 | protected $kernel; |
||
25 | |||
26 | /**@var array */ |
||
27 | protected $conf = []; |
||
28 | |||
29 | /**@var array */ |
||
30 | protected static $staticBlackList = [ |
||
31 | '/index.php' => 1, |
||
32 | '/.htaccess' => 1, |
||
33 | '/web.config' => 1, |
||
34 | ]; |
||
35 | |||
36 | /**@var array */ |
||
37 | private $rawGlobals = []; |
||
38 | |||
39 | /**@var CleanerManager */ |
||
40 | protected $cleanerManager; |
||
41 | |||
42 | public function __construct(array $conf = []) |
||
43 | { |
||
44 | $this->conf = $conf; |
||
45 | |||
46 | // Merge $_ENV $_SERVER |
||
47 | $server = isset($this->conf['_SERVER']) ? $this->conf['_SERVER'] : []; |
||
48 | $env = isset($this->conf['_ENV']) ? $this->conf['_ENV'] : []; |
||
49 | $this->rawGlobals['_SERVER'] = array_merge($_SERVER, $server); |
||
50 | $this->rawGlobals['_ENV'] = array_merge($_ENV, $env); |
||
51 | } |
||
52 | |||
53 | public function prepareLaravel() |
||
54 | { |
||
55 | list($this->app, $this->kernel) = $this->createAppKernel(); |
||
56 | |||
57 | $this->reflectionApp = new ReflectionApp($this->app); |
||
58 | |||
59 | $this->saveSnapshot(); |
||
60 | |||
61 | // Create cleaner manager |
||
62 | $this->cleanerManager = new CleanerManager($this->app, $this->conf); |
||
63 | } |
||
64 | |||
65 | protected function saveSnapshot() |
||
66 | { |
||
67 | $this->snapshotApp = clone $this->app; |
||
68 | |||
69 | $instances = $this->reflectionApp->instances(); |
||
70 | |||
71 | foreach ($instances as $key => $value) { |
||
72 | $this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | protected function createAppKernel() |
||
77 | { |
||
78 | // Register autoload |
||
79 | self::autoload($this->conf['root_path']); |
||
80 | |||
81 | // Make kernel for Laravel |
||
82 | $app = require $this->conf['root_path'] . '/bootstrap/app.php'; |
||
83 | $kernel = $this->isLumen() ? null : $app->make(HttpKernel::class); |
||
84 | |||
85 | // Load all Configurations for Lumen |
||
86 | if ($this->isLumen()) { |
||
87 | $this->configureLumen($app); |
||
88 | } |
||
89 | |||
90 | // Boot |
||
91 | if ($this->isLumen()) { |
||
92 | if (method_exists($app, 'boot')) { |
||
93 | $app->boot(); |
||
94 | } |
||
95 | } else { |
||
96 | $app->make(ConsoleKernel::class)->bootstrap(); |
||
97 | } |
||
98 | |||
99 | return [$app, $kernel]; |
||
100 | } |
||
101 | |||
102 | protected function configureLumen(Container $app) |
||
103 | { |
||
104 | $cfgPaths = [ |
||
105 | // Framework default configuration |
||
106 | $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/', |
||
107 | // App configuration |
||
108 | $this->conf['root_path'] . '/config/', |
||
109 | ]; |
||
110 | |||
111 | $keys = []; |
||
112 | foreach ($cfgPaths as $cfgPath) { |
||
113 | $configs = (array)glob($cfgPath . '*.php'); |
||
114 | foreach ($configs as $config) { |
||
115 | $config = substr(basename($config), 0, -4); |
||
116 | $keys[$config] = $config; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | foreach ($keys as $key) { |
||
121 | $app->configure($key); |
||
|
|||
122 | } |
||
123 | } |
||
124 | |||
125 | public static function autoload($rootPath) |
||
126 | { |
||
127 | $autoload = $rootPath . '/bootstrap/autoload.php'; |
||
128 | if (file_exists($autoload)) { |
||
129 | require_once $autoload; |
||
130 | } else { |
||
131 | require_once $rootPath . '/vendor/autoload.php'; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | public function getRawGlobals() |
||
136 | { |
||
137 | return $this->rawGlobals; |
||
138 | } |
||
139 | |||
140 | public function handleDynamic(IlluminateRequest $request) |
||
141 | { |
||
142 | ob_start(); |
||
143 | |||
144 | if ($this->isLumen()) { |
||
145 | $response = $this->app->dispatch($request); |
||
146 | if ($response instanceof SymfonyResponse) { |
||
147 | $content = $response->getContent(); |
||
148 | } else { |
||
149 | $content = (string)$response; |
||
150 | } |
||
151 | |||
152 | $this->reflectionApp->callTerminableMiddleware($response); |
||
153 | } else { |
||
154 | $response = $this->kernel->handle($request); |
||
155 | $content = $response->getContent(); |
||
156 | $this->kernel->terminate($request, $response); |
||
157 | } |
||
158 | |||
159 | // prefer content in response, secondly ob |
||
160 | if (strlen($content) === 0 && ob_get_length() > 0) { |
||
161 | $response->setContent(ob_get_contents()); |
||
162 | } |
||
163 | |||
164 | ob_end_clean(); |
||
165 | |||
166 | return $response; |
||
167 | } |
||
168 | |||
169 | public function handleStatic(IlluminateRequest $request) |
||
170 | { |
||
171 | $uri = $request->getRequestUri(); |
||
172 | if (isset(self::$staticBlackList[$uri])) { |
||
173 | return false; |
||
174 | } |
||
175 | |||
176 | $publicPath = $this->conf['static_path']; |
||
177 | $requestFile = $publicPath . $uri; |
||
178 | if (is_file($requestFile)) { |
||
179 | return $this->createStaticResponse($requestFile, $request); |
||
180 | } elseif (is_dir($requestFile)) { |
||
181 | $indexFile = $this->lookupIndex($requestFile); |
||
182 | if ($indexFile === false) { |
||
183 | return false; |
||
184 | } else { |
||
185 | return $this->createStaticResponse($indexFile, $request); |
||
186 | } |
||
187 | } else { |
||
188 | return false; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | protected function lookupIndex($folder) |
||
193 | { |
||
194 | $folder = rtrim($folder, '/') . '/'; |
||
195 | foreach (['index.html', 'index.htm'] as $index) { |
||
196 | $tmpFile = $folder . $index; |
||
197 | if (is_file($tmpFile)) { |
||
198 | return $tmpFile; |
||
199 | } |
||
200 | } |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | public function createStaticResponse($requestFile, IlluminateRequest $request) |
||
205 | { |
||
206 | $response = new BinaryFileResponse($requestFile); |
||
207 | $response->prepare($request); |
||
208 | $response->isNotModified($request); |
||
209 | return $response; |
||
210 | } |
||
211 | |||
212 | public function clean() |
||
213 | { |
||
214 | $this->cleanerManager->clean($this->snapshotApp, $this->reflectionApp); |
||
215 | } |
||
216 | |||
217 | public function fireEvent($name, array $params = []) |
||
218 | { |
||
219 | $params[] = $this->app; |
||
220 | return method_exists($this->app['events'], 'dispatch') ? |
||
221 | $this->app['events']->dispatch($name, $params) : $this->app['events']->fire($name, $params); |
||
222 | } |
||
223 | |||
224 | public function bindRequest(IlluminateRequest $request) |
||
225 | { |
||
226 | $this->app->instance('request', $request); |
||
227 | } |
||
228 | |||
229 | public function bindSwoole($swoole) |
||
233 | }); |
||
234 | } |
||
235 | |||
236 | public function saveSession() |
||
237 | { |
||
238 | if ($this->app->offsetExists('session')) { |
||
239 | $this->app['session']->save(); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | protected function isLumen() |
||
246 | } |
||
247 | } |
||
248 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.