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 |
||
130 | class Puli |
||
131 | { |
||
132 | /** |
||
133 | * @var string|null |
||
134 | */ |
||
135 | private $rootDir; |
||
136 | |||
137 | /** |
||
138 | * @var string |
||
139 | */ |
||
140 | private $env; |
||
141 | |||
142 | /** |
||
143 | * @var EventDispatcherInterface|null |
||
144 | */ |
||
145 | private $dispatcher; |
||
146 | |||
147 | /** |
||
148 | * @var Context|ProjectContext |
||
149 | */ |
||
150 | private $context; |
||
151 | |||
152 | /** |
||
153 | * @var ResourceRepository |
||
154 | */ |
||
155 | private $repo; |
||
156 | |||
157 | /** |
||
158 | * @var Discovery |
||
159 | */ |
||
160 | private $discovery; |
||
161 | |||
162 | /** |
||
163 | * @var object |
||
164 | */ |
||
165 | private $factory; |
||
166 | |||
167 | /** |
||
168 | * @var FactoryManager |
||
169 | */ |
||
170 | private $factoryManager; |
||
171 | |||
172 | /** |
||
173 | * @var ConfigFileManager |
||
174 | */ |
||
175 | private $configFileManager; |
||
176 | |||
177 | /** |
||
178 | * @var RootModuleFileManager |
||
179 | */ |
||
180 | private $rootModuleFileManager; |
||
181 | |||
182 | /** |
||
183 | * @var ModuleManager |
||
184 | */ |
||
185 | private $moduleManager; |
||
186 | |||
187 | /** |
||
188 | * @var RepositoryManager |
||
189 | */ |
||
190 | private $repositoryManager; |
||
191 | |||
192 | /** |
||
193 | * @var DiscoveryManager |
||
194 | */ |
||
195 | private $discoveryManager; |
||
196 | |||
197 | /** |
||
198 | * @var AssetManager |
||
199 | */ |
||
200 | private $assetManager; |
||
201 | |||
202 | /** |
||
203 | * @var InstallationManager |
||
204 | */ |
||
205 | private $installationManager; |
||
206 | |||
207 | /** |
||
208 | * @var InstallerManager |
||
209 | */ |
||
210 | private $installerManager; |
||
211 | |||
212 | /** |
||
213 | * @var ServerManager |
||
214 | */ |
||
215 | private $serverManager; |
||
216 | |||
217 | /** |
||
218 | * @var UrlGenerator |
||
219 | */ |
||
220 | private $urlGenerator; |
||
221 | |||
222 | /** |
||
223 | * @var Storage|null |
||
224 | */ |
||
225 | private $storage; |
||
226 | |||
227 | /** |
||
228 | * @var JsonStorage|null |
||
229 | */ |
||
230 | private $jsonStorage; |
||
231 | |||
232 | /** |
||
233 | * @var ConfigFileConverter|null |
||
234 | */ |
||
235 | private $configFileConverter; |
||
236 | |||
237 | /** |
||
238 | * @var JsonConverter|null |
||
239 | */ |
||
240 | private $moduleFileConverter; |
||
241 | |||
242 | /** |
||
243 | * @var JsonConverter|null |
||
244 | */ |
||
245 | private $legacyModuleFileConverter; |
||
246 | |||
247 | /** |
||
248 | * @var JsonConverter|null |
||
249 | */ |
||
250 | private $rootModuleFileConverter; |
||
251 | |||
252 | /** |
||
253 | * @var JsonConverter|null |
||
254 | */ |
||
255 | private $legacyRootModuleFileConverter; |
||
256 | |||
257 | /** |
||
258 | * @var MigrationManager|null |
||
259 | */ |
||
260 | private $moduleFileMigrationManager; |
||
261 | |||
262 | /** |
||
263 | * @var JsonEncoder |
||
264 | */ |
||
265 | private $jsonEncoder; |
||
266 | |||
267 | /** |
||
268 | * @var JsonDecoder |
||
269 | */ |
||
270 | private $jsonDecoder; |
||
271 | |||
272 | /** |
||
273 | * @var JsonValidator |
||
274 | */ |
||
275 | private $jsonValidator; |
||
276 | |||
277 | /** |
||
278 | * @var JsonVersioner |
||
279 | */ |
||
280 | private $jsonVersioner; |
||
281 | |||
282 | /** |
||
283 | * @var LoggerInterface |
||
284 | */ |
||
285 | private $logger; |
||
286 | |||
287 | /** |
||
288 | * @var bool |
||
289 | */ |
||
290 | private $started = false; |
||
291 | |||
292 | /** |
||
293 | * @var bool |
||
294 | */ |
||
295 | private $pluginsEnabled = true; |
||
296 | |||
297 | /** |
||
298 | * Parses the system context for a home directory. |
||
299 | * |
||
300 | * @return null|string Returns the path to the home directory or `null` |
||
301 | * if none was found. |
||
302 | */ |
||
303 | 50 | private static function parseHomeDirectory() |
|
318 | |||
319 | /** |
||
320 | * Creates a new instance for the given Puli project. |
||
321 | * |
||
322 | * @param string|null $rootDir The root directory of the Puli project. |
||
323 | * If none is passed, the object operates in |
||
324 | * the global context. You can set or switch |
||
325 | * the root directories later on by calling |
||
326 | * {@link setRootDirectory()}. |
||
327 | * @param string $env One of the {@link Environment} constants. |
||
328 | * |
||
329 | * @see Puli, start() |
||
330 | */ |
||
331 | 52 | public function __construct($rootDir = null, $env = Environment::DEV) |
|
336 | |||
337 | /** |
||
338 | * Starts the service container. |
||
339 | */ |
||
340 | 50 | public function start() |
|
383 | |||
384 | /** |
||
385 | * Returns whether the service container is started. |
||
386 | * |
||
387 | * @return bool Returns `true` if the container is started and `false` |
||
388 | * otherwise. |
||
389 | */ |
||
390 | public function isStarted() |
||
394 | |||
395 | /** |
||
396 | * Sets the root directory of the managed Puli project. |
||
397 | * |
||
398 | * @param string|null $rootDir The root directory of the managed Puli |
||
399 | * project or `null` to start Puli outside of a |
||
400 | * specific project. |
||
401 | */ |
||
402 | 52 | public function setRootDirectory($rootDir) |
|
412 | |||
413 | /** |
||
414 | * Sets the environment of the managed Puli project. |
||
415 | * |
||
416 | * @param string $env One of the {@link Environment} constants. |
||
417 | */ |
||
418 | 52 | public function setEnvironment($env) |
|
428 | |||
429 | /** |
||
430 | * Retturns the environment of the managed Puli project. |
||
431 | * |
||
432 | * @return string One of the {@link Environment} constants. |
||
433 | */ |
||
434 | 2 | public function getEnvironment() |
|
438 | |||
439 | /** |
||
440 | * Returns the root directory of the managed Puli project. |
||
441 | * |
||
442 | * If no Puli project is managed at the moment, `null` is returned. |
||
443 | * |
||
444 | * @return string|null The root directory of the managed Puli project or |
||
445 | * `null` if none is set. |
||
446 | */ |
||
447 | 4 | public function getRootDirectory() |
|
451 | |||
452 | /** |
||
453 | * Sets the logger to use. |
||
454 | * |
||
455 | * @param LoggerInterface $logger The logger to use. |
||
456 | */ |
||
457 | public function setLogger(LoggerInterface $logger) |
||
465 | |||
466 | /** |
||
467 | * Returns the logger. |
||
468 | * |
||
469 | * @return LoggerInterface The logger. |
||
470 | */ |
||
471 | public function getLogger() |
||
475 | |||
476 | /** |
||
477 | * Sets the event dispatcher to use. |
||
478 | * |
||
479 | * @param EventDispatcherInterface $dispatcher The event dispatcher to use. |
||
480 | */ |
||
481 | 2 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
489 | |||
490 | /** |
||
491 | * Returns the used event dispatcher. |
||
492 | * |
||
493 | * @return EventDispatcherInterface|null The used logger. |
||
494 | */ |
||
495 | 3 | public function getEventDispatcher() |
|
499 | |||
500 | /** |
||
501 | * Enables all Puli plugins. |
||
502 | */ |
||
503 | public function enablePlugins() |
||
507 | |||
508 | /** |
||
509 | * Disables all Puli plugins. |
||
510 | */ |
||
511 | 1 | public function disablePlugins() |
|
515 | |||
516 | /** |
||
517 | * Returns whether Puli plugins are enabled. |
||
518 | * |
||
519 | * @return bool Returns `true` if Puli plugins will be loaded and `false` |
||
520 | * otherwise. |
||
521 | */ |
||
522 | public function arePluginsEnabled() |
||
526 | |||
527 | /** |
||
528 | * Returns the context. |
||
529 | * |
||
530 | * @return Context|ProjectContext The context. |
||
531 | */ |
||
532 | 30 | public function getContext() |
|
540 | |||
541 | /** |
||
542 | * Returns the resource repository of the project. |
||
543 | * |
||
544 | * @return EditableRepository The resource repository. |
||
545 | */ |
||
546 | 8 | View Code Duplication | public function getRepository() |
547 | { |
||
548 | 8 | if (!$this->started) { |
|
549 | throw new LogicException('Puli was not started'); |
||
550 | } |
||
551 | |||
552 | 8 | if (!$this->context instanceof ProjectContext) { |
|
553 | 1 | return null; |
|
554 | } |
||
555 | |||
556 | 7 | if (!$this->repo) { |
|
557 | 7 | $this->repo = $this->getFactory()->createRepository(); |
|
558 | } |
||
559 | |||
560 | 7 | return $this->repo; |
|
561 | } |
||
562 | |||
563 | /** |
||
564 | * Returns the resource discovery of the project. |
||
565 | * |
||
566 | * @return EditableDiscovery The resource discovery. |
||
567 | */ |
||
568 | 5 | View Code Duplication | public function getDiscovery() |
569 | { |
||
570 | 5 | if (!$this->started) { |
|
571 | throw new LogicException('Puli was not started'); |
||
572 | } |
||
573 | |||
574 | 5 | if (!$this->context instanceof ProjectContext) { |
|
575 | 1 | return null; |
|
576 | } |
||
577 | |||
578 | 4 | if (!$this->discovery) { |
|
579 | 4 | $this->discovery = $this->getFactory()->createDiscovery($this->getRepository()); |
|
580 | } |
||
581 | |||
582 | 4 | return $this->discovery; |
|
583 | } |
||
584 | |||
585 | /** |
||
586 | * @return object |
||
587 | */ |
||
588 | 9 | public function getFactory() |
|
600 | |||
601 | /** |
||
602 | * @return FactoryManager |
||
603 | */ |
||
604 | 16 | public function getFactoryManager() |
|
605 | { |
||
606 | 16 | if (!$this->started) { |
|
607 | throw new LogicException('Puli was not started'); |
||
608 | } |
||
609 | |||
610 | 16 | if (!$this->factoryManager && $this->context instanceof ProjectContext) { |
|
611 | 14 | $this->factoryManager = new FactoryManagerImpl( |
|
612 | 14 | $this->context, |
|
613 | 14 | new DefaultGeneratorRegistry(), |
|
614 | 14 | new ClassWriter() |
|
615 | ); |
||
616 | |||
617 | // Don't set via the constructor to prevent cyclic dependencies |
||
618 | 14 | $this->factoryManager->setModules($this->getModuleManager()->getModules()); |
|
619 | 14 | $this->factoryManager->setServers($this->getServerManager()->getServers()); |
|
620 | } |
||
621 | |||
622 | 16 | return $this->factoryManager; |
|
623 | } |
||
624 | |||
625 | /** |
||
626 | * Returns the configuration file manager. |
||
627 | * |
||
628 | * @return ConfigFileManager The configuration file manager. |
||
629 | */ |
||
630 | 2 | public function getConfigFileManager() |
|
645 | |||
646 | /** |
||
647 | * Returns the root module file manager. |
||
648 | * |
||
649 | * @return RootModuleFileManager The module file manager. |
||
650 | */ |
||
651 | 15 | View Code Duplication | public function getRootModuleFileManager() |
666 | |||
667 | /** |
||
668 | * Returns the module manager. |
||
669 | * |
||
670 | * @return ModuleManager The module manager. |
||
671 | */ |
||
672 | 15 | View Code Duplication | public function getModuleManager() |
687 | |||
688 | /** |
||
689 | * Returns the resource repository manager. |
||
690 | * |
||
691 | * @return RepositoryManager The repository manager. |
||
692 | */ |
||
693 | 2 | View Code Duplication | public function getRepositoryManager() |
694 | { |
||
695 | 2 | if (!$this->started) { |
|
696 | throw new LogicException('Puli was not started'); |
||
697 | } |
||
698 | |||
699 | 2 | if (!$this->repositoryManager && $this->context instanceof ProjectContext) { |
|
700 | 1 | $this->repositoryManager = new RepositoryManagerImpl( |
|
701 | 1 | $this->context, |
|
702 | 1 | $this->getRepository(), |
|
703 | 1 | $this->getModuleManager()->findModules(Expr::method('isEnabled', Expr::same(true))), |
|
704 | 1 | $this->getJsonStorage() |
|
705 | ); |
||
706 | } |
||
707 | |||
708 | 2 | return $this->repositoryManager; |
|
709 | } |
||
710 | |||
711 | /** |
||
712 | * Returns the resource discovery manager. |
||
713 | * |
||
714 | * @return DiscoveryManager The discovery manager. |
||
715 | */ |
||
716 | 3 | View Code Duplication | public function getDiscoveryManager() |
717 | { |
||
718 | 3 | if (!$this->started) { |
|
719 | throw new LogicException('Puli was not started'); |
||
720 | } |
||
721 | |||
722 | 3 | if (!$this->discoveryManager && $this->context instanceof ProjectContext) { |
|
723 | 2 | $this->discoveryManager = new DiscoveryManagerImpl( |
|
724 | 2 | $this->context, |
|
725 | 2 | $this->getDiscovery(), |
|
726 | 2 | $this->getModuleManager()->findModules(Expr::method('isEnabled', Expr::same(true))), |
|
727 | 2 | $this->getJsonStorage(), |
|
728 | 2 | $this->logger |
|
729 | ); |
||
730 | } |
||
731 | |||
732 | 3 | return $this->discoveryManager; |
|
733 | } |
||
734 | |||
735 | /** |
||
736 | * Returns the asset manager. |
||
737 | * |
||
738 | * @return AssetManager The asset manager. |
||
739 | */ |
||
740 | 2 | public function getAssetManager() |
|
741 | { |
||
742 | 2 | if (!$this->started) { |
|
743 | throw new LogicException('Puli was not started'); |
||
744 | } |
||
745 | |||
746 | 2 | if (!$this->assetManager && $this->context instanceof ProjectContext) { |
|
747 | 1 | $this->assetManager = new DiscoveryAssetManager( |
|
748 | 1 | $this->getDiscoveryManager(), |
|
749 | 1 | $this->getServerManager()->getServers() |
|
750 | ); |
||
751 | } |
||
752 | |||
753 | 2 | return $this->assetManager; |
|
754 | } |
||
755 | |||
756 | /** |
||
757 | * Returns the installation manager. |
||
758 | * |
||
759 | * @return InstallationManager The installation manager. |
||
760 | */ |
||
761 | 2 | public function getInstallationManager() |
|
762 | { |
||
763 | 2 | if (!$this->started) { |
|
764 | throw new LogicException('Puli was not started'); |
||
765 | } |
||
766 | |||
767 | 2 | if (!$this->installationManager && $this->context instanceof ProjectContext) { |
|
768 | 1 | $this->installationManager = new InstallationManagerImpl( |
|
769 | 1 | $this->getContext(), |
|
770 | 1 | $this->getRepository(), |
|
771 | 1 | $this->getServerManager()->getServers(), |
|
772 | 1 | $this->getInstallerManager() |
|
773 | ); |
||
774 | } |
||
775 | |||
776 | 2 | return $this->installationManager; |
|
777 | } |
||
778 | |||
779 | /** |
||
780 | * Returns the installer manager. |
||
781 | * |
||
782 | * @return InstallerManager The installer manager. |
||
783 | */ |
||
784 | 15 | public function getInstallerManager() |
|
785 | { |
||
786 | 15 | if (!$this->started) { |
|
787 | throw new LogicException('Puli was not started'); |
||
788 | } |
||
789 | |||
790 | 15 | if (!$this->installerManager && $this->context instanceof ProjectContext) { |
|
791 | 14 | $this->installerManager = new ModuleFileInstallerManager( |
|
792 | 14 | $this->getRootModuleFileManager(), |
|
793 | 14 | $this->getModuleManager()->getModules() |
|
794 | ); |
||
795 | } |
||
796 | |||
797 | 15 | return $this->installerManager; |
|
798 | } |
||
799 | |||
800 | /** |
||
801 | * Returns the server manager. |
||
802 | * |
||
803 | * @return ServerManager The server manager. |
||
804 | */ |
||
805 | 15 | public function getServerManager() |
|
806 | { |
||
807 | 15 | if (!$this->started) { |
|
808 | throw new LogicException('Puli was not started'); |
||
809 | } |
||
810 | |||
811 | 15 | if (!$this->serverManager && $this->context instanceof ProjectContext) { |
|
812 | 14 | $this->serverManager = new ModuleFileServerManager( |
|
813 | 14 | $this->getRootModuleFileManager(), |
|
814 | 14 | $this->getInstallerManager() |
|
815 | ); |
||
816 | } |
||
817 | |||
818 | 15 | return $this->serverManager; |
|
819 | } |
||
820 | |||
821 | /** |
||
822 | * Returns the resource URL generator. |
||
823 | * |
||
824 | * @return UrlGenerator The resource URL generator. |
||
825 | */ |
||
826 | 2 | public function getUrlGenerator() |
|
827 | { |
||
828 | 2 | if (!$this->started) { |
|
829 | throw new LogicException('Puli was not started'); |
||
830 | } |
||
831 | |||
832 | 2 | if (!$this->urlGenerator && $this->context instanceof ProjectContext) { |
|
833 | 1 | $urlFormats = array(); |
|
834 | 1 | foreach ($this->getServerManager()->getServers() as $server) { |
|
835 | $urlFormats[$server->getName()] = $server->getUrlFormat(); |
||
836 | } |
||
837 | |||
838 | 1 | $this->urlGenerator = new DiscoveryUrlGenerator($this->getDiscovery(), $urlFormats); |
|
839 | } |
||
840 | |||
841 | 2 | return $this->urlGenerator; |
|
842 | } |
||
843 | |||
844 | /** |
||
845 | * Returns the file storage. |
||
846 | * |
||
847 | * @return Storage The storage. |
||
848 | */ |
||
849 | 48 | public function getStorage() |
|
857 | |||
858 | /** |
||
859 | * Returns the configuration file serializer. |
||
860 | * |
||
861 | * @return ConfigFileConverter The configuration file serializer. |
||
862 | */ |
||
863 | 46 | public function getConfigFileConverter() |
|
871 | |||
872 | /** |
||
873 | * Returns the module file converter. |
||
874 | * |
||
875 | * @return JsonConverter The module file converter. |
||
876 | */ |
||
877 | 14 | public function getModuleFileConverter() |
|
887 | |||
888 | /** |
||
889 | * Returns the module file serializer with support for legacy versions. |
||
890 | * |
||
891 | * @return JsonConverter The module file converter. |
||
892 | */ |
||
893 | 14 | View Code Duplication | public function getLegacyModuleFileConverter() |
915 | |||
916 | /** |
||
917 | * Returns the module file converter. |
||
918 | * |
||
919 | * @return JsonConverter The module file converter. |
||
920 | */ |
||
921 | 26 | 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 | 26 | View Code Duplication | public function getLegacyRootModuleFileConverter() |
959 | |||
960 | /** |
||
961 | * Returns the JSON encoder. |
||
962 | * |
||
963 | * @return JsonEncoder The JSON encoder. |
||
964 | */ |
||
965 | 48 | public function getJsonEncoder() |
|
976 | |||
977 | /** |
||
978 | * Returns the JSON decoder. |
||
979 | * |
||
980 | * @return JsonDecoder The JSON decoder. |
||
981 | */ |
||
982 | 48 | public function getJsonDecoder() |
|
990 | |||
991 | /** |
||
992 | * Returns the JSON validator. |
||
993 | * |
||
994 | * @return JsonValidator The JSON validator. |
||
995 | */ |
||
996 | 26 | public function getJsonValidator() |
|
1009 | |||
1010 | 26 | private function activatePlugins() |
|
1020 | |||
1021 | 23 | private function createGlobalContext() |
|
1034 | |||
1035 | /** |
||
1036 | * Creates the context of a Puli project. |
||
1037 | * |
||
1038 | * The home directory is read from the context variable "PULI_HOME". |
||
1039 | * If this variable is not set, the home directory defaults to: |
||
1040 | * |
||
1041 | * * `$HOME/.puli` on Linux, where `$HOME` is the context variable |
||
1042 | * "HOME". |
||
1043 | * * `$APPDATA/Puli` on Windows, where `$APPDATA` is the context |
||
1044 | * variable "APPDATA". |
||
1045 | * |
||
1046 | * If none of these variables can be found, an exception is thrown. |
||
1047 | * |
||
1048 | * A .htaccess file is put into the home directory to protect it from web |
||
1049 | * access. |
||
1050 | * |
||
1051 | * @param string $rootDir The path to the project. |
||
1052 | * |
||
1053 | * @return ProjectContext The project context. |
||
1054 | */ |
||
1055 | 27 | private function createProjectContext($rootDir, $env) |
|
1088 | |||
1089 | /** |
||
1090 | * Decorates a converter with a {@link ValidatingConverter}. |
||
1091 | * |
||
1092 | * @param JsonConverter $innerConverter The converter to decorate. |
||
1093 | * @param string|callable|null $schema The schema. |
||
1094 | * |
||
1095 | * @return ValidatingConverter The decorated converter. |
||
1096 | */ |
||
1097 | 26 | private function createValidatingConverter(JsonConverter $innerConverter, $schema = null) |
|
1101 | |||
1102 | /** |
||
1103 | * Returns the JSON file storage. |
||
1104 | * |
||
1105 | * @return JsonStorage The JSON file storage. |
||
1106 | */ |
||
1107 | 15 | private function getJsonStorage() |
|
1121 | |||
1122 | /** |
||
1123 | * Returns the JSON versioner. |
||
1124 | * |
||
1125 | * @return JsonVersioner The JSON versioner. |
||
1126 | */ |
||
1127 | 26 | private function getJsonVersioner() |
|
1140 | |||
1141 | /** |
||
1142 | * Returns the migration manager for module files. |
||
1143 | * |
||
1144 | * @return MigrationManager The migration manager. |
||
1145 | */ |
||
1146 | 26 | private function getModuleFileMigrationManager() |
|
1156 | |||
1157 | /** |
||
1158 | * Validates the given plugin class name. |
||
1159 | * |
||
1160 | * @param string $pluginClass The fully qualified name of a plugin class. |
||
1161 | */ |
||
1162 | 25 | private function validatePluginClass($pluginClass) |
|
1178 | |||
1179 | 49 | private function loadConfigFile($homeDir, Config $baseConfig) |
|
1206 | } |
||
1207 |
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: