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 |
||
| 133 | class Container |
||
| 134 | { |
||
| 135 | /** |
||
| 136 | * @var string|null |
||
| 137 | */ |
||
| 138 | private $rootDir; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | private $env; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var EventDispatcherInterface|null |
||
| 147 | */ |
||
| 148 | private $dispatcher; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var Context|ProjectContext |
||
| 152 | */ |
||
| 153 | private $context; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var ResourceRepository |
||
| 157 | */ |
||
| 158 | private $repo; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var Discovery |
||
| 162 | */ |
||
| 163 | private $discovery; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var object |
||
| 167 | */ |
||
| 168 | private $factory; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var FactoryManager |
||
| 172 | */ |
||
| 173 | private $factoryManager; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var ConfigFileManager |
||
| 177 | */ |
||
| 178 | private $configFileManager; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var RootModuleFileManager |
||
| 182 | */ |
||
| 183 | private $rootModuleFileManager; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var ModuleManager |
||
| 187 | */ |
||
| 188 | private $moduleManager; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var RepositoryManager |
||
| 192 | */ |
||
| 193 | private $repositoryManager; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var DiscoveryManager |
||
| 197 | */ |
||
| 198 | private $discoveryManager; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var AssetManager |
||
| 202 | */ |
||
| 203 | private $assetManager; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @var InstallationManager |
||
| 207 | */ |
||
| 208 | private $installationManager; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @var InstallerManager |
||
| 212 | */ |
||
| 213 | private $installerManager; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @var ServerManager |
||
| 217 | */ |
||
| 218 | private $serverManager; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @var UrlGenerator |
||
| 222 | */ |
||
| 223 | private $urlGenerator; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @var Storage|null |
||
| 227 | */ |
||
| 228 | private $storage; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @var JsonStorage|null |
||
| 232 | */ |
||
| 233 | private $jsonStorage; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @var ConfigFileConverter|null |
||
| 237 | */ |
||
| 238 | private $configFileConverter; |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @var JsonConverter|null |
||
| 242 | */ |
||
| 243 | private $moduleFileConverter; |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @var JsonConverter|null |
||
| 247 | */ |
||
| 248 | private $legacyModuleFileConverter; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @var JsonConverter|null |
||
| 252 | */ |
||
| 253 | private $rootModuleFileConverter; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @var JsonConverter|null |
||
| 257 | */ |
||
| 258 | private $legacyRootModuleFileConverter; |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @var MigrationManager|null |
||
| 262 | */ |
||
| 263 | private $moduleFileMigrationManager; |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @var JsonEncoder |
||
| 267 | */ |
||
| 268 | private $jsonEncoder; |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @var JsonDecoder |
||
| 272 | */ |
||
| 273 | private $jsonDecoder; |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @var JsonValidator |
||
| 277 | */ |
||
| 278 | private $jsonValidator; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @var JsonVersioner |
||
| 282 | */ |
||
| 283 | private $jsonVersioner; |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @var LoggerInterface |
||
| 287 | */ |
||
| 288 | private $logger; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @var CacheFileConverter|null |
||
| 292 | */ |
||
| 293 | private $cacheFileConverter; |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @var CacheManager|null |
||
| 297 | */ |
||
| 298 | private $cacheManager; |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @var bool |
||
| 302 | */ |
||
| 303 | private $started = false; |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @var bool |
||
| 307 | */ |
||
| 308 | private $pluginsEnabled = true; |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Parses the system context for a home directory. |
||
| 312 | * |
||
| 313 | * @return null|string Returns the path to the home directory or `null` |
||
| 314 | * if none was found. |
||
| 315 | */ |
||
| 316 | 52 | private static function parseHomeDirectory() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Creates a new instance for the given Puli project. |
||
| 334 | * |
||
| 335 | * @param string|null $rootDir The root directory of the Puli project. |
||
| 336 | * If none is passed, the object operates in |
||
| 337 | * the global context. You can set or switch |
||
| 338 | * the root directories later on by calling |
||
| 339 | * {@link setRootDirectory()}. |
||
| 340 | * @param string $env One of the {@link Environment} constants. |
||
| 341 | * |
||
| 342 | * @see Puli, start() |
||
| 343 | */ |
||
| 344 | 54 | public function __construct($rootDir = null, $env = Environment::DEV) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Starts the service container. |
||
| 352 | */ |
||
| 353 | 52 | public function start() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Returns whether the service container is started. |
||
| 399 | * |
||
| 400 | * @return bool Returns `true` if the container is started and `false` |
||
| 401 | * otherwise. |
||
| 402 | */ |
||
| 403 | public function isStarted() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Sets the root directory of the managed Puli project. |
||
| 410 | * |
||
| 411 | * @param string|null $rootDir The root directory of the managed Puli |
||
| 412 | * project or `null` to start Puli outside of a |
||
| 413 | * specific project. |
||
| 414 | */ |
||
| 415 | 54 | public function setRootDirectory($rootDir) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Sets the environment of the managed Puli project. |
||
| 428 | * |
||
| 429 | * @param string $env One of the {@link Environment} constants. |
||
| 430 | */ |
||
| 431 | 54 | public function setEnvironment($env) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Retturns the environment of the managed Puli project. |
||
| 444 | * |
||
| 445 | * @return string One of the {@link Environment} constants. |
||
| 446 | */ |
||
| 447 | 2 | public function getEnvironment() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Returns the root directory of the managed Puli project. |
||
| 454 | * |
||
| 455 | * If no Puli project is managed at the moment, `null` is returned. |
||
| 456 | * |
||
| 457 | * @return string|null The root directory of the managed Puli project or |
||
| 458 | * `null` if none is set. |
||
| 459 | */ |
||
| 460 | 4 | public function getRootDirectory() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Sets the logger to use. |
||
| 467 | * |
||
| 468 | * @param LoggerInterface $logger The logger to use. |
||
| 469 | */ |
||
| 470 | public function setLogger(LoggerInterface $logger) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns the logger. |
||
| 481 | * |
||
| 482 | * @return LoggerInterface The logger. |
||
| 483 | */ |
||
| 484 | public function getLogger() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Sets the event dispatcher to use. |
||
| 491 | * |
||
| 492 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. |
||
| 493 | */ |
||
| 494 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Returns the used event dispatcher. |
||
| 505 | * |
||
| 506 | * @return EventDispatcherInterface|null The used logger. |
||
| 507 | */ |
||
| 508 | 3 | public function getEventDispatcher() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Enables all Puli plugins. |
||
| 515 | */ |
||
| 516 | public function enablePlugins() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Disables all Puli plugins. |
||
| 523 | */ |
||
| 524 | 1 | public function disablePlugins() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Returns whether Puli plugins are enabled. |
||
| 531 | * |
||
| 532 | * @return bool Returns `true` if Puli plugins will be loaded and `false` |
||
| 533 | * otherwise. |
||
| 534 | */ |
||
| 535 | public function arePluginsEnabled() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns the context. |
||
| 542 | * |
||
| 543 | * @return Context|ProjectContext The context. |
||
| 544 | */ |
||
| 545 | 31 | public function getContext() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Returns the resource repository of the project. |
||
| 556 | * |
||
| 557 | * @return EditableRepository The resource repository. |
||
| 558 | */ |
||
| 559 | 8 | View Code Duplication | public function getRepository() |
| 575 | |||
| 576 | /** |
||
| 577 | * Returns the resource discovery of the project. |
||
| 578 | * |
||
| 579 | * @return EditableDiscovery The resource discovery. |
||
| 580 | */ |
||
| 581 | 5 | View Code Duplication | public function getDiscovery() |
| 597 | |||
| 598 | /** |
||
| 599 | * @return object |
||
| 600 | */ |
||
| 601 | 9 | public function getFactory() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * @return FactoryManager |
||
| 616 | */ |
||
| 617 | 17 | public function getFactoryManager() |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Returns the configuration file manager. |
||
| 640 | * |
||
| 641 | * @return ConfigFileManager The configuration file manager. |
||
| 642 | */ |
||
| 643 | 2 | public function getConfigFileManager() |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Returns the root module file manager. |
||
| 661 | * |
||
| 662 | * @return RootModuleFileManager The module file manager. |
||
| 663 | */ |
||
| 664 | 16 | View Code Duplication | public function getRootModuleFileManager() |
| 679 | |||
| 680 | /** |
||
| 681 | * Returns the module manager. |
||
| 682 | * |
||
| 683 | * @return ModuleManager The module manager. |
||
| 684 | */ |
||
| 685 | 16 | View Code Duplication | public function getModuleManager() |
| 700 | |||
| 701 | /** |
||
| 702 | * Returns the resource repository manager. |
||
| 703 | * |
||
| 704 | * @return RepositoryManager The repository manager. |
||
| 705 | */ |
||
| 706 | 2 | View Code Duplication | public function getRepositoryManager() |
| 723 | |||
| 724 | /** |
||
| 725 | * Returns the resource discovery manager. |
||
| 726 | * |
||
| 727 | * @return DiscoveryManager The discovery manager. |
||
| 728 | */ |
||
| 729 | 3 | View Code Duplication | public function getDiscoveryManager() |
| 747 | |||
| 748 | /** |
||
| 749 | * Returns the asset manager. |
||
| 750 | * |
||
| 751 | * @return AssetManager The asset manager. |
||
| 752 | */ |
||
| 753 | 2 | public function getAssetManager() |
|
| 768 | |||
| 769 | /** |
||
| 770 | * Returns the installation manager. |
||
| 771 | * |
||
| 772 | * @return InstallationManager The installation manager. |
||
| 773 | */ |
||
| 774 | 2 | public function getInstallationManager() |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Returns the installer manager. |
||
| 794 | * |
||
| 795 | * @return InstallerManager The installer manager. |
||
| 796 | */ |
||
| 797 | 16 | public function getInstallerManager() |
|
| 812 | |||
| 813 | /** |
||
| 814 | * Returns the server manager. |
||
| 815 | * |
||
| 816 | * @return ServerManager The server manager. |
||
| 817 | */ |
||
| 818 | 16 | public function getServerManager() |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Returns the resource URL generator. |
||
| 836 | * |
||
| 837 | * @return UrlGenerator The resource URL generator. |
||
| 838 | */ |
||
| 839 | 2 | public function getUrlGenerator() |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Returns the file storage. |
||
| 859 | * |
||
| 860 | * @return Storage The storage. |
||
| 861 | */ |
||
| 862 | 50 | public function getStorage() |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Returns the configuration file serializer. |
||
| 873 | * |
||
| 874 | * @return ConfigFileConverter The configuration file serializer. |
||
| 875 | */ |
||
| 876 | 48 | public function getConfigFileConverter() |
|
| 884 | |||
| 885 | /** |
||
| 886 | * Returns the module file converter. |
||
| 887 | * |
||
| 888 | * @return JsonConverter The module file converter. |
||
| 889 | */ |
||
| 890 | 15 | public function getModuleFileConverter() |
|
| 900 | |||
| 901 | /** |
||
| 902 | * Returns the module file serializer with support for legacy versions. |
||
| 903 | * |
||
| 904 | * @return JsonConverter The module file converter. |
||
| 905 | */ |
||
| 906 | 15 | View Code Duplication | public function getLegacyModuleFileConverter() |
| 928 | |||
| 929 | /** |
||
| 930 | * Returns the module file converter. |
||
| 931 | * |
||
| 932 | * @return JsonConverter The module file converter. |
||
| 933 | */ |
||
| 934 | 27 | public function getRootModuleFileConverter() |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Returns the module file serializer with support for legacy versions. |
||
| 947 | * |
||
| 948 | * @return JsonConverter The module file converter. |
||
| 949 | */ |
||
| 950 | 27 | View Code Duplication | public function getLegacyRootModuleFileConverter() |
| 972 | |||
| 973 | /** |
||
| 974 | * Returns the JSON encoder. |
||
| 975 | * |
||
| 976 | * @return JsonEncoder The JSON encoder. |
||
| 977 | */ |
||
| 978 | 50 | public function getJsonEncoder() |
|
| 989 | |||
| 990 | /** |
||
| 991 | * Returns the JSON decoder. |
||
| 992 | * |
||
| 993 | * @return JsonDecoder The JSON decoder. |
||
| 994 | */ |
||
| 995 | 50 | public function getJsonDecoder() |
|
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Returns the JSON validator. |
||
| 1006 | * |
||
| 1007 | * @return JsonValidator The JSON validator. |
||
| 1008 | */ |
||
| 1009 | 27 | public function getJsonValidator() |
|
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Returns the cache file converter. |
||
| 1025 | * |
||
| 1026 | * @return CacheFileConverter The cache file converter. |
||
| 1027 | */ |
||
| 1028 | public function getCacheFileConverter() |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Returns the cached configuration manager. |
||
| 1041 | * |
||
| 1042 | * @return CacheManager The cached configuration manager. |
||
| 1043 | */ |
||
| 1044 | 2 | public function getCacheManager() |
|
| 1056 | |||
| 1057 | 27 | private function activatePlugins() |
|
| 1067 | |||
| 1068 | 24 | private function createGlobalContext() |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Creates the context of a Puli project. |
||
| 1084 | * |
||
| 1085 | * The home directory is read from the context variable "PULI_HOME". |
||
| 1086 | * If this variable is not set, the home directory defaults to: |
||
| 1087 | * |
||
| 1088 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
| 1089 | * "HOME". |
||
| 1090 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
| 1091 | * variable "APPDATA". |
||
| 1092 | * |
||
| 1093 | * If none of these variables can be found, an exception is thrown. |
||
| 1094 | * |
||
| 1095 | * A .htaccess file is put into the home directory to protect it from web |
||
| 1096 | * access. |
||
| 1097 | * |
||
| 1098 | * @param string $rootDir The path to the project. |
||
| 1099 | * |
||
| 1100 | * @return ProjectContext The project context. |
||
| 1101 | */ |
||
| 1102 | 28 | private function createProjectContext($rootDir, $env) |
|
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Decorates a converter with a {@link ValidatingConverter}. |
||
| 1138 | * |
||
| 1139 | * @param JsonConverter $innerConverter The converter to decorate. |
||
| 1140 | * @param string|callable|null $schema The schema. |
||
| 1141 | * |
||
| 1142 | * @return ValidatingConverter The decorated converter. |
||
| 1143 | */ |
||
| 1144 | 27 | private function createValidatingConverter(JsonConverter $innerConverter, $schema = null) |
|
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Returns the JSON file storage. |
||
| 1151 | * |
||
| 1152 | * @return JsonStorage The JSON file storage. |
||
| 1153 | */ |
||
| 1154 | 16 | private function getJsonStorage() |
|
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Returns the JSON versioner. |
||
| 1171 | * |
||
| 1172 | * @return JsonVersioner The JSON versioner. |
||
| 1173 | */ |
||
| 1174 | 27 | private function getJsonVersioner() |
|
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Returns the migration manager for module files. |
||
| 1190 | * |
||
| 1191 | * @return MigrationManager The migration manager. |
||
| 1192 | */ |
||
| 1193 | 27 | private function getModuleFileMigrationManager() |
|
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Validates the given plugin class name. |
||
| 1206 | * |
||
| 1207 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
| 1208 | */ |
||
| 1209 | 26 | private function validatePluginClass($pluginClass) |
|
| 1225 | |||
| 1226 | 51 | private function loadConfigFile($homeDir, Config $baseConfig) |
|
| 1253 | } |
||
| 1254 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: