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:
Complex classes like Container 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
134 | class Container |
||
135 | { |
||
136 | /** |
||
137 | * @var string|null |
||
138 | */ |
||
139 | private $rootDir; |
||
140 | |||
141 | /** |
||
142 | * @var string |
||
143 | */ |
||
144 | private $env; |
||
145 | |||
146 | /** |
||
147 | * @var EventDispatcherInterface|null |
||
148 | */ |
||
149 | private $dispatcher; |
||
150 | |||
151 | /** |
||
152 | * @var Context|ProjectContext |
||
153 | */ |
||
154 | private $context; |
||
155 | |||
156 | /** |
||
157 | * @var ResourceRepository |
||
158 | */ |
||
159 | private $repo; |
||
160 | |||
161 | /** |
||
162 | * @var Discovery |
||
163 | */ |
||
164 | private $discovery; |
||
165 | |||
166 | /** |
||
167 | * @var object |
||
168 | */ |
||
169 | private $factory; |
||
170 | |||
171 | /** |
||
172 | * @var FactoryManager |
||
173 | */ |
||
174 | private $factoryManager; |
||
175 | |||
176 | /** |
||
177 | * @var ConfigFileManager |
||
178 | */ |
||
179 | private $configFileManager; |
||
180 | |||
181 | /** |
||
182 | * @var RootModuleFileManager |
||
183 | */ |
||
184 | private $rootModuleFileManager; |
||
185 | |||
186 | /** |
||
187 | * @var ModuleManager |
||
188 | */ |
||
189 | private $moduleManager; |
||
190 | |||
191 | /** |
||
192 | * @var RepositoryManager |
||
193 | */ |
||
194 | private $repositoryManager; |
||
195 | |||
196 | /** |
||
197 | * @var DiscoveryManager |
||
198 | */ |
||
199 | private $discoveryManager; |
||
200 | |||
201 | /** |
||
202 | * @var AssetManager |
||
203 | */ |
||
204 | private $assetManager; |
||
205 | |||
206 | /** |
||
207 | * @var InstallationManager |
||
208 | */ |
||
209 | private $installationManager; |
||
210 | |||
211 | /** |
||
212 | * @var InstallerManager |
||
213 | */ |
||
214 | private $installerManager; |
||
215 | |||
216 | /** |
||
217 | * @var ServerManager |
||
218 | */ |
||
219 | private $serverManager; |
||
220 | |||
221 | /** |
||
222 | * @var UrlGenerator |
||
223 | */ |
||
224 | private $urlGenerator; |
||
225 | |||
226 | /** |
||
227 | * @var Storage|null |
||
228 | */ |
||
229 | private $storage; |
||
230 | |||
231 | /** |
||
232 | * @var JsonStorage|null |
||
233 | */ |
||
234 | private $jsonStorage; |
||
235 | |||
236 | /** |
||
237 | * @var ConfigFileConverter|null |
||
238 | */ |
||
239 | private $configFileConverter; |
||
240 | |||
241 | /** |
||
242 | * @var JsonConverter|null |
||
243 | */ |
||
244 | private $moduleFileConverter; |
||
245 | |||
246 | /** |
||
247 | * @var JsonConverter|null |
||
248 | */ |
||
249 | private $legacyModuleFileConverter; |
||
250 | |||
251 | /** |
||
252 | * @var JsonConverter|null |
||
253 | */ |
||
254 | private $rootModuleFileConverter; |
||
255 | |||
256 | /** |
||
257 | * @var JsonConverter|null |
||
258 | */ |
||
259 | private $legacyRootModuleFileConverter; |
||
260 | |||
261 | /** |
||
262 | * @var MigrationManager|null |
||
263 | */ |
||
264 | private $moduleFileMigrationManager; |
||
265 | |||
266 | /** |
||
267 | * @var JsonEncoder |
||
268 | */ |
||
269 | private $jsonEncoder; |
||
270 | |||
271 | /** |
||
272 | * @var JsonDecoder |
||
273 | */ |
||
274 | private $jsonDecoder; |
||
275 | |||
276 | /** |
||
277 | * @var JsonValidator |
||
278 | */ |
||
279 | private $jsonValidator; |
||
280 | |||
281 | /** |
||
282 | * @var JsonVersioner |
||
283 | */ |
||
284 | private $jsonVersioner; |
||
285 | |||
286 | /** |
||
287 | * @var LoggerInterface |
||
288 | */ |
||
289 | private $logger; |
||
290 | |||
291 | /** |
||
292 | * @var CacheFileConverter|null |
||
293 | */ |
||
294 | private $cacheFileConverter; |
||
295 | |||
296 | /** |
||
297 | * @var CacheFileStorage|null |
||
298 | */ |
||
299 | private $cacheFileStorage; |
||
300 | |||
301 | /** |
||
302 | * @var CacheManager|null |
||
303 | */ |
||
304 | private $cacheManager; |
||
305 | |||
306 | /** |
||
307 | * @var bool |
||
308 | */ |
||
309 | private $started = false; |
||
310 | |||
311 | /** |
||
312 | * @var bool |
||
313 | */ |
||
314 | private $pluginsEnabled = true; |
||
315 | |||
316 | /** |
||
317 | * Parses the system context for a home directory. |
||
318 | * |
||
319 | * @return null|string Returns the path to the home directory or `null` |
||
320 | * if none was found. |
||
321 | */ |
||
322 | 52 | private static function parseHomeDirectory() |
|
337 | |||
338 | /** |
||
339 | * Creates a new instance for the given Puli project. |
||
340 | * |
||
341 | * @param string|null $rootDir The root directory of the Puli project. |
||
342 | * If none is passed, the object operates in |
||
343 | * the global context. You can set or switch |
||
344 | * the root directories later on by calling |
||
345 | * {@link setRootDirectory()}. |
||
346 | * @param string $env One of the {@link Environment} constants. |
||
347 | * |
||
348 | * @see Puli, start() |
||
349 | */ |
||
350 | 54 | public function __construct($rootDir = null, $env = Environment::DEV) |
|
355 | |||
356 | /** |
||
357 | * Starts the service container. |
||
358 | */ |
||
359 | 52 | public function start() |
|
402 | |||
403 | /** |
||
404 | * Returns whether the service container is started. |
||
405 | * |
||
406 | * @return bool Returns `true` if the container is started and `false` |
||
407 | * otherwise. |
||
408 | */ |
||
409 | public function isStarted() |
||
413 | |||
414 | /** |
||
415 | * Sets the root directory of the managed Puli project. |
||
416 | * |
||
417 | * @param string|null $rootDir The root directory of the managed Puli |
||
418 | * project or `null` to start Puli outside of a |
||
419 | * specific project. |
||
420 | */ |
||
421 | 54 | public function setRootDirectory($rootDir) |
|
431 | |||
432 | /** |
||
433 | * Sets the environment of the managed Puli project. |
||
434 | * |
||
435 | * @param string $env One of the {@link Environment} constants. |
||
436 | */ |
||
437 | 54 | public function setEnvironment($env) |
|
447 | |||
448 | /** |
||
449 | * Retturns the environment of the managed Puli project. |
||
450 | * |
||
451 | * @return string One of the {@link Environment} constants. |
||
452 | */ |
||
453 | 2 | public function getEnvironment() |
|
457 | |||
458 | /** |
||
459 | * Returns the root directory of the managed Puli project. |
||
460 | * |
||
461 | * If no Puli project is managed at the moment, `null` is returned. |
||
462 | * |
||
463 | * @return string|null The root directory of the managed Puli project or |
||
464 | * `null` if none is set. |
||
465 | */ |
||
466 | 4 | public function getRootDirectory() |
|
470 | |||
471 | /** |
||
472 | * Sets the logger to use. |
||
473 | * |
||
474 | * @param LoggerInterface $logger The logger to use. |
||
475 | */ |
||
476 | public function setLogger(LoggerInterface $logger) |
||
484 | |||
485 | /** |
||
486 | * Returns the logger. |
||
487 | * |
||
488 | * @return LoggerInterface The logger. |
||
489 | */ |
||
490 | public function getLogger() |
||
494 | |||
495 | /** |
||
496 | * Sets the event dispatcher to use. |
||
497 | * |
||
498 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. |
||
499 | */ |
||
500 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
508 | |||
509 | /** |
||
510 | * Returns the used event dispatcher. |
||
511 | * |
||
512 | * @return EventDispatcherInterface|null The used logger. |
||
513 | */ |
||
514 | 3 | public function getEventDispatcher() |
|
518 | |||
519 | /** |
||
520 | * Enables all Puli plugins. |
||
521 | */ |
||
522 | public function enablePlugins() |
||
526 | |||
527 | /** |
||
528 | * Disables all Puli plugins. |
||
529 | */ |
||
530 | 1 | public function disablePlugins() |
|
534 | |||
535 | /** |
||
536 | * Returns whether Puli plugins are enabled. |
||
537 | * |
||
538 | * @return bool Returns `true` if Puli plugins will be loaded and `false` |
||
539 | * otherwise. |
||
540 | */ |
||
541 | public function arePluginsEnabled() |
||
545 | |||
546 | /** |
||
547 | * Returns the context. |
||
548 | * |
||
549 | * @return Context|ProjectContext The context. |
||
550 | */ |
||
551 | 31 | public function getContext() |
|
559 | |||
560 | /** |
||
561 | * Returns the resource repository of the project. |
||
562 | * |
||
563 | * @return EditableRepository The resource repository. |
||
564 | */ |
||
565 | 8 | View Code Duplication | public function getRepository() |
581 | |||
582 | /** |
||
583 | * Returns the resource discovery of the project. |
||
584 | * |
||
585 | * @return EditableDiscovery The resource discovery. |
||
586 | */ |
||
587 | 5 | View Code Duplication | public function getDiscovery() |
603 | |||
604 | /** |
||
605 | * @return object |
||
606 | */ |
||
607 | 9 | public function getFactory() |
|
619 | |||
620 | /** |
||
621 | * @return FactoryManager |
||
622 | */ |
||
623 | 17 | public function getFactoryManager() |
|
643 | |||
644 | /** |
||
645 | * Returns the configuration file manager. |
||
646 | * |
||
647 | * @return ConfigFileManager The configuration file manager. |
||
648 | */ |
||
649 | 2 | public function getConfigFileManager() |
|
664 | |||
665 | /** |
||
666 | * Returns the root module file manager. |
||
667 | * |
||
668 | * @return RootModuleFileManager The module file manager. |
||
669 | */ |
||
670 | 16 | View Code Duplication | public function getRootModuleFileManager() |
685 | |||
686 | /** |
||
687 | * Returns the module manager. |
||
688 | * |
||
689 | * @return ModuleManager The module manager. |
||
690 | */ |
||
691 | 16 | View Code Duplication | public function getModuleManager() |
706 | |||
707 | /** |
||
708 | * Returns the resource repository manager. |
||
709 | * |
||
710 | * @return RepositoryManager The repository manager. |
||
711 | */ |
||
712 | 2 | View Code Duplication | public function getRepositoryManager() |
729 | |||
730 | /** |
||
731 | * Returns the resource discovery manager. |
||
732 | * |
||
733 | * @return DiscoveryManager The discovery manager. |
||
734 | */ |
||
735 | 3 | View Code Duplication | public function getDiscoveryManager() |
753 | |||
754 | /** |
||
755 | * Returns the asset manager. |
||
756 | * |
||
757 | * @return AssetManager The asset manager. |
||
758 | */ |
||
759 | 2 | public function getAssetManager() |
|
774 | |||
775 | /** |
||
776 | * Returns the installation manager. |
||
777 | * |
||
778 | * @return InstallationManager The installation manager. |
||
779 | */ |
||
780 | 2 | public function getInstallationManager() |
|
797 | |||
798 | /** |
||
799 | * Returns the installer manager. |
||
800 | * |
||
801 | * @return InstallerManager The installer manager. |
||
802 | */ |
||
803 | 16 | public function getInstallerManager() |
|
818 | |||
819 | /** |
||
820 | * Returns the server manager. |
||
821 | * |
||
822 | * @return ServerManager The server manager. |
||
823 | */ |
||
824 | 16 | public function getServerManager() |
|
839 | |||
840 | /** |
||
841 | * Returns the resource URL generator. |
||
842 | * |
||
843 | * @return UrlGenerator The resource URL generator. |
||
844 | */ |
||
845 | 2 | public function getUrlGenerator() |
|
862 | |||
863 | /** |
||
864 | * Returns the file storage. |
||
865 | * |
||
866 | * @return Storage The storage. |
||
867 | */ |
||
868 | 50 | public function getStorage() |
|
876 | |||
877 | /** |
||
878 | * Returns the configuration file serializer. |
||
879 | * |
||
880 | * @return ConfigFileConverter The configuration file serializer. |
||
881 | */ |
||
882 | 48 | public function getConfigFileConverter() |
|
890 | |||
891 | /** |
||
892 | * Returns the module file converter. |
||
893 | * |
||
894 | * @return JsonConverter The module file converter. |
||
895 | */ |
||
896 | 15 | public function getModuleFileConverter() |
|
906 | |||
907 | /** |
||
908 | * Returns the module file serializer with support for legacy versions. |
||
909 | * |
||
910 | * @return JsonConverter The module file converter. |
||
911 | */ |
||
912 | 15 | View Code Duplication | public function getLegacyModuleFileConverter() |
934 | |||
935 | /** |
||
936 | * Returns the module file converter. |
||
937 | * |
||
938 | * @return JsonConverter The module file converter. |
||
939 | */ |
||
940 | 27 | public function getRootModuleFileConverter() |
|
950 | |||
951 | /** |
||
952 | * Returns the module file serializer with support for legacy versions. |
||
953 | * |
||
954 | * @return JsonConverter The module file converter. |
||
955 | */ |
||
956 | 27 | View Code Duplication | public function getLegacyRootModuleFileConverter() |
978 | |||
979 | /** |
||
980 | * Returns the JSON encoder. |
||
981 | * |
||
982 | * @return JsonEncoder The JSON encoder. |
||
983 | */ |
||
984 | 50 | public function getJsonEncoder() |
|
995 | |||
996 | /** |
||
997 | * Returns the JSON decoder. |
||
998 | * |
||
999 | * @return JsonDecoder The JSON decoder. |
||
1000 | */ |
||
1001 | 50 | public function getJsonDecoder() |
|
1009 | |||
1010 | /** |
||
1011 | * Returns the JSON validator. |
||
1012 | * |
||
1013 | * @return JsonValidator The JSON validator. |
||
1014 | */ |
||
1015 | 27 | public function getJsonValidator() |
|
1028 | |||
1029 | /** |
||
1030 | * Returns the cache file converter. |
||
1031 | * |
||
1032 | * @return CacheFileConverter The cache file converter. |
||
1033 | */ |
||
1034 | 1 | public function getCacheFileConverter() |
|
1044 | |||
1045 | /** |
||
1046 | * Returns the cache file storage. |
||
1047 | * |
||
1048 | * @return CacheFileStorage The cache file storage. |
||
1049 | */ |
||
1050 | 1 | public function getCacheFileStorage() |
|
1063 | |||
1064 | /** |
||
1065 | * Returns the cached configuration manager. |
||
1066 | * |
||
1067 | * @return CacheManager The cached configuration manager. |
||
1068 | */ |
||
1069 | 2 | public function getCacheManager() |
|
1081 | |||
1082 | 27 | private function activatePlugins() |
|
1092 | |||
1093 | 24 | private function createGlobalContext() |
|
1106 | |||
1107 | /** |
||
1108 | * Creates the context of a Puli project. |
||
1109 | * |
||
1110 | * The home directory is read from the context variable "PULI_HOME". |
||
1111 | * If this variable is not set, the home directory defaults to: |
||
1112 | * |
||
1113 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
1114 | * "HOME". |
||
1115 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
1116 | * variable "APPDATA". |
||
1117 | * |
||
1118 | * If none of these variables can be found, an exception is thrown. |
||
1119 | * |
||
1120 | * A .htaccess file is put into the home directory to protect it from web |
||
1121 | * access. |
||
1122 | * |
||
1123 | * @param string $rootDir The path to the project. |
||
1124 | * |
||
1125 | * @return ProjectContext The project context. |
||
1126 | */ |
||
1127 | 28 | private function createProjectContext($rootDir, $env) |
|
1160 | |||
1161 | /** |
||
1162 | * Decorates a converter with a {@link ValidatingConverter}. |
||
1163 | * |
||
1164 | * @param JsonConverter $innerConverter The converter to decorate. |
||
1165 | * @param string|callable|null $schema The schema. |
||
1166 | * |
||
1167 | * @return ValidatingConverter The decorated converter. |
||
1168 | */ |
||
1169 | 27 | private function createValidatingConverter(JsonConverter $innerConverter, $schema = null) |
|
1173 | |||
1174 | /** |
||
1175 | * Returns the JSON file storage. |
||
1176 | * |
||
1177 | * @return JsonStorage The JSON file storage. |
||
1178 | */ |
||
1179 | 16 | private function getJsonStorage() |
|
1193 | |||
1194 | /** |
||
1195 | * Returns the JSON versioner. |
||
1196 | * |
||
1197 | * @return JsonVersioner The JSON versioner. |
||
1198 | */ |
||
1199 | 27 | private function getJsonVersioner() |
|
1212 | |||
1213 | /** |
||
1214 | * Returns the migration manager for module files. |
||
1215 | * |
||
1216 | * @return MigrationManager The migration manager. |
||
1217 | */ |
||
1218 | 27 | private function getModuleFileMigrationManager() |
|
1228 | |||
1229 | /** |
||
1230 | * Validates the given plugin class name. |
||
1231 | * |
||
1232 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
1233 | */ |
||
1234 | 26 | private function validatePluginClass($pluginClass) |
|
1250 | |||
1251 | 51 | private function loadConfigFile($homeDir, Config $baseConfig) |
|
1278 | } |
||
1279 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: