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 | ||
| 117 | class Puli | ||
| 118 | { | ||
| 119 | /** | ||
| 120 | * @var string|null | ||
| 121 | */ | ||
| 122 | private $rootDir; | ||
| 123 | |||
| 124 | /** | ||
| 125 | * @var string | ||
| 126 | */ | ||
| 127 | private $env; | ||
| 128 | |||
| 129 | /** | ||
| 130 | * @var EventDispatcherInterface|null | ||
| 131 | */ | ||
| 132 | private $dispatcher; | ||
| 133 | |||
| 134 | /** | ||
| 135 | * @var Context|ProjectContext | ||
| 136 | */ | ||
| 137 | private $context; | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @var ResourceRepository | ||
| 141 | */ | ||
| 142 | private $repo; | ||
| 143 | |||
| 144 | /** | ||
| 145 | * @var Discovery | ||
| 146 | */ | ||
| 147 | private $discovery; | ||
| 148 | |||
| 149 | /** | ||
| 150 | * @var object | ||
| 151 | */ | ||
| 152 | private $factory; | ||
| 153 | |||
| 154 | /** | ||
| 155 | * @var FactoryManager | ||
| 156 | */ | ||
| 157 | private $factoryManager; | ||
| 158 | |||
| 159 | /** | ||
| 160 | * @var ConfigFileManager | ||
| 161 | */ | ||
| 162 | private $configFileManager; | ||
| 163 | |||
| 164 | /** | ||
| 165 | * @var RootPackageFileManager | ||
| 166 | */ | ||
| 167 | private $rootPackageFileManager; | ||
| 168 | |||
| 169 | /** | ||
| 170 | * @var PackageManager | ||
| 171 | */ | ||
| 172 | private $packageManager; | ||
| 173 | |||
| 174 | /** | ||
| 175 | * @var RepositoryManager | ||
| 176 | */ | ||
| 177 | private $repositoryManager; | ||
| 178 | |||
| 179 | /** | ||
| 180 | * @var DiscoveryManager | ||
| 181 | */ | ||
| 182 | private $discoveryManager; | ||
| 183 | |||
| 184 | /** | ||
| 185 | * @var AssetManager | ||
| 186 | */ | ||
| 187 | private $assetManager; | ||
| 188 | |||
| 189 | /** | ||
| 190 | * @var InstallationManager | ||
| 191 | */ | ||
| 192 | private $installationManager; | ||
| 193 | |||
| 194 | /** | ||
| 195 | * @var InstallerManager | ||
| 196 | */ | ||
| 197 | private $installerManager; | ||
| 198 | |||
| 199 | /** | ||
| 200 | * @var ServerManager | ||
| 201 | */ | ||
| 202 | private $serverManager; | ||
| 203 | |||
| 204 | /** | ||
| 205 | * @var UrlGenerator | ||
| 206 | */ | ||
| 207 | private $urlGenerator; | ||
| 208 | |||
| 209 | /** | ||
| 210 | * @var Storage|null | ||
| 211 | */ | ||
| 212 | private $storage; | ||
| 213 | |||
| 214 | /** | ||
| 215 | * @var ConfigFileStorage|null | ||
| 216 | */ | ||
| 217 | private $configFileStorage; | ||
| 218 | |||
| 219 | /** | ||
| 220 | * @var ConfigFileSerializer|null | ||
| 221 | */ | ||
| 222 | private $configFileSerializer; | ||
| 223 | |||
| 224 | /** | ||
| 225 | * @var PackageFileStorage|null | ||
| 226 | */ | ||
| 227 | private $packageFileStorage; | ||
| 228 | |||
| 229 | /** | ||
| 230 | * @var PackageFileSerializer|null | ||
| 231 | */ | ||
| 232 | private $packageFileSerializer; | ||
| 233 | |||
| 234 | /** | ||
| 235 | * @var LoggerInterface | ||
| 236 | */ | ||
| 237 | private $logger; | ||
| 238 | |||
| 239 | /** | ||
| 240 | * @var bool | ||
| 241 | */ | ||
| 242 | private $started = false; | ||
| 243 | |||
| 244 | /** | ||
| 245 | * @var bool | ||
| 246 | */ | ||
| 247 | private $pluginsEnabled = true; | ||
| 248 | |||
| 249 | /** | ||
| 250 | * Parses the system context for a home directory. | ||
| 251 | * | ||
| 252 | * @return null|string Returns the path to the home directory or `null` | ||
| 253 | * if none was found. | ||
| 254 | */ | ||
| 255 | 50 | private static function parseHomeDirectory() | |
| 270 | |||
| 271 | /** | ||
| 272 | * Creates a new instance for the given Puli project. | ||
| 273 | * | ||
| 274 | * @param string|null $rootDir The root directory of the Puli project. | ||
| 275 | * If none is passed, the object operates in | ||
| 276 | * the global context. You can set or switch | ||
| 277 | * the root directories later on by calling | ||
| 278 |      *                             {@link setRootDirectory()}. | ||
| 279 |      * @param string      $env     One of the {@link Environment} constants. | ||
| 280 | * | ||
| 281 | * @see Puli, start() | ||
| 282 | */ | ||
| 283 | 52 | public function __construct($rootDir = null, $env = Environment::DEV) | |
| 288 | |||
| 289 | /** | ||
| 290 | * Starts the service container. | ||
| 291 | */ | ||
| 292 | 50 | public function start() | |
| 335 | |||
| 336 | /** | ||
| 337 | * Returns whether the service container is started. | ||
| 338 | * | ||
| 339 | * @return bool Returns `true` if the container is started and `false` | ||
| 340 | * otherwise. | ||
| 341 | */ | ||
| 342 | public function isStarted() | ||
| 346 | |||
| 347 | /** | ||
| 348 | * Sets the root directory of the managed Puli project. | ||
| 349 | * | ||
| 350 | * @param string|null $rootDir The root directory of the managed Puli | ||
| 351 | * project or `null` to start Puli outside of a | ||
| 352 | * specific project. | ||
| 353 | */ | ||
| 354 | 52 | public function setRootDirectory($rootDir) | |
| 364 | |||
| 365 | /** | ||
| 366 | * Sets the environment of the managed Puli project. | ||
| 367 | * | ||
| 368 |      * @param string $env One of the {@link Environment} constants. | ||
| 369 | */ | ||
| 370 | 52 | public function setEnvironment($env) | |
| 380 | |||
| 381 | /** | ||
| 382 | * Retturns the environment of the managed Puli project. | ||
| 383 | * | ||
| 384 |      * @return string One of the {@link Environment} constants. | ||
| 385 | */ | ||
| 386 | 2 | public function getEnvironment() | |
| 390 | |||
| 391 | /** | ||
| 392 | * Returns the root directory of the managed Puli project. | ||
| 393 | * | ||
| 394 | * If no Puli project is managed at the moment, `null` is returned. | ||
| 395 | * | ||
| 396 | * @return string|null The root directory of the managed Puli project or | ||
| 397 | * `null` if none is set. | ||
| 398 | */ | ||
| 399 | 4 | public function getRootDirectory() | |
| 403 | |||
| 404 | /** | ||
| 405 | * Sets the logger to use. | ||
| 406 | * | ||
| 407 | * @param LoggerInterface $logger The logger to use. | ||
| 408 | */ | ||
| 409 | public function setLogger(LoggerInterface $logger) | ||
| 417 | |||
| 418 | /** | ||
| 419 | * Returns the used logger. | ||
| 420 | * | ||
| 421 | * @return LoggerInterface The used logger. | ||
| 422 | */ | ||
| 423 | public function getLogger() | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Sets the event dispatcher to use. | ||
| 430 | * | ||
| 431 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. | ||
| 432 | */ | ||
| 433 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) | |
| 441 | |||
| 442 | /** | ||
| 443 | * Returns the used event dispatcher. | ||
| 444 | * | ||
| 445 | * @return EventDispatcherInterface|null The used logger. | ||
| 446 | */ | ||
| 447 | 3 | public function getEventDispatcher() | |
| 451 | |||
| 452 | /** | ||
| 453 | * Enables all Puli plugins. | ||
| 454 | */ | ||
| 455 | public function enablePlugins() | ||
| 459 | |||
| 460 | /** | ||
| 461 | * Disables all Puli plugins. | ||
| 462 | */ | ||
| 463 | 1 | public function disablePlugins() | |
| 467 | |||
| 468 | /** | ||
| 469 | * Returns whether Puli plugins are enabled. | ||
| 470 | * | ||
| 471 | * @return bool Returns `true` if Puli plugins will be loaded and `false` | ||
| 472 | * otherwise. | ||
| 473 | */ | ||
| 474 | public function arePluginsEnabled() | ||
| 478 | |||
| 479 | /** | ||
| 480 | * Returns the context. | ||
| 481 | * | ||
| 482 | * @return Context|ProjectContext The context. | ||
| 483 | */ | ||
| 484 | 30 | public function getContext() | |
| 492 | |||
| 493 | /** | ||
| 494 | * Returns the resource repository of the project. | ||
| 495 | * | ||
| 496 | * @return EditableRepository The resource repository. | ||
| 497 | */ | ||
| 498 | 8 | View Code Duplication | public function getRepository() | 
| 514 | |||
| 515 | /** | ||
| 516 | * Returns the resource discovery of the project. | ||
| 517 | * | ||
| 518 | * @return EditableDiscovery The resource discovery. | ||
| 519 | */ | ||
| 520 | 5 | View Code Duplication | public function getDiscovery() | 
| 536 | |||
| 537 | /** | ||
| 538 | * @return object | ||
| 539 | */ | ||
| 540 | 9 | public function getFactory() | |
| 552 | |||
| 553 | /** | ||
| 554 | * @return FactoryManager | ||
| 555 | */ | ||
| 556 | 16 | public function getFactoryManager() | |
| 575 | |||
| 576 | /** | ||
| 577 | * Returns the configuration file manager. | ||
| 578 | * | ||
| 579 | * @return ConfigFileManager The configuration file manager. | ||
| 580 | */ | ||
| 581 | 2 | public function getConfigFileManager() | |
| 597 | |||
| 598 | /** | ||
| 599 | * Returns the root package file manager. | ||
| 600 | * | ||
| 601 | * @return RootPackageFileManager The package file manager. | ||
| 602 | */ | ||
| 603 | 15 | View Code Duplication | public function getRootPackageFileManager() | 
| 618 | |||
| 619 | /** | ||
| 620 | * Returns the package manager. | ||
| 621 | * | ||
| 622 | * @return PackageManager The package manager. | ||
| 623 | */ | ||
| 624 | 15 | View Code Duplication | public function getPackageManager() | 
| 639 | |||
| 640 | /** | ||
| 641 | * Returns the resource repository manager. | ||
| 642 | * | ||
| 643 | * @return RepositoryManager The repository manager. | ||
| 644 | */ | ||
| 645 | 2 | View Code Duplication | public function getRepositoryManager() | 
| 662 | |||
| 663 | /** | ||
| 664 | * Returns the resource discovery manager. | ||
| 665 | * | ||
| 666 | * @return DiscoveryManager The discovery manager. | ||
| 667 | */ | ||
| 668 | 3 | View Code Duplication | public function getDiscoveryManager() | 
| 686 | |||
| 687 | /** | ||
| 688 | * Returns the asset manager. | ||
| 689 | * | ||
| 690 | * @return AssetManager The asset manager. | ||
| 691 | */ | ||
| 692 | 2 | public function getAssetManager() | |
| 707 | |||
| 708 | /** | ||
| 709 | * Returns the installation manager. | ||
| 710 | * | ||
| 711 | * @return InstallationManager The installation manager. | ||
| 712 | */ | ||
| 713 | 2 | public function getInstallationManager() | |
| 730 | |||
| 731 | /** | ||
| 732 | * Returns the installer manager. | ||
| 733 | * | ||
| 734 | * @return InstallerManager The installer manager. | ||
| 735 | */ | ||
| 736 | 15 | public function getInstallerManager() | |
| 751 | |||
| 752 | /** | ||
| 753 | * Returns the server manager. | ||
| 754 | * | ||
| 755 | * @return ServerManager The server manager. | ||
| 756 | */ | ||
| 757 | 15 | public function getServerManager() | |
| 772 | |||
| 773 | /** | ||
| 774 | * Returns the resource URL generator. | ||
| 775 | * | ||
| 776 | * @return UrlGenerator The resource URL generator. | ||
| 777 | */ | ||
| 778 | 2 | public function getUrlGenerator() | |
| 795 | |||
| 796 | /** | ||
| 797 | * Returns the cached file storage. | ||
| 798 | * | ||
| 799 | * @return Storage The storage. | ||
| 800 | */ | ||
| 801 | 48 | public function getStorage() | |
| 809 | |||
| 810 | /** | ||
| 811 | * Returns the cached configuration file serializer. | ||
| 812 | * | ||
| 813 | * @return ConfigFileSerializer The configuration file serializer. | ||
| 814 | */ | ||
| 815 | 47 | public function getConfigFileSerializer() | |
| 823 | |||
| 824 | /** | ||
| 825 | * Returns the cached package file serializer. | ||
| 826 | * | ||
| 827 | * @return PackageFileSerializer The package file serializer. | ||
| 828 | */ | ||
| 829 | 27 | public function getPackageFileSerializer() | |
| 842 | |||
| 843 | 26 | private function activatePlugins() | |
| 853 | |||
| 854 | 23 | private function createGlobalContext() | |
| 867 | |||
| 868 | /** | ||
| 869 | * Creates the context of a Puli project. | ||
| 870 | * | ||
| 871 | * The home directory is read from the context variable "PULI_HOME". | ||
| 872 | * If this variable is not set, the home directory defaults to: | ||
| 873 | * | ||
| 874 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable | ||
| 875 | * "HOME". | ||
| 876 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context | ||
| 877 | * variable "APPDATA". | ||
| 878 | * | ||
| 879 | * If none of these variables can be found, an exception is thrown. | ||
| 880 | * | ||
| 881 | * A .htaccess file is put into the home directory to protect it from web | ||
| 882 | * access. | ||
| 883 | * | ||
| 884 | * @param string $rootDir The path to the project. | ||
| 885 | * | ||
| 886 | * @return ProjectContext The project context. | ||
| 887 | */ | ||
| 888 | 27 | private function createProjectContext($rootDir, $env) | |
| 915 | |||
| 916 | /** | ||
| 917 | * Returns the cached configuration file storage. | ||
| 918 | * | ||
| 919 | * @return ConfigFileStorage The configuration file storage. | ||
| 920 | */ | ||
| 921 | 2 | private function getConfigFileStorage() | |
| 933 | |||
| 934 | /** | ||
| 935 | * Returns the cached package file storage. | ||
| 936 | * | ||
| 937 | * @return PackageFileStorage The package file storage. | ||
| 938 | */ | ||
| 939 | 14 | private function getPackageFileStorage() | |
| 951 | |||
| 952 | /** | ||
| 953 | * Validates the given plugin class name. | ||
| 954 | * | ||
| 955 | * @param string $pluginClass The fully qualified name of a plugin class. | ||
| 956 | */ | ||
| 957 | 25 | private function validatePluginClass($pluginClass) | |
| 973 | |||
| 974 | 49 | private function loadConfigFile($homeDir, Config $baseConfig) | |
| 995 | } | ||
| 996 | 
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: