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 Puli 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 Puli, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 126 | class Puli |
||
| 127 | { |
||
| 128 | /** |
||
| 129 | * @var string|null |
||
| 130 | */ |
||
| 131 | private $rootDir; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | private $env; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var EventDispatcherInterface|null |
||
| 140 | */ |
||
| 141 | private $dispatcher; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var Context|ProjectContext |
||
| 145 | */ |
||
| 146 | private $context; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var ResourceRepository |
||
| 150 | */ |
||
| 151 | private $repo; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var Discovery |
||
| 155 | */ |
||
| 156 | private $discovery; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var object |
||
| 160 | */ |
||
| 161 | private $factory; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var FactoryManager |
||
| 165 | */ |
||
| 166 | private $factoryManager; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var ConfigFileManager |
||
| 170 | */ |
||
| 171 | private $configFileManager; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var RootModuleFileManager |
||
| 175 | */ |
||
| 176 | private $rootModuleFileManager; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var ModuleManager |
||
| 180 | */ |
||
| 181 | private $moduleManager; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var RepositoryManager |
||
| 185 | */ |
||
| 186 | private $repositoryManager; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var DiscoveryManager |
||
| 190 | */ |
||
| 191 | private $discoveryManager; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var AssetManager |
||
| 195 | */ |
||
| 196 | private $assetManager; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @var InstallationManager |
||
| 200 | */ |
||
| 201 | private $installationManager; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var InstallerManager |
||
| 205 | */ |
||
| 206 | private $installerManager; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var ServerManager |
||
| 210 | */ |
||
| 211 | private $serverManager; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @var UrlGenerator |
||
| 215 | */ |
||
| 216 | private $urlGenerator; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @var Storage|null |
||
| 220 | */ |
||
| 221 | private $storage; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @var ConfigFileStorage|null |
||
| 225 | */ |
||
| 226 | private $configFileStorage; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @var ConfigFileConverter|null |
||
| 230 | */ |
||
| 231 | private $configFileConverter; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @var ModuleFileStorage|null |
||
| 235 | */ |
||
| 236 | private $moduleFileStorage; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @var JsonConverter|null |
||
| 240 | */ |
||
| 241 | private $moduleFileConverter; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @var JsonConverter|null |
||
| 245 | */ |
||
| 246 | private $legacyModuleFileConverter; |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @var JsonConverter|null |
||
| 250 | */ |
||
| 251 | private $rootModuleFileConverter; |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @var JsonConverter|null |
||
| 255 | */ |
||
| 256 | private $legacyRootModuleFileConverter; |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @var JsonEncoder |
||
| 260 | */ |
||
| 261 | private $jsonEncoder; |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @var JsonDecoder |
||
| 265 | */ |
||
| 266 | private $jsonDecoder; |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @var LoggerInterface |
||
| 270 | */ |
||
| 271 | private $logger; |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @var bool |
||
| 275 | */ |
||
| 276 | private $started = false; |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @var bool |
||
| 280 | */ |
||
| 281 | private $pluginsEnabled = true; |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @var CacheFileConverter|null |
||
| 285 | */ |
||
| 286 | private $cacheFileConverter; |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @var CacheFileStorage|null |
||
| 290 | */ |
||
| 291 | private $cacheFileStorage; |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @var CacheManager|null |
||
| 295 | */ |
||
| 296 | private $cacheManager; |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Parses the system context for a home directory. |
||
| 300 | * |
||
| 301 | * @return null|string Returns the path to the home directory or `null` |
||
| 302 | * if none was found. |
||
| 303 | */ |
||
| 304 | 52 | private static function parseHomeDirectory() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Creates a new instance for the given Puli project. |
||
| 322 | * |
||
| 323 | * @param string|null $rootDir The root directory of the Puli project. |
||
| 324 | * If none is passed, the object operates in |
||
| 325 | * the global context. You can set or switch |
||
| 326 | * the root directories later on by calling |
||
| 327 | * {@link setRootDirectory()}. |
||
| 328 | * @param string $env One of the {@link Environment} constants. |
||
| 329 | * |
||
| 330 | * @see Puli, start() |
||
| 331 | */ |
||
| 332 | 54 | public function __construct($rootDir = null, $env = Environment::DEV) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Starts the service container. |
||
| 340 | */ |
||
| 341 | 52 | public function start() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Returns whether the service container is started. |
||
| 387 | * |
||
| 388 | * @return bool Returns `true` if the container is started and `false` |
||
| 389 | * otherwise. |
||
| 390 | */ |
||
| 391 | public function isStarted() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Sets the root directory of the managed Puli project. |
||
| 398 | * |
||
| 399 | * @param string|null $rootDir The root directory of the managed Puli |
||
| 400 | * project or `null` to start Puli outside of a |
||
| 401 | * specific project. |
||
| 402 | */ |
||
| 403 | 54 | public function setRootDirectory($rootDir) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Sets the environment of the managed Puli project. |
||
| 416 | * |
||
| 417 | * @param string $env One of the {@link Environment} constants. |
||
| 418 | */ |
||
| 419 | 54 | public function setEnvironment($env) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Retturns the environment of the managed Puli project. |
||
| 432 | * |
||
| 433 | * @return string One of the {@link Environment} constants. |
||
| 434 | */ |
||
| 435 | 2 | public function getEnvironment() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Returns the root directory of the managed Puli project. |
||
| 442 | * |
||
| 443 | * If no Puli project is managed at the moment, `null` is returned. |
||
| 444 | * |
||
| 445 | * @return string|null The root directory of the managed Puli project or |
||
| 446 | * `null` if none is set. |
||
| 447 | */ |
||
| 448 | 4 | public function getRootDirectory() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Sets the logger to use. |
||
| 455 | * |
||
| 456 | * @param LoggerInterface $logger The logger to use. |
||
| 457 | */ |
||
| 458 | public function setLogger(LoggerInterface $logger) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Returns the logger. |
||
| 469 | * |
||
| 470 | * @return LoggerInterface The logger. |
||
| 471 | */ |
||
| 472 | public function getLogger() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Sets the event dispatcher to use. |
||
| 479 | * |
||
| 480 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. |
||
| 481 | */ |
||
| 482 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Returns the used event dispatcher. |
||
| 493 | * |
||
| 494 | * @return EventDispatcherInterface|null The used logger. |
||
| 495 | */ |
||
| 496 | 3 | public function getEventDispatcher() |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Enables all Puli plugins. |
||
| 503 | */ |
||
| 504 | public function enablePlugins() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Disables all Puli plugins. |
||
| 511 | */ |
||
| 512 | 1 | public function disablePlugins() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Returns whether Puli plugins are enabled. |
||
| 519 | * |
||
| 520 | * @return bool Returns `true` if Puli plugins will be loaded and `false` |
||
| 521 | * otherwise. |
||
| 522 | */ |
||
| 523 | public function arePluginsEnabled() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns the context. |
||
| 530 | * |
||
| 531 | * @return Context|ProjectContext The context. |
||
| 532 | */ |
||
| 533 | 31 | public function getContext() |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the resource repository of the project. |
||
| 544 | * |
||
| 545 | * @return EditableRepository The resource repository. |
||
| 546 | */ |
||
| 547 | 8 | View Code Duplication | public function getRepository() |
| 563 | |||
| 564 | /** |
||
| 565 | * Returns the resource discovery of the project. |
||
| 566 | * |
||
| 567 | * @return EditableDiscovery The resource discovery. |
||
| 568 | */ |
||
| 569 | 5 | View Code Duplication | public function getDiscovery() |
| 585 | |||
| 586 | /** |
||
| 587 | * @return object |
||
| 588 | */ |
||
| 589 | 9 | public function getFactory() |
|
| 601 | |||
| 602 | /** |
||
| 603 | * @return FactoryManager |
||
| 604 | */ |
||
| 605 | 17 | public function getFactoryManager() |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Returns the configuration file manager. |
||
| 628 | * |
||
| 629 | * @return ConfigFileManager The configuration file manager. |
||
| 630 | */ |
||
| 631 | 2 | public function getConfigFileManager() |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Returns the root module file manager. |
||
| 649 | * |
||
| 650 | * @return RootModuleFileManager The module file manager. |
||
| 651 | */ |
||
| 652 | 16 | View Code Duplication | public function getRootModuleFileManager() |
| 667 | |||
| 668 | /** |
||
| 669 | * Returns the module manager. |
||
| 670 | * |
||
| 671 | * @return ModuleManager The module manager. |
||
| 672 | */ |
||
| 673 | 16 | View Code Duplication | public function getModuleManager() |
| 688 | |||
| 689 | /** |
||
| 690 | * Returns the resource repository manager. |
||
| 691 | * |
||
| 692 | * @return RepositoryManager The repository manager. |
||
| 693 | */ |
||
| 694 | 2 | View Code Duplication | public function getRepositoryManager() |
| 711 | |||
| 712 | /** |
||
| 713 | * Returns the resource discovery manager. |
||
| 714 | * |
||
| 715 | * @return DiscoveryManager The discovery manager. |
||
| 716 | */ |
||
| 717 | 3 | View Code Duplication | public function getDiscoveryManager() |
| 735 | |||
| 736 | /** |
||
| 737 | * Returns the asset manager. |
||
| 738 | * |
||
| 739 | * @return AssetManager The asset manager. |
||
| 740 | */ |
||
| 741 | 2 | public function getAssetManager() |
|
| 756 | |||
| 757 | /** |
||
| 758 | * Returns the installation manager. |
||
| 759 | * |
||
| 760 | * @return InstallationManager The installation manager. |
||
| 761 | */ |
||
| 762 | 2 | public function getInstallationManager() |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Returns the installer manager. |
||
| 782 | * |
||
| 783 | * @return InstallerManager The installer manager. |
||
| 784 | */ |
||
| 785 | 16 | public function getInstallerManager() |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Returns the server manager. |
||
| 803 | * |
||
| 804 | * @return ServerManager The server manager. |
||
| 805 | */ |
||
| 806 | 16 | public function getServerManager() |
|
| 821 | |||
| 822 | /** |
||
| 823 | * Returns the resource URL generator. |
||
| 824 | * |
||
| 825 | * @return UrlGenerator The resource URL generator. |
||
| 826 | */ |
||
| 827 | 2 | public function getUrlGenerator() |
|
| 844 | |||
| 845 | /** |
||
| 846 | * Returns the file storage. |
||
| 847 | * |
||
| 848 | * @return Storage The storage. |
||
| 849 | */ |
||
| 850 | 50 | public function getStorage() |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Returns the configuration file serializer. |
||
| 861 | * |
||
| 862 | * @return ConfigFileConverter The configuration file serializer. |
||
| 863 | */ |
||
| 864 | 49 | public function getConfigFileConverter() |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Returns the module file converter. |
||
| 875 | * |
||
| 876 | * @return JsonConverter The module file converter. |
||
| 877 | */ |
||
| 878 | 28 | public function getModuleFileConverter() |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Returns the module file serializer with support for legacy versions. |
||
| 892 | * |
||
| 893 | * @return JsonConverter The module file converter. |
||
| 894 | */ |
||
| 895 | 28 | View Code Duplication | public function getLegacyModuleFileConverter() |
| 914 | |||
| 915 | /** |
||
| 916 | * Returns the module file converter. |
||
| 917 | * |
||
| 918 | * @return JsonConverter The module file converter. |
||
| 919 | */ |
||
| 920 | 28 | public function getRootModuleFileConverter() |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Returns the module file serializer with support for legacy versions. |
||
| 934 | * |
||
| 935 | * @return JsonConverter The module file converter. |
||
| 936 | */ |
||
| 937 | 28 | View Code Duplication | public function getLegacyRootModuleFileConverter() |
| 956 | |||
| 957 | /** |
||
| 958 | * Returns the JSON encoder. |
||
| 959 | * |
||
| 960 | * @return JsonEncoder The JSON encoder. |
||
| 961 | */ |
||
| 962 | 50 | public function getJsonEncoder() |
|
| 973 | |||
| 974 | /** |
||
| 975 | * Returns the JSON decoder. |
||
| 976 | * |
||
| 977 | * @return JsonDecoder The JSON decoder. |
||
| 978 | */ |
||
| 979 | 50 | public function getJsonDecoder() |
|
| 987 | |||
| 988 | /** |
||
| 989 | * Returns the cache file converter. |
||
| 990 | * |
||
| 991 | * @return CacheFileConverter The cache file converter. |
||
| 992 | */ |
||
| 993 | 1 | public function getCacheFileConverter() |
|
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Returns the cache file storage. |
||
| 1004 | * |
||
| 1005 | * @return CacheFileStorage The cache file storage. |
||
| 1006 | */ |
||
| 1007 | 1 | public function getCacheFileStorage() |
|
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Returns the cached configuration manager. |
||
| 1023 | * |
||
| 1024 | * @return CacheManager The cached configuration manager. |
||
| 1025 | */ |
||
| 1026 | 2 | public function getCacheManager() |
|
| 1038 | |||
| 1039 | 27 | private function activatePlugins() |
|
| 1049 | |||
| 1050 | 24 | private function createGlobalContext() |
|
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Creates the context of a Puli project. |
||
| 1066 | * |
||
| 1067 | * The home directory is read from the context variable "PULI_HOME". |
||
| 1068 | * If this variable is not set, the home directory defaults to: |
||
| 1069 | * |
||
| 1070 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
| 1071 | * "HOME". |
||
| 1072 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
| 1073 | * variable "APPDATA". |
||
| 1074 | * |
||
| 1075 | * If none of these variables can be found, an exception is thrown. |
||
| 1076 | * |
||
| 1077 | * A .htaccess file is put into the home directory to protect it from web |
||
| 1078 | * access. |
||
| 1079 | * |
||
| 1080 | * @param string $rootDir The path to the project. |
||
| 1081 | * |
||
| 1082 | * @return ProjectContext The project context. |
||
| 1083 | */ |
||
| 1084 | 28 | private function createProjectContext($rootDir, $env) |
|
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Returns the configuration file storage. |
||
| 1121 | * |
||
| 1122 | * @return ConfigFileStorage The configuration file storage. |
||
| 1123 | */ |
||
| 1124 | 2 | private function getConfigFileStorage() |
|
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Returns the module file storage. |
||
| 1141 | * |
||
| 1142 | * @return ModuleFileStorage The module file storage. |
||
| 1143 | */ |
||
| 1144 | 15 | private function getModuleFileStorage() |
|
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Validates the given plugin class name. |
||
| 1162 | * |
||
| 1163 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
| 1164 | */ |
||
| 1165 | 26 | private function validatePluginClass($pluginClass) |
|
| 1181 | |||
| 1182 | 51 | private function loadConfigFile($homeDir, Config $baseConfig) |
|
| 1209 | } |
||
| 1210 |
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: