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() |
|
| 293 | { |
||
| 294 | 50 | if ($this->started) { |
|
| 295 | throw new LogicException('Puli is already started'); |
||
| 296 | } |
||
| 297 | |||
| 298 | 50 | if (null !== $this->rootDir) { |
|
| 299 | 27 | $this->context = $this->createProjectContext($this->rootDir, $this->env); |
|
| 300 | 27 | $bootstrapFile = $this->context->getConfig()->get(Config::BOOTSTRAP_FILE); |
|
| 301 | |||
| 302 | // Run the project's bootstrap file to enable project-specific |
||
| 303 | // autoloading |
||
| 304 | 27 | if (null !== $bootstrapFile) { |
|
| 305 | // Backup autoload functions of the PHAR |
||
| 306 | 1 | $autoloadFunctions = spl_autoload_functions(); |
|
| 307 | |||
| 308 | 1 | foreach ($autoloadFunctions as $autoloadFunction) { |
|
| 309 | 1 | spl_autoload_unregister($autoloadFunction); |
|
| 310 | 1 | } |
|
| 311 | |||
| 312 | // Add project-specific autoload functions |
||
| 313 | 1 | require_once Path::makeAbsolute($bootstrapFile, $this->rootDir); |
|
| 314 | |||
| 315 | // Prepend autoload functions of the PHAR again |
||
| 316 | // This is needed if the user specific autoload functions were |
||
| 317 | // added with $prepend=true (as done by Composer) |
||
| 318 | // Classes in the PHAR should always take precedence |
||
| 319 | 1 | for ($i = count($autoloadFunctions) - 1; $i >= 0; --$i) { |
|
| 320 | 1 | spl_autoload_register($autoloadFunctions[$i], true, true); |
|
| 321 | 1 | } |
|
| 322 | 1 | } |
|
| 323 | 27 | } else { |
|
| 324 | 23 | $this->context = $this->createGlobalContext(); |
|
| 325 | } |
||
| 326 | |||
| 327 | 49 | $this->dispatcher = $this->context->getEventDispatcher(); |
|
| 328 | 49 | $this->started = true; |
|
| 329 | |||
| 330 | // Start plugins once the container is running |
||
| 331 | 49 | if ($this->rootDir && $this->pluginsEnabled) { |
|
|
|
|||
| 332 | 26 | $this->activatePlugins(); |
|
| 333 | 24 | } |
|
| 334 | 47 | } |
|
| 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() |
||
| 343 | { |
||
| 344 | return $this->started; |
||
| 345 | } |
||
| 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) |
|
| 355 | { |
||
| 356 | 52 | if ($this->started) { |
|
| 357 | throw new LogicException('Puli is already started'); |
||
| 358 | } |
||
| 359 | |||
| 360 | 52 | Assert::nullOrDirectory($rootDir); |
|
| 361 | |||
| 362 | 52 | $this->rootDir = $rootDir ? Path::canonicalize($rootDir) : null; |
|
| 363 | 52 | } |
|
| 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() |
|
| 400 | { |
||
| 401 | 4 | return $this->rootDir; |
|
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Sets the logger to use. |
||
| 406 | * |
||
| 407 | * @param LoggerInterface $logger The logger to use. |
||
| 408 | */ |
||
| 409 | public function setLogger(LoggerInterface $logger) |
||
| 410 | { |
||
| 411 | if ($this->started) { |
||
| 412 | throw new LogicException('Puli is already started'); |
||
| 413 | } |
||
| 414 | |||
| 415 | $this->logger = $logger; |
||
| 416 | } |
||
| 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) |
|
| 434 | { |
||
| 435 | 2 | if ($this->started) { |
|
| 436 | throw new LogicException('Puli is already started'); |
||
| 437 | } |
||
| 438 | |||
| 439 | 2 | $this->dispatcher = $dispatcher; |
|
| 440 | 2 | } |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Returns the used event dispatcher. |
||
| 444 | * |
||
| 445 | * @return EventDispatcherInterface|null The used logger. |
||
| 446 | */ |
||
| 447 | 3 | public function getEventDispatcher() |
|
| 448 | { |
||
| 449 | 3 | return $this->dispatcher; |
|
| 450 | } |
||
| 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() |
|
| 541 | { |
||
| 542 | 9 | if (!$this->started) { |
|
| 543 | throw new LogicException('Puli was not started'); |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return FactoryManager |
||
| 555 | */ |
||
| 556 | 16 | public function getFactoryManager() |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Returns the configuration file manager. |
||
| 579 | * |
||
| 580 | * @return ConfigFileManager The configuration file manager. |
||
| 581 | 2 | */ |
|
| 582 | public function getConfigFileManager() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Returns the root package file manager. |
||
| 601 | * |
||
| 602 | * @return RootPackageFileManager The package file manager. |
||
| 603 | 15 | */ |
|
| 604 | View Code Duplication | public function getRootPackageFileManager() |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Returns the package manager. |
||
| 622 | * |
||
| 623 | * @return PackageManager The package manager. |
||
| 624 | 15 | */ |
|
| 625 | View Code Duplication | public function getPackageManager() |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Returns the resource repository manager. |
||
| 643 | * |
||
| 644 | * @return RepositoryManager The repository manager. |
||
| 645 | 2 | */ |
|
| 646 | View Code Duplication | public function getRepositoryManager() |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Returns the resource discovery manager. |
||
| 666 | * |
||
| 667 | * @return DiscoveryManager The discovery manager. |
||
| 668 | 3 | */ |
|
| 669 | View Code Duplication | public function getDiscoveryManager() |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Returns the asset manager. |
||
| 690 | * |
||
| 691 | * @return AssetManager The asset manager. |
||
| 692 | 2 | */ |
|
| 693 | public function getAssetManager() |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Returns the installation manager. |
||
| 711 | * |
||
| 712 | * @return InstallationManager The installation manager. |
||
| 713 | 2 | */ |
|
| 714 | public function getInstallationManager() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Returns the installer manager. |
||
| 734 | * |
||
| 735 | * @return InstallerManager The installer manager. |
||
| 736 | 15 | */ |
|
| 737 | public function getInstallerManager() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Returns the server manager. |
||
| 755 | * |
||
| 756 | * @return ServerManager The server manager. |
||
| 757 | 15 | */ |
|
| 758 | public function getServerManager() |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Returns the resource URL generator. |
||
| 776 | * |
||
| 777 | * @return UrlGenerator The resource URL generator. |
||
| 778 | 2 | */ |
|
| 779 | public function getUrlGenerator() |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Returns the cached file storage. |
||
| 799 | * |
||
| 800 | * @return Storage The storage. |
||
| 801 | 48 | */ |
|
| 802 | public function getStorage() |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Returns the cached configuration file serializer. |
||
| 813 | * |
||
| 814 | * @return ConfigFileSerializer The configuration file serializer. |
||
| 815 | 47 | */ |
|
| 816 | public function getConfigFileSerializer() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Returns the cached package file serializer. |
||
| 827 | * |
||
| 828 | * @return PackageFileSerializer The package file serializer. |
||
| 829 | 27 | */ |
|
| 830 | public function getPackageFileSerializer() |
||
| 843 | 26 | ||
| 844 | private function activatePlugins() |
||
| 854 | 23 | ||
| 855 | private function createGlobalContext() |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Creates the context of a Puli project. |
||
| 871 | * |
||
| 872 | * The home directory is read from the context variable "PULI_HOME". |
||
| 873 | * If this variable is not set, the home directory defaults to: |
||
| 874 | * |
||
| 875 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
| 876 | * "HOME". |
||
| 877 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
| 878 | * variable "APPDATA". |
||
| 879 | * |
||
| 880 | * If none of these variables can be found, an exception is thrown. |
||
| 881 | * |
||
| 882 | * A .htaccess file is put into the home directory to protect it from web |
||
| 883 | * access. |
||
| 884 | * |
||
| 885 | * @param string $rootDir The path to the project. |
||
| 886 | * |
||
| 887 | * @return ProjectContext The project context. |
||
| 888 | 27 | */ |
|
| 889 | private function createProjectContext($rootDir, $env) |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Returns the cached configuration file storage. |
||
| 919 | * |
||
| 920 | * @return ConfigFileStorage The configuration file storage. |
||
| 921 | 2 | */ |
|
| 922 | private function getConfigFileStorage() |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Returns the cached package file storage. |
||
| 937 | * |
||
| 938 | * @return PackageFileStorage The package file storage. |
||
| 939 | 14 | */ |
|
| 940 | private function getPackageFileStorage() |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Validates the given plugin class name. |
||
| 955 | * |
||
| 956 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
| 957 | 25 | */ |
|
| 958 | private function validatePluginClass($pluginClass) |
||
| 974 | 49 | ||
| 975 | private function loadConfigFile($homeDir, Config $baseConfig) |
||
| 996 | } |
||
| 997 |
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: