| Total Complexity | 44 |
| Total Lines | 250 |
| 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 |
||
| 13 | class Laravel |
||
| 14 | { |
||
| 15 | /**@var Container */ |
||
| 16 | protected $app; |
||
| 17 | |||
| 18 | /**@var Container */ |
||
| 19 | protected $snapshotApp; |
||
| 20 | |||
| 21 | /**@var ReflectionApp */ |
||
| 22 | protected $reflectionApp; |
||
| 23 | |||
| 24 | /**@var HttpKernel */ |
||
| 25 | protected $kernel; |
||
| 26 | |||
| 27 | /**@var array */ |
||
| 28 | protected $conf = []; |
||
| 29 | |||
| 30 | /**@var array */ |
||
| 31 | protected static $staticBlackList = [ |
||
| 32 | '/index.php' => 1, |
||
| 33 | '/.htaccess' => 1, |
||
| 34 | '/web.config' => 1, |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /**@var array */ |
||
| 38 | private $rawGlobals = []; |
||
| 39 | |||
| 40 | /**@var CleanerManager */ |
||
| 41 | protected $cleanerManager; |
||
| 42 | |||
| 43 | public function __construct(array $conf = []) |
||
| 44 | { |
||
| 45 | $this->conf = $conf; |
||
| 46 | |||
| 47 | // Merge $_ENV $_SERVER |
||
| 48 | $server = isset($this->conf['_SERVER']) ? $this->conf['_SERVER'] : []; |
||
| 49 | $env = isset($this->conf['_ENV']) ? $this->conf['_ENV'] : []; |
||
| 50 | $this->rawGlobals['_SERVER'] = array_merge($_SERVER, $server); |
||
| 51 | $this->rawGlobals['_ENV'] = array_merge($_ENV, $env); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function prepareLaravel() |
||
| 55 | { |
||
| 56 | list($this->app, $this->kernel) = $this->createAppKernel(); |
||
| 57 | |||
| 58 | $this->reflectionApp = new ReflectionApp($this->app); |
||
| 59 | |||
| 60 | $this->saveSnapshot(); |
||
| 61 | |||
| 62 | // Create cleaner manager |
||
| 63 | $this->cleanerManager = new CleanerManager($this->app, $this->conf); |
||
| 64 | } |
||
| 65 | |||
| 66 | protected function saveSnapshot() |
||
| 67 | { |
||
| 68 | $this->snapshotApp = clone $this->app; |
||
| 69 | |||
| 70 | $instances = $this->reflectionApp->instances(); |
||
| 71 | |||
| 72 | foreach ($instances as $key => $value) { |
||
| 73 | $this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function createAppKernel() |
||
| 78 | { |
||
| 79 | // Register autoload |
||
| 80 | self::autoload($this->conf['root_path']); |
||
| 81 | |||
| 82 | // Make kernel for Laravel |
||
| 83 | $app = require $this->conf['root_path'] . '/bootstrap/app.php'; |
||
| 84 | $kernel = $this->isLumen() ? null : $app->make(HttpKernel::class); |
||
| 85 | |||
| 86 | // Load all Configurations for Lumen |
||
| 87 | if ($this->isLumen()) { |
||
| 88 | $this->configureLumen($app); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Boot |
||
| 92 | if ($this->isLumen()) { |
||
| 93 | if (method_exists($app, 'boot')) { |
||
| 94 | $app->boot(); |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | $app->make(ConsoleKernel::class)->bootstrap(); |
||
| 98 | } |
||
| 99 | |||
| 100 | return [$app, $kernel]; |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function configureLumen(Container $app) |
||
| 104 | { |
||
| 105 | $cfgPaths = [ |
||
| 106 | // Framework default configuration |
||
| 107 | $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/', |
||
| 108 | // App configuration |
||
| 109 | $this->conf['root_path'] . '/config/', |
||
| 110 | ]; |
||
| 111 | |||
| 112 | $keys = []; |
||
| 113 | foreach ($cfgPaths as $cfgPath) { |
||
| 114 | $configs = (array)glob($cfgPath . '*.php'); |
||
| 115 | foreach ($configs as $config) { |
||
| 116 | $config = substr(basename($config), 0, -4); |
||
| 117 | $keys[$config] = $config; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | foreach ($keys as $key) { |
||
| 122 | $app->configure($key); |
||
|
|
|||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | public static function autoload($rootPath) |
||
| 127 | { |
||
| 128 | $autoload = $rootPath . '/bootstrap/autoload.php'; |
||
| 129 | if (file_exists($autoload)) { |
||
| 130 | require_once $autoload; |
||
| 131 | } else { |
||
| 132 | require_once $rootPath . '/vendor/autoload.php'; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getRawGlobals() |
||
| 139 | } |
||
| 140 | |||
| 141 | public function handleDynamic(IlluminateRequest $request) |
||
| 142 | { |
||
| 143 | ob_start(); |
||
| 144 | |||
| 145 | if ($this->isLumen()) { |
||
| 146 | $response = $this->app->dispatch($request); |
||
| 147 | if ($response instanceof StreamedResponse) { |
||
| 148 | $content = ''; |
||
| 149 | $response = $response->sendContent(); |
||
| 150 | } elseif ($response instanceof SymfonyResponse) { |
||
| 151 | $content = $response->getContent(); |
||
| 152 | } else { |
||
| 153 | $content = (string)$response; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->reflectionApp->callTerminableMiddleware($response); |
||
| 157 | } else { |
||
| 158 | $response = $this->kernel->handle($request); |
||
| 159 | if ($response instanceof StreamedResponse) { |
||
| 160 | $content = ''; |
||
| 161 | $response = $response->sendContent(); |
||
| 162 | } else { |
||
| 163 | $content = $response->getContent(); |
||
| 164 | } |
||
| 165 | $this->kernel->terminate($request, $response); |
||
| 166 | } |
||
| 167 | |||
| 168 | // prefer content in response, secondly ob |
||
| 169 | if (strlen($content) === 0 && ob_get_length() > 0) { |
||
| 170 | $obContent = ob_get_contents(); |
||
| 171 | if ($response instanceof StreamedResponse) { |
||
| 172 | $reflect = new \ReflectionClass(StreamedResponse::class); |
||
| 173 | $contentProperty = $reflect->getProperty('content'); |
||
| 174 | $contentProperty->setAccessible(true); |
||
| 175 | $contentProperty->setValue($response, $obContent); |
||
| 176 | } else { |
||
| 177 | $response->setContent($obContent); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | ob_end_clean(); |
||
| 182 | |||
| 183 | return $response; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function handleStatic(IlluminateRequest $request) |
||
| 187 | { |
||
| 188 | $uri = $request->getRequestUri(); |
||
| 189 | if (isset(self::$staticBlackList[$uri])) { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | $publicPath = $this->conf['static_path']; |
||
| 194 | $requestFile = $publicPath . $uri; |
||
| 195 | if (is_file($requestFile)) { |
||
| 196 | return $this->createStaticResponse($requestFile, $request); |
||
| 197 | } elseif (is_dir($requestFile)) { |
||
| 198 | $indexFile = $this->lookupIndex($requestFile); |
||
| 199 | if ($indexFile === false) { |
||
| 200 | return false; |
||
| 201 | } else { |
||
| 202 | return $this->createStaticResponse($indexFile, $request); |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | return false; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | protected function lookupIndex($folder) |
||
| 210 | { |
||
| 211 | $folder = rtrim($folder, '/') . '/'; |
||
| 212 | foreach (['index.html', 'index.htm'] as $index) { |
||
| 213 | $tmpFile = $folder . $index; |
||
| 214 | if (is_file($tmpFile)) { |
||
| 215 | return $tmpFile; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function createStaticResponse($requestFile, IlluminateRequest $request) |
||
| 227 | } |
||
| 228 | |||
| 229 | public function clean() |
||
| 230 | { |
||
| 231 | $this->cleanerManager->clean($this->snapshotApp, $this->reflectionApp); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function fireEvent($name, array $params = []) |
||
| 235 | { |
||
| 236 | $params[] = $this->app; |
||
| 237 | return method_exists($this->app['events'], 'dispatch') ? |
||
| 238 | $this->app['events']->dispatch($name, $params) : $this->app['events']->fire($name, $params); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function bindRequest(IlluminateRequest $request) |
||
| 242 | { |
||
| 243 | $this->app->instance('request', $request); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function bindSwoole($swoole) |
||
| 247 | { |
||
| 248 | $this->app->singleton('swoole', function () use ($swoole) { |
||
| 249 | return $swoole; |
||
| 250 | }); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function saveSession() |
||
| 254 | { |
||
| 255 | if ($this->app->offsetExists('session')) { |
||
| 256 | $this->app['session']->save(); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | protected function isLumen() |
||
| 263 | } |
||
| 264 | } |
||
| 265 |
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.