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 |
||
29 | class ApplicationPackage implements RegistrationInterface |
||
30 | { |
||
31 | /** @var array $config */ |
||
32 | private $config; |
||
33 | |||
34 | /** @var Router $router */ |
||
35 | private $router; |
||
36 | |||
37 | /** @var bool $i18nEnabledSite */ |
||
38 | private $i18nEnabledSite = false; |
||
39 | |||
40 | /** @var array $supportedLocales */ |
||
41 | private $supportedLocales = []; |
||
42 | |||
43 | /** |
||
44 | * ApplicationPackage constructor. |
||
45 | * @param array $config |
||
46 | * @param \League\Route\Router $router |
||
47 | */ |
||
48 | 8 | public function __construct(array $config, Router $router) |
|
53 | |||
54 | /** |
||
55 | * @param Container $c |
||
56 | */ |
||
57 | 8 | public function addToContainer(Container $c) |
|
58 | { |
||
59 | 8 | $this->setConfigArray($c); |
|
60 | 8 | $this->setLocale($c); |
|
61 | 8 | $this->setupLogs($c); |
|
62 | 8 | $this->setupPdoConnection($c); |
|
63 | 8 | $this->setupViewEngine($c); |
|
64 | 8 | $this->setupTranslator($c); |
|
65 | 8 | $this->setupModules($c); |
|
66 | 8 | $this->setupDownloadController($c); |
|
67 | 8 | } |
|
68 | |||
69 | /** |
||
70 | * @param Container $c |
||
71 | */ |
||
72 | 8 | private function setConfigArray(Container $c) |
|
78 | |||
79 | /** |
||
80 | * @param Container $c |
||
81 | */ |
||
82 | 8 | private function setLocale(Container $c) |
|
90 | |||
91 | /** |
||
92 | * @param Container $c |
||
93 | */ |
||
94 | 8 | private function setupViewEngine(Container $c) |
|
154 | |||
155 | /** |
||
156 | * @param Container $c |
||
157 | */ |
||
158 | 8 | private function setupModules(Container $c) |
|
198 | |||
199 | /** |
||
200 | * @param Container $c |
||
201 | */ |
||
202 | 8 | private function setupTranslator(Container $c) |
|
219 | |||
220 | |||
221 | /** |
||
222 | * @param Container $c |
||
223 | */ |
||
224 | 8 | private function setupPdoConnection(Container $c) |
|
225 | { |
||
226 | // set up a db connection |
||
227 | $c[PDO::class] = $c->factory(function (Container $c): PDO { |
||
228 | 1 | $credentials = $c->get('db'); |
|
229 | 1 | $host = $credentials['host']; |
|
230 | 1 | $db = $credentials['database']; |
|
231 | 1 | $user = $credentials['user']; |
|
232 | 1 | $pass = $credentials['pass']; |
|
233 | |||
234 | 1 | $dbConnection = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $pass, [ |
|
235 | 1 | PDO::ATTR_EMULATE_PREPARES => false, |
|
236 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
237 | ]); |
||
238 | |||
239 | 1 | return $dbConnection; |
|
240 | 8 | }); |
|
241 | 8 | } |
|
242 | |||
243 | /** |
||
244 | * @param Container $c |
||
245 | */ |
||
246 | 8 | private function setupDownloadController(Container $c): void |
|
247 | { |
||
248 | 8 | $uploadDirectory = $c->get('uploads_dir'); |
|
249 | 8 | $c[DownloadController::class] = new DownloadController($uploadDirectory); |
|
250 | 8 | $strategy = new JsonStrategy(new ResponseFactory()); |
|
251 | 8 | $strategy->setContainer($c); |
|
252 | 8 | $this->router->map('GET', '/download', [DownloadController::class, 'downloadAction'])->setStrategy($strategy); |
|
253 | 8 | } |
|
254 | |||
255 | /** |
||
256 | * @param Container $c |
||
257 | */ |
||
258 | 8 | private function setupLogs(Container $c) |
|
277 | |||
278 | /** |
||
279 | * @return string |
||
280 | */ |
||
281 | 1 | public function getEntityPath(): string |
|
285 | |||
286 | /** |
||
287 | * @return bool |
||
288 | */ |
||
289 | 1 | public function hasEntityPath(): bool |
|
293 | } |
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.