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 ConfigFileSerializer|null |
||
226 | */ |
||
227 | private $configFileSerializer; |
||
228 | |||
229 | /** |
||
230 | * @var PackageFileStorage|null |
||
231 | */ |
||
232 | private $packageFileStorage; |
||
233 | |||
234 | /** |
||
235 | * @var PackageFileSerializer|null |
||
236 | */ |
||
237 | private $packageFileSerializer; |
||
238 | |||
239 | /** |
||
240 | * @var LoggerInterface |
||
241 | */ |
||
242 | private $logger; |
||
243 | |||
244 | /** |
||
245 | * @var bool |
||
246 | */ |
||
247 | private $started = false; |
||
248 | |||
249 | /** |
||
250 | * @var bool |
||
251 | */ |
||
252 | private $pluginsEnabled = true; |
||
253 | |||
254 | /** |
||
255 | * @var CacheFileSerializer|null |
||
256 | */ |
||
257 | private $cacheFileSerializer; |
||
258 | |||
259 | /** |
||
260 | * @var CacheFileStorage|null |
||
261 | */ |
||
262 | private $cacheFileStorage; |
||
263 | |||
264 | /** |
||
265 | * @var CacheManager|null |
||
266 | */ |
||
267 | private $cacheManager; |
||
268 | |||
269 | /** |
||
270 | * Parses the system context for a home directory. |
||
271 | * |
||
272 | * @return null|string Returns the path to the home directory or `null` |
||
273 | * if none was found. |
||
274 | */ |
||
275 | 50 | private static function parseHomeDirectory() |
|
290 | |||
291 | /** |
||
292 | * Creates a new instance for the given Puli project. |
||
293 | * |
||
294 | * @param string|null $rootDir The root directory of the Puli project. |
||
295 | * If none is passed, the object operates in |
||
296 | * the global context. You can set or switch |
||
297 | * the root directories later on by calling |
||
298 | * {@link setRootDirectory()}. |
||
299 | * @param string $env One of the {@link Environment} constants. |
||
300 | * |
||
301 | * @see Puli, start() |
||
302 | */ |
||
303 | 52 | public function __construct($rootDir = null, $env = Environment::DEV) |
|
308 | |||
309 | /** |
||
310 | * Starts the service container. |
||
311 | */ |
||
312 | 50 | public function start() |
|
355 | |||
356 | /** |
||
357 | * Returns whether the service container is started. |
||
358 | * |
||
359 | * @return bool Returns `true` if the container is started and `false` |
||
360 | * otherwise. |
||
361 | */ |
||
362 | public function isStarted() |
||
366 | |||
367 | /** |
||
368 | * Sets the root directory of the managed Puli project. |
||
369 | * |
||
370 | * @param string|null $rootDir The root directory of the managed Puli |
||
371 | * project or `null` to start Puli outside of a |
||
372 | * specific project. |
||
373 | */ |
||
374 | 52 | public function setRootDirectory($rootDir) |
|
384 | |||
385 | /** |
||
386 | * Sets the environment of the managed Puli project. |
||
387 | * |
||
388 | * @param string $env One of the {@link Environment} constants. |
||
389 | */ |
||
390 | 52 | public function setEnvironment($env) |
|
400 | |||
401 | /** |
||
402 | * Retturns the environment of the managed Puli project. |
||
403 | * |
||
404 | * @return string One of the {@link Environment} constants. |
||
405 | */ |
||
406 | 2 | public function getEnvironment() |
|
410 | |||
411 | /** |
||
412 | * Returns the root directory of the managed Puli project. |
||
413 | * |
||
414 | * If no Puli project is managed at the moment, `null` is returned. |
||
415 | * |
||
416 | * @return string|null The root directory of the managed Puli project or |
||
417 | * `null` if none is set. |
||
418 | */ |
||
419 | 4 | public function getRootDirectory() |
|
423 | |||
424 | /** |
||
425 | * Sets the logger to use. |
||
426 | * |
||
427 | * @param LoggerInterface $logger The logger to use. |
||
428 | */ |
||
429 | public function setLogger(LoggerInterface $logger) |
||
437 | |||
438 | /** |
||
439 | * Returns the used logger. |
||
440 | * |
||
441 | * @return LoggerInterface The used logger. |
||
442 | */ |
||
443 | public function getLogger() |
||
447 | |||
448 | /** |
||
449 | * Sets the event dispatcher to use. |
||
450 | * |
||
451 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. |
||
452 | */ |
||
453 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
461 | |||
462 | /** |
||
463 | * Returns the used event dispatcher. |
||
464 | * |
||
465 | * @return EventDispatcherInterface|null The used logger. |
||
466 | */ |
||
467 | 3 | public function getEventDispatcher() |
|
471 | |||
472 | /** |
||
473 | * Enables all Puli plugins. |
||
474 | */ |
||
475 | public function enablePlugins() |
||
479 | |||
480 | /** |
||
481 | * Disables all Puli plugins. |
||
482 | */ |
||
483 | 1 | public function disablePlugins() |
|
487 | |||
488 | /** |
||
489 | * Returns whether Puli plugins are enabled. |
||
490 | * |
||
491 | * @return bool Returns `true` if Puli plugins will be loaded and `false` |
||
492 | * otherwise. |
||
493 | */ |
||
494 | public function arePluginsEnabled() |
||
498 | |||
499 | /** |
||
500 | * Returns the context. |
||
501 | * |
||
502 | * @return Context|ProjectContext The context. |
||
503 | */ |
||
504 | 30 | public function getContext() |
|
512 | |||
513 | /** |
||
514 | * Returns the resource repository of the project. |
||
515 | * |
||
516 | * @return EditableRepository The resource repository. |
||
517 | */ |
||
518 | 8 | View Code Duplication | public function getRepository() |
534 | |||
535 | /** |
||
536 | * Returns the resource discovery of the project. |
||
537 | * |
||
538 | * @return EditableDiscovery The resource discovery. |
||
539 | */ |
||
540 | 5 | View Code Duplication | public function getDiscovery() |
556 | |||
557 | /** |
||
558 | * @return object |
||
559 | */ |
||
560 | 9 | public function getFactory() |
|
572 | |||
573 | /** |
||
574 | * @return FactoryManager |
||
575 | */ |
||
576 | 16 | public function getFactoryManager() |
|
596 | |||
597 | /** |
||
598 | * Returns the configuration file manager. |
||
599 | * |
||
600 | * @return ConfigFileManager The configuration file manager. |
||
601 | */ |
||
602 | 2 | public function getConfigFileManager() |
|
618 | |||
619 | /** |
||
620 | * Returns the root package file manager. |
||
621 | * |
||
622 | * @return RootPackageFileManager The package file manager. |
||
623 | */ |
||
624 | 15 | View Code Duplication | public function getRootPackageFileManager() |
639 | |||
640 | /** |
||
641 | * Returns the package manager. |
||
642 | * |
||
643 | * @return PackageManager The package manager. |
||
644 | */ |
||
645 | 15 | View Code Duplication | public function getPackageManager() |
660 | |||
661 | /** |
||
662 | * Returns the resource repository manager. |
||
663 | * |
||
664 | * @return RepositoryManager The repository manager. |
||
665 | */ |
||
666 | 2 | View Code Duplication | public function getRepositoryManager() |
683 | |||
684 | /** |
||
685 | * Returns the resource discovery manager. |
||
686 | * |
||
687 | * @return DiscoveryManager The discovery manager. |
||
688 | */ |
||
689 | 3 | View Code Duplication | public function getDiscoveryManager() |
707 | |||
708 | /** |
||
709 | * Returns the asset manager. |
||
710 | * |
||
711 | * @return AssetManager The asset manager. |
||
712 | */ |
||
713 | 2 | public function getAssetManager() |
|
728 | |||
729 | /** |
||
730 | * Returns the installation manager. |
||
731 | * |
||
732 | * @return InstallationManager The installation manager. |
||
733 | */ |
||
734 | 2 | public function getInstallationManager() |
|
751 | |||
752 | /** |
||
753 | * Returns the installer manager. |
||
754 | * |
||
755 | * @return InstallerManager The installer manager. |
||
756 | */ |
||
757 | 15 | public function getInstallerManager() |
|
772 | |||
773 | /** |
||
774 | * Returns the server manager. |
||
775 | * |
||
776 | * @return ServerManager The server manager. |
||
777 | */ |
||
778 | 15 | public function getServerManager() |
|
793 | |||
794 | /** |
||
795 | * Returns the resource URL generator. |
||
796 | * |
||
797 | * @return UrlGenerator The resource URL generator. |
||
798 | */ |
||
799 | 2 | public function getUrlGenerator() |
|
816 | |||
817 | /** |
||
818 | * Returns the cached file storage. |
||
819 | * |
||
820 | * @return Storage The storage. |
||
821 | */ |
||
822 | 48 | public function getStorage() |
|
830 | |||
831 | /** |
||
832 | * Returns the cached configuration file serializer. |
||
833 | * |
||
834 | * @return ConfigFileSerializer The configuration file serializer. |
||
835 | */ |
||
836 | 47 | public function getConfigFileSerializer() |
|
844 | |||
845 | /** |
||
846 | * Returns the cached package file serializer. |
||
847 | * |
||
848 | * @return PackageFileSerializer The package file serializer. |
||
849 | */ |
||
850 | 27 | public function getPackageFileSerializer() |
|
863 | |||
864 | /** |
||
865 | * Returns the cache file serializer. |
||
866 | * |
||
867 | * @return CacheFileSerializer The cache file serializer. |
||
868 | */ |
||
869 | public function getCacheFileSerializer() |
||
877 | |||
878 | /** |
||
879 | * Returns the cache file storage. |
||
880 | * |
||
881 | * @return CacheFileStorage The cache file storage. |
||
882 | */ |
||
883 | public function getCacheFileStorage() |
||
894 | |||
895 | /** |
||
896 | * Returns the cached configuration manager. |
||
897 | * |
||
898 | * @return CacheManager The cached configuration manager. |
||
899 | */ |
||
900 | public function getCacheManager() |
||
912 | |||
913 | 26 | private function activatePlugins() |
|
923 | |||
924 | 23 | private function createGlobalContext() |
|
937 | |||
938 | /** |
||
939 | * Creates the context of a Puli project. |
||
940 | * |
||
941 | * The home directory is read from the context variable "PULI_HOME". |
||
942 | * If this variable is not set, the home directory defaults to: |
||
943 | * |
||
944 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
945 | * "HOME". |
||
946 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
947 | * variable "APPDATA". |
||
948 | * |
||
949 | * If none of these variables can be found, an exception is thrown. |
||
950 | * |
||
951 | * A .htaccess file is put into the home directory to protect it from web |
||
952 | * access. |
||
953 | * |
||
954 | * @param string $rootDir The path to the project. |
||
955 | * |
||
956 | * @return ProjectContext The project context. |
||
957 | */ |
||
958 | 27 | private function createProjectContext($rootDir, $env) |
|
985 | |||
986 | /** |
||
987 | * Returns the cached configuration file storage. |
||
988 | * |
||
989 | * @return ConfigFileStorage The configuration file storage. |
||
990 | */ |
||
991 | 2 | private function getConfigFileStorage() |
|
1003 | |||
1004 | /** |
||
1005 | * Returns the cached package file storage. |
||
1006 | * |
||
1007 | * @return PackageFileStorage The package file storage. |
||
1008 | */ |
||
1009 | 14 | private function getPackageFileStorage() |
|
1021 | |||
1022 | /** |
||
1023 | * Validates the given plugin class name. |
||
1024 | * |
||
1025 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
1026 | */ |
||
1027 | 25 | private function validatePluginClass($pluginClass) |
|
1043 | |||
1044 | 49 | private function loadConfigFile($homeDir, Config $baseConfig) |
|
1065 | } |
||
1066 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: