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 |
||
23 | class ApplicationPackage implements RegistrationInterface |
||
24 | { |
||
25 | /** @var array $config */ |
||
26 | private $config; |
||
27 | |||
28 | /** @var Router $router */ |
||
29 | private $router; |
||
30 | |||
31 | /** @var bool $i18nEnabledSite */ |
||
32 | private $i18nEnabledSite = false; |
||
33 | |||
34 | /** @var array $supportedLocales */ |
||
35 | private $supportedLocales = []; |
||
36 | |||
37 | /** |
||
38 | * ApplicationPackage constructor. |
||
39 | * @param array $config |
||
40 | * @param \League\Route\Router $router |
||
41 | */ |
||
42 | 1 | public function __construct(array $config, Router $router) |
|
43 | { |
||
44 | 1 | $this->config = $config; |
|
45 | 1 | $this->router = $router; |
|
46 | 1 | } |
|
47 | |||
48 | /** |
||
49 | * @param Container $c |
||
50 | */ |
||
51 | 1 | public function addToContainer(Container $c) |
|
52 | { |
||
53 | 1 | $this->setConfigArray($c); |
|
54 | 1 | $this->setLocale($c); |
|
55 | 1 | $this->setupPdoConnection($c); |
|
56 | 1 | $this->setupViewEngine($c); |
|
57 | 1 | $this->setupTranslator($c); |
|
58 | 1 | $this->setupModules($c); |
|
59 | 1 | } |
|
60 | |||
61 | /** |
||
62 | * @param Container $c |
||
63 | */ |
||
64 | 1 | private function setConfigArray(Container $c) |
|
65 | { |
||
66 | 1 | foreach($this->config as $key => $value) |
|
67 | { |
||
68 | 1 | $c[$key] = $value; |
|
69 | } |
||
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * @param Container $c |
||
74 | */ |
||
75 | 1 | private function setLocale(Container $c) |
|
76 | { |
||
77 | 1 | $i18n = $c->get('i18n'); |
|
78 | 1 | $this->i18nEnabledSite = $i18n['enabled']; |
|
79 | 1 | $this->supportedLocales = $i18n['supported_locales']; |
|
80 | 1 | $defaultLocale = $i18n['default_locale']; |
|
81 | 1 | Locale::setDefault($defaultLocale); |
|
82 | 1 | } |
|
83 | |||
84 | /** |
||
85 | * @param Container $c |
||
86 | */ |
||
87 | 1 | private function setupViewEngine(Container $c) |
|
88 | { |
||
89 | // set up the view engine dependencies |
||
90 | 1 | $viewEngine = new PlatesEngine($c->get('viewFolder')); |
|
91 | |||
92 | 1 | $c[PlatesEngine::class] = $viewEngine; |
|
93 | |||
94 | View Code Duplication | $c[NotFoundDecorator::class] = $c->factory(function (Container $c) { |
|
|
|||
95 | 1 | $layout = $c->get('default_layout'); |
|
96 | 1 | $viewEngine = $c->get(PlatesEngine::class); |
|
97 | 1 | $notFoundDecorator = new NotFoundDecorator($viewEngine); |
|
98 | 1 | $notFoundDecorator->setLayout($layout); |
|
99 | |||
100 | 1 | return $notFoundDecorator; |
|
101 | 1 | }); |
|
102 | |||
103 | View Code Duplication | $c[NotAllowedDecorator::class] = $c->factory(function (Container $c) { |
|
104 | 1 | $layout = $c->get('default_layout'); |
|
105 | 1 | $viewEngine = $c->get(PlatesEngine::class); |
|
106 | 1 | $notAllowedDecorator = new NotAllowedDecorator($viewEngine); |
|
107 | 1 | $notAllowedDecorator->setLayout($layout); |
|
108 | |||
109 | 1 | return $notAllowedDecorator; |
|
110 | 1 | }); |
|
111 | |||
112 | $c[PlatesStrategy::class] = $c->factory(function (Container $c) { |
||
113 | 1 | $viewEngine = $c->get(PlatesEngine::class); |
|
114 | 1 | $notFoundDecorator = $c->get(NotFoundDecorator::class); |
|
115 | 1 | $notAllowedDecorator = $c->get(NotAllowedDecorator::class); |
|
116 | 1 | $layout = $c->get('default_layout'); |
|
117 | 1 | $strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout); |
|
118 | |||
119 | 1 | return $strategy; |
|
120 | 1 | }); |
|
121 | |||
122 | /** @var PlatesStrategy $strategy */ |
||
123 | 1 | $strategy = $c->get(PlatesStrategy::class); |
|
124 | 1 | $strategy->setContainer($c); |
|
125 | |||
126 | 1 | if ($this->i18nEnabledSite === true) { |
|
127 | 1 | $strategy->setI18nEnabled(true); |
|
128 | 1 | $strategy->setSupportedLocales($this->supportedLocales); |
|
129 | } |
||
130 | |||
131 | 1 | $this->router->setStrategy($strategy); |
|
132 | 1 | } |
|
133 | |||
134 | /** |
||
135 | * @param Container $c |
||
136 | */ |
||
137 | 1 | private function setupModules(Container $c) |
|
138 | { |
||
139 | // set up the modules and vendor package modules |
||
140 | 1 | $packages = $c->get('packages'); |
|
141 | 1 | $i18n = $c->get('i18n'); |
|
142 | /** @var Translator $translator */ |
||
143 | 1 | $translator = $c->get(Translator::class); |
|
144 | |||
145 | 1 | foreach ($packages as $packageName) { |
|
146 | 1 | if (class_exists($packageName)) { |
|
147 | /** @var RegistrationInterface $package */ |
||
148 | 1 | $package = new $packageName(); |
|
149 | |||
150 | 1 | if ($package->hasEntityPath()) { |
|
151 | 1 | $paths = $c['entity_paths']; |
|
152 | 1 | $paths[] = $package->getEntityPath(); |
|
153 | 1 | $c['entity_paths'] = $paths; |
|
154 | } |
||
155 | } |
||
156 | } |
||
157 | 1 | reset($packages); |
|
158 | 1 | foreach ($packages as $packageName) { |
|
159 | 1 | if (class_exists($packageName)) { |
|
160 | /** @var RegistrationInterface $package */ |
||
161 | 1 | $package = new $packageName(); |
|
162 | 1 | $package->addToContainer($c); |
|
163 | |||
164 | 1 | if ($package instanceof RouterConfigInterface) { |
|
165 | 1 | $package->addRoutes($c, $this->router); |
|
166 | } |
||
167 | |||
168 | 1 | if ($package instanceof I18nRegistrationInterface) { |
|
169 | foreach ($i18n['supported_locales'] as $locale) { |
||
170 | $factory = new TranslatorFactory(); |
||
171 | 1 | $factory->addPackageTranslations($translator, $package, $locale); |
|
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | 1 | } |
|
177 | |||
178 | /** |
||
179 | * @param Container $c |
||
180 | */ |
||
181 | 1 | private function setupTranslator(Container $c) |
|
198 | |||
199 | |||
200 | /** |
||
201 | * @param Container $c |
||
202 | */ |
||
203 | 1 | private function setupPdoConnection(Container $c) |
|
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | 1 | function getEntityPath(): string |
|
229 | |||
230 | /** |
||
231 | * @return bool |
||
232 | */ |
||
233 | 1 | function hasEntityPath(): bool |
|
237 | } |
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.