Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
19 | abstract class Base |
||
20 | { |
||
21 | |||
22 | protected $root_dir; |
||
23 | |||
24 | protected $pid_file; |
||
25 | |||
26 | public $base_config; |
||
27 | |||
28 | public $handler_config; |
||
29 | |||
30 | public $wrapper_config; |
||
31 | |||
32 | protected $kernel; |
||
33 | |||
34 | protected $tmp_autoloader; |
||
35 | |||
36 | protected $app; |
||
37 | |||
38 | protected $server; |
||
39 | |||
40 | protected $diactorosFactory; |
||
41 | |||
42 | /** |
||
43 | * For wrappers' events. |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $callbacks = []; |
||
47 | |||
48 | /** |
||
49 | * Start the server |
||
50 | * @codeCoverageIgnore |
||
51 | */ |
||
52 | public function start() |
||
56 | |||
57 | 20 | final public function init(array $configs) |
|
65 | |||
66 | 20 | public function prepareKernel() |
|
67 | { |
||
68 | // unregister temporary autoloader |
||
69 | 20 | foreach (spl_autoload_functions() as $function) { |
|
70 | 20 | spl_autoload_unregister($function); |
|
71 | 10 | } |
|
72 | |||
73 | 20 | if (file_exists(__DIR__ . '/../vendor/autoload.php')) { |
|
74 | 20 | require __DIR__ . '/../vendor/autoload.php'; |
|
75 | 10 | } else { |
|
76 | require $this->root_dir . '/bootstrap/autoload.php'; |
||
77 | } |
||
78 | 20 | View Code Duplication | if (isset($this->base_config['callbacks']['bootstraping'])) { |
|
|||
79 | 20 | foreach ($this->base_config['callbacks']['bootstraping'] as $callback) { |
|
80 | 20 | $callback($this); |
|
81 | 10 | } |
|
82 | 10 | } |
|
83 | 20 | $this->app = $this->getApp(); |
|
84 | |||
85 | 20 | if (isset($this->wrapper_config['environment_path'])) { |
|
86 | 20 | $this->app->useEnvironmentPath($this->wrapper_config['environment_path']); |
|
87 | 10 | } |
|
88 | |||
89 | 20 | $this->kernel = $this->app->make(\Illuminate\Contracts\Http\Kernel::class); |
|
90 | 20 | $virus = function () { |
|
91 | // Insert bofore BootProviders |
||
92 | 20 | array_splice($this->bootstrappers, -1, 0, [\Illuminate\Foundation\Bootstrap\SetRequestForConsole::class]); |
|
93 | 20 | }; |
|
94 | 20 | $virus = \Closure::bind($virus, $this->kernel, $this->kernel); |
|
95 | 20 | $virus(); |
|
96 | |||
97 | 20 | $this->kernel->bootstrap(); |
|
98 | 20 | chdir(public_path()); |
|
99 | 20 | $config = $this->app['config']->get('laravoole.base_config', []); |
|
100 | 20 | $this->app['config']->set('laravoole.base_config', array_merge($config, $this->base_config)); |
|
101 | |||
102 | 20 | View Code Duplication | if (isset($this->base_config['callbacks']['bootstraped'])) { |
103 | 20 | foreach ($this->base_config['callbacks']['bootstraped'] as $callback) { |
|
104 | 20 | $callback($this); |
|
105 | 10 | } |
|
106 | 10 | } |
|
107 | 20 | $this->events = $this->app['events']; |
|
108 | 20 | } |
|
109 | |||
110 | 20 | public function handleRequest($request, IlluminateRequest $illuminate_request = null) |
|
111 | { |
||
112 | 20 | clearstatcache(); |
|
113 | |||
114 | 20 | $kernel = $this->kernel; |
|
115 | |||
116 | try { |
||
117 | |||
118 | 20 | ob_start(); |
|
119 | |||
120 | 20 | if (!$illuminate_request) { |
|
121 | 20 | if ($request instanceof ServerRequestInterface) { |
|
122 | 8 | $request = (new HttpFoundationFactory)->createRequest($request); |
|
123 | 8 | $illuminate_request = IlluminateRequest::createFromBase($request); |
|
124 | 10 | } elseif ($request instanceof swoole_http_request) { |
|
125 | 8 | $illuminate_request = $this->convertRequest($request); |
|
126 | 4 | } else { |
|
127 | 11 | $illuminate_request = IlluminateRequest::createFromBase($request); |
|
128 | } |
||
129 | 10 | } |
|
130 | |||
131 | 20 | $this->events->fire('laravoole.requesting', [$illuminate_request]); |
|
132 | |||
133 | 20 | $illuminate_response = $kernel->handle($illuminate_request); |
|
134 | |||
135 | 20 | $content = $illuminate_response->getContent(); |
|
136 | |||
137 | 20 | if (strlen($content) === 0 && ob_get_length() > 0) { |
|
138 | $illuminate_response->setContent(ob_get_contents()); |
||
139 | } |
||
140 | |||
141 | 20 | ob_end_clean(); |
|
142 | |||
143 | 10 | } catch (\Exception $e) { |
|
144 | echo '[ERR] ' . $e->getFile() . '(' . $e->getLine() . '): ' . $e->getMessage() . PHP_EOL; |
||
145 | echo $e->getTraceAsString() . PHP_EOL; |
||
146 | } catch (\Throwable $e) { |
||
147 | echo '[ERR] ' . $e->getFile() . '(' . $e->getLine() . '): ' . $e->getMessage() . PHP_EOL; |
||
148 | echo $e->getTraceAsString() . PHP_EOL; |
||
149 | 20 | } finally { |
|
150 | 20 | if (isset($illuminate_response)) { |
|
151 | 20 | $kernel->terminate($illuminate_request, $illuminate_response); |
|
152 | 10 | } |
|
153 | 20 | $this->events->fire('laravoole.requested', [$illuminate_request, $illuminate_response]); |
|
154 | |||
155 | 20 | $this->clean($illuminate_request); |
|
156 | |||
157 | } |
||
158 | |||
159 | 20 | return $illuminate_response; |
|
160 | |||
161 | } |
||
162 | |||
163 | 8 | public function onPsrRequest(ServerRequestInterface $psrRequest) |
|
164 | { |
||
165 | 8 | $illuminate_response = $this->handleRequest($psrRequest); |
|
166 | 8 | if (!$this->diactorosFactory) { |
|
167 | 8 | $this->diactorosFactory = new DiactorosFactory; |
|
168 | 4 | } |
|
169 | 8 | return $this->diactorosFactory->createResponse($illuminate_response); |
|
170 | |||
171 | } |
||
172 | |||
173 | 8 | protected function convertRequest($request, $classname = IlluminateRequest::class) |
|
188 | |||
189 | 20 | protected function clean(IlluminateRequest $request) |
|
190 | { |
||
191 | 20 | if ($request->hasSession()) { |
|
211 | |||
212 | 20 | public function getApp() |
|
219 | |||
220 | 20 | protected function createApp() |
|
243 | |||
244 | } |
||
245 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.