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 |
||
| 122 | class Puli |
||
| 123 | { |
||
| 124 | /** |
||
| 125 | * @var string|null |
||
| 126 | */ |
||
| 127 | private $rootDir; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | private $env; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var EventDispatcherInterface|null |
||
| 136 | */ |
||
| 137 | private $dispatcher; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var Context|ProjectContext |
||
| 141 | */ |
||
| 142 | private $context; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var ResourceRepository |
||
| 146 | */ |
||
| 147 | private $repo; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var Discovery |
||
| 151 | */ |
||
| 152 | private $discovery; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var object |
||
| 156 | */ |
||
| 157 | private $factory; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var FactoryManager |
||
| 161 | */ |
||
| 162 | private $factoryManager; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var ConfigFileManager |
||
| 166 | */ |
||
| 167 | private $configFileManager; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var RootPackageFileManager |
||
| 171 | */ |
||
| 172 | private $rootPackageFileManager; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var PackageManager |
||
| 176 | */ |
||
| 177 | private $packageManager; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var RepositoryManager |
||
| 181 | */ |
||
| 182 | private $repositoryManager; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var DiscoveryManager |
||
| 186 | */ |
||
| 187 | private $discoveryManager; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var AssetManager |
||
| 191 | */ |
||
| 192 | private $assetManager; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var InstallationManager |
||
| 196 | */ |
||
| 197 | private $installationManager; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var InstallerManager |
||
| 201 | */ |
||
| 202 | private $installerManager; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var ServerManager |
||
| 206 | */ |
||
| 207 | private $serverManager; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @var UrlGenerator |
||
| 211 | */ |
||
| 212 | private $urlGenerator; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @var Storage|null |
||
| 216 | */ |
||
| 217 | private $storage; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @var ConfigFileStorage|null |
||
| 221 | */ |
||
| 222 | private $configFileStorage; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @var ConfigFileConverter|null |
||
| 226 | */ |
||
| 227 | private $configFileConverter; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @var PackageFileStorage|null |
||
| 231 | */ |
||
| 232 | private $packageFileStorage; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @var JsonConverter|null |
||
| 236 | */ |
||
| 237 | private $packageFileConverter; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @var JsonConverter|null |
||
| 241 | */ |
||
| 242 | private $legacyPackageFileConverter; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @var JsonConverter|null |
||
| 246 | */ |
||
| 247 | private $rootPackageFileConverter; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @var JsonConverter|null |
||
| 251 | */ |
||
| 252 | private $legacyRootPackageFileConverter; |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @var JsonEncoder |
||
| 256 | */ |
||
| 257 | private $jsonEncoder; |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @var JsonDecoder |
||
| 261 | */ |
||
| 262 | private $jsonDecoder; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @var LoggerInterface |
||
| 266 | */ |
||
| 267 | private $logger; |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @var bool |
||
| 271 | */ |
||
| 272 | private $started = false; |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @var bool |
||
| 276 | */ |
||
| 277 | private $pluginsEnabled = true; |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Parses the system context for a home directory. |
||
| 281 | * |
||
| 282 | * @return null|string Returns the path to the home directory or `null` |
||
| 283 | * if none was found. |
||
| 284 | */ |
||
| 285 | 50 | private static function parseHomeDirectory() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Creates a new instance for the given Puli project. |
||
| 303 | * |
||
| 304 | * @param string|null $rootDir The root directory of the Puli project. |
||
| 305 | * If none is passed, the object operates in |
||
| 306 | * the global context. You can set or switch |
||
| 307 | * the root directories later on by calling |
||
| 308 | * {@link setRootDirectory()}. |
||
| 309 | * @param string $env One of the {@link Environment} constants. |
||
| 310 | * |
||
| 311 | * @see Puli, start() |
||
| 312 | */ |
||
| 313 | 52 | public function __construct($rootDir = null, $env = Environment::DEV) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Starts the service container. |
||
| 321 | */ |
||
| 322 | 50 | public function start() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Returns whether the service container is started. |
||
| 368 | * |
||
| 369 | * @return bool Returns `true` if the container is started and `false` |
||
| 370 | * otherwise. |
||
| 371 | */ |
||
| 372 | public function isStarted() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Sets the root directory of the managed Puli project. |
||
| 379 | * |
||
| 380 | * @param string|null $rootDir The root directory of the managed Puli |
||
| 381 | * project or `null` to start Puli outside of a |
||
| 382 | * specific project. |
||
| 383 | */ |
||
| 384 | 52 | public function setRootDirectory($rootDir) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Sets the environment of the managed Puli project. |
||
| 397 | * |
||
| 398 | * @param string $env One of the {@link Environment} constants. |
||
| 399 | */ |
||
| 400 | 52 | public function setEnvironment($env) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Retturns the environment of the managed Puli project. |
||
| 413 | * |
||
| 414 | * @return string One of the {@link Environment} constants. |
||
| 415 | */ |
||
| 416 | 2 | public function getEnvironment() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the root directory of the managed Puli project. |
||
| 423 | * |
||
| 424 | * If no Puli project is managed at the moment, `null` is returned. |
||
| 425 | * |
||
| 426 | * @return string|null The root directory of the managed Puli project or |
||
| 427 | * `null` if none is set. |
||
| 428 | */ |
||
| 429 | 4 | public function getRootDirectory() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Sets the logger to use. |
||
| 436 | * |
||
| 437 | * @param LoggerInterface $logger The logger to use. |
||
| 438 | */ |
||
| 439 | public function setLogger(LoggerInterface $logger) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the logger. |
||
| 450 | * |
||
| 451 | * @return LoggerInterface The logger. |
||
| 452 | */ |
||
| 453 | public function getLogger() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Sets the event dispatcher to use. |
||
| 460 | * |
||
| 461 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. |
||
| 462 | */ |
||
| 463 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Returns the used event dispatcher. |
||
| 474 | * |
||
| 475 | * @return EventDispatcherInterface|null The used logger. |
||
| 476 | */ |
||
| 477 | 3 | public function getEventDispatcher() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Enables all Puli plugins. |
||
| 484 | */ |
||
| 485 | public function enablePlugins() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Disables all Puli plugins. |
||
| 492 | */ |
||
| 493 | 1 | public function disablePlugins() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Returns whether Puli plugins are enabled. |
||
| 500 | * |
||
| 501 | * @return bool Returns `true` if Puli plugins will be loaded and `false` |
||
| 502 | * otherwise. |
||
| 503 | */ |
||
| 504 | public function arePluginsEnabled() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns the context. |
||
| 511 | * |
||
| 512 | * @return Context|ProjectContext The context. |
||
| 513 | */ |
||
| 514 | 30 | public function getContext() |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Returns the resource repository of the project. |
||
| 525 | * |
||
| 526 | * @return EditableRepository The resource repository. |
||
| 527 | */ |
||
| 528 | 8 | View Code Duplication | public function getRepository() |
| 544 | |||
| 545 | /** |
||
| 546 | * Returns the resource discovery of the project. |
||
| 547 | * |
||
| 548 | * @return EditableDiscovery The resource discovery. |
||
| 549 | */ |
||
| 550 | 5 | View Code Duplication | public function getDiscovery() |
| 566 | |||
| 567 | /** |
||
| 568 | * @return object |
||
| 569 | */ |
||
| 570 | 9 | public function getFactory() |
|
| 582 | |||
| 583 | /** |
||
| 584 | * @return FactoryManager |
||
| 585 | */ |
||
| 586 | 16 | public function getFactoryManager() |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Returns the configuration file manager. |
||
| 609 | * |
||
| 610 | * @return ConfigFileManager The configuration file manager. |
||
| 611 | */ |
||
| 612 | 2 | public function getConfigFileManager() |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Returns the root package file manager. |
||
| 630 | * |
||
| 631 | * @return RootPackageFileManager The package file manager. |
||
| 632 | */ |
||
| 633 | 15 | View Code Duplication | public function getRootPackageFileManager() |
| 648 | |||
| 649 | /** |
||
| 650 | * Returns the package manager. |
||
| 651 | * |
||
| 652 | * @return PackageManager The package manager. |
||
| 653 | */ |
||
| 654 | 15 | View Code Duplication | public function getPackageManager() |
| 669 | |||
| 670 | /** |
||
| 671 | * Returns the resource repository manager. |
||
| 672 | * |
||
| 673 | * @return RepositoryManager The repository manager. |
||
| 674 | */ |
||
| 675 | 2 | View Code Duplication | public function getRepositoryManager() |
| 692 | |||
| 693 | /** |
||
| 694 | * Returns the resource discovery manager. |
||
| 695 | * |
||
| 696 | * @return DiscoveryManager The discovery manager. |
||
| 697 | */ |
||
| 698 | 3 | View Code Duplication | public function getDiscoveryManager() |
| 716 | |||
| 717 | /** |
||
| 718 | * Returns the asset manager. |
||
| 719 | * |
||
| 720 | * @return AssetManager The asset manager. |
||
| 721 | */ |
||
| 722 | 2 | public function getAssetManager() |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Returns the installation manager. |
||
| 740 | * |
||
| 741 | * @return InstallationManager The installation manager. |
||
| 742 | */ |
||
| 743 | 2 | public function getInstallationManager() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Returns the installer manager. |
||
| 763 | * |
||
| 764 | * @return InstallerManager The installer manager. |
||
| 765 | */ |
||
| 766 | 15 | public function getInstallerManager() |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Returns the server manager. |
||
| 784 | * |
||
| 785 | * @return ServerManager The server manager. |
||
| 786 | */ |
||
| 787 | 15 | public function getServerManager() |
|
| 802 | |||
| 803 | /** |
||
| 804 | * Returns the resource URL generator. |
||
| 805 | * |
||
| 806 | * @return UrlGenerator The resource URL generator. |
||
| 807 | */ |
||
| 808 | 2 | public function getUrlGenerator() |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Returns the file storage. |
||
| 828 | * |
||
| 829 | * @return Storage The storage. |
||
| 830 | */ |
||
| 831 | 48 | public function getStorage() |
|
| 839 | |||
| 840 | /** |
||
| 841 | * Returns the configuration file serializer. |
||
| 842 | * |
||
| 843 | * @return ConfigFileConverter The configuration file serializer. |
||
| 844 | */ |
||
| 845 | 47 | public function getConfigFileConverter() |
|
| 853 | |||
| 854 | /** |
||
| 855 | * Returns the package file converter. |
||
| 856 | * |
||
| 857 | * @return JsonConverter The package file converter. |
||
| 858 | */ |
||
| 859 | 27 | public function getPackageFileConverter() |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Returns the package file serializer with support for legacy versions. |
||
| 873 | * |
||
| 874 | * @return JsonConverter The package file converter. |
||
| 875 | */ |
||
| 876 | 27 | View Code Duplication | public function getLegacyPackageFileConverter() |
| 895 | |||
| 896 | /** |
||
| 897 | * Returns the package file converter. |
||
| 898 | * |
||
| 899 | * @return JsonConverter The package file converter. |
||
| 900 | */ |
||
| 901 | 27 | public function getRootPackageFileConverter() |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Returns the package file serializer with support for legacy versions. |
||
| 915 | * |
||
| 916 | * @return JsonConverter The package file converter. |
||
| 917 | */ |
||
| 918 | 27 | View Code Duplication | public function getLegacyRootPackageFileConverter() |
| 937 | |||
| 938 | /** |
||
| 939 | * Returns the JSON encoder. |
||
| 940 | * |
||
| 941 | * @return JsonEncoder The JSON encoder. |
||
| 942 | */ |
||
| 943 | 48 | public function getJsonEncoder() |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Returns the JSON decoder. |
||
| 957 | * |
||
| 958 | * @return JsonDecoder The JSON decoder. |
||
| 959 | */ |
||
| 960 | 48 | public function getJsonDecoder() |
|
| 968 | |||
| 969 | 26 | private function activatePlugins() |
|
| 979 | |||
| 980 | 23 | private function createGlobalContext() |
|
| 993 | |||
| 994 | /** |
||
| 995 | * Creates the context of a Puli project. |
||
| 996 | * |
||
| 997 | * The home directory is read from the context variable "PULI_HOME". |
||
| 998 | * If this variable is not set, the home directory defaults to: |
||
| 999 | * |
||
| 1000 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
| 1001 | * "HOME". |
||
| 1002 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
| 1003 | * variable "APPDATA". |
||
| 1004 | * |
||
| 1005 | * If none of these variables can be found, an exception is thrown. |
||
| 1006 | * |
||
| 1007 | * A .htaccess file is put into the home directory to protect it from web |
||
| 1008 | * access. |
||
| 1009 | * |
||
| 1010 | * @param string $rootDir The path to the project. |
||
| 1011 | * |
||
| 1012 | * @return ProjectContext The project context. |
||
| 1013 | */ |
||
| 1014 | 27 | private function createProjectContext($rootDir, $env) |
|
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Returns the configuration file storage. |
||
| 1051 | * |
||
| 1052 | * @return ConfigFileStorage The configuration file storage. |
||
| 1053 | */ |
||
| 1054 | 2 | private function getConfigFileStorage() |
|
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Returns the package file storage. |
||
| 1071 | * |
||
| 1072 | * @return PackageFileStorage The package file storage. |
||
| 1073 | */ |
||
| 1074 | 14 | private function getPackageFileStorage() |
|
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Validates the given plugin class name. |
||
| 1092 | * |
||
| 1093 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
| 1094 | */ |
||
| 1095 | 25 | private function validatePluginClass($pluginClass) |
|
| 1111 | |||
| 1112 | 49 | private function loadConfigFile($homeDir, Config $baseConfig) |
|
| 1139 | } |
||
| 1140 |
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: