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 DiscoveryManagerImpl 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 DiscoveryManagerImpl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class DiscoveryManagerImpl implements DiscoveryManager |
||
67 | { |
||
68 | /** |
||
69 | * @var ProjectContext |
||
70 | */ |
||
71 | private $context; |
||
72 | |||
73 | /** |
||
74 | * @var LoggerInterface |
||
75 | */ |
||
76 | private $logger; |
||
77 | |||
78 | /** |
||
79 | * @var EditableDiscovery |
||
80 | */ |
||
81 | private $discovery; |
||
82 | |||
83 | /** |
||
84 | * @var PackageCollection |
||
85 | */ |
||
86 | private $packages; |
||
87 | |||
88 | /** |
||
89 | * @var PackageFileStorage |
||
90 | */ |
||
91 | private $packageFileStorage; |
||
92 | |||
93 | /** |
||
94 | * @var RootPackage |
||
95 | */ |
||
96 | private $rootPackage; |
||
97 | |||
98 | /** |
||
99 | * @var RootPackageFile |
||
100 | */ |
||
101 | private $rootPackageFile; |
||
102 | |||
103 | /** |
||
104 | * @var BindingTypeDescriptorCollection |
||
105 | */ |
||
106 | private $typeDescriptors; |
||
107 | |||
108 | /** |
||
109 | * @var BindingDescriptorCollection |
||
110 | */ |
||
111 | private $bindingDescriptors; |
||
112 | |||
113 | /** |
||
114 | * Creates a tag manager. |
||
115 | * |
||
116 | * @param ProjectContext $context |
||
117 | * @param EditableDiscovery $discovery |
||
118 | * @param PackageCollection $packages |
||
119 | * @param PackageFileStorage $packageFileStorage |
||
120 | * @param LoggerInterface|null $logger |
||
121 | */ |
||
122 | 99 | public function __construct( |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | public function getContext() |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 5 | public function addRootTypeDescriptor(BindingTypeDescriptor $typeDescriptor, $flags = 0) |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 11 | public function removeRootTypeDescriptor($typeName) |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 4 | public function removeRootTypeDescriptors(Expression $expr) |
|
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | 1 | public function clearRootTypeDescriptors() |
|
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | 2 | public function getRootTypeDescriptor($typeName) |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 5 | View Code Duplication | public function getRootTypeDescriptors() |
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | 1 | View Code Duplication | public function findRootTypeDescriptors(Expression $expr) |
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | 1 | public function hasRootTypeDescriptor($typeName) |
|
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | 1 | View Code Duplication | public function hasRootTypeDescriptors(Expression $expr = null) |
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | 6 | public function getTypeDescriptor($typeName, $packageName) |
|
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | 2 | public function getTypeDescriptors() |
|
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | 2 | View Code Duplication | public function findTypeDescriptors(Expression $expr) |
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | 3 | public function hasTypeDescriptor($typeName, $packageName = null) |
|
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | */ |
||
430 | 3 | View Code Duplication | public function hasTypeDescriptors(Expression $expr = null) |
448 | |||
449 | /** |
||
450 | * {@inheritdoc} |
||
451 | */ |
||
452 | 13 | public function addRootBindingDescriptor(BindingDescriptor $bindingDescriptor, $flags = 0) |
|
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | 6 | public function removeRootBindingDescriptor(Uuid $uuid) |
|
537 | |||
538 | /** |
||
539 | * {@inheritdoc} |
||
540 | */ |
||
541 | 3 | View Code Duplication | public function removeRootBindingDescriptors(Expression $expr) |
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | 1 | public function clearRootBindingDescriptors() |
|
576 | |||
577 | /** |
||
578 | * {@inheritdoc} |
||
579 | */ |
||
580 | public function getRootBindingDescriptor(Uuid $uuid) |
||
590 | |||
591 | /** |
||
592 | * {@inheritdoc} |
||
593 | */ |
||
594 | 3 | View Code Duplication | public function getRootBindingDescriptors() |
608 | |||
609 | /** |
||
610 | * {@inheritdoc} |
||
611 | */ |
||
612 | 1 | View Code Duplication | public function findRootBindingDescriptors(Expression $expr) |
619 | |||
620 | /** |
||
621 | * {@inheritdoc} |
||
622 | */ |
||
623 | 1 | public function hasRootBindingDescriptor(Uuid $uuid) |
|
627 | |||
628 | /** |
||
629 | * {@inheritdoc} |
||
630 | */ |
||
631 | 1 | View Code Duplication | public function hasRootBindingDescriptors(Expression $expr = null) |
641 | |||
642 | /** |
||
643 | * {@inheritdoc} |
||
644 | */ |
||
645 | 8 | View Code Duplication | public function enableBindingDescriptor(Uuid $uuid) |
690 | |||
691 | /** |
||
692 | * {@inheritdoc} |
||
693 | */ |
||
694 | 8 | View Code Duplication | public function disableBindingDescriptor(Uuid $uuid) |
739 | |||
740 | /** |
||
741 | * {@inheritdoc} |
||
742 | */ |
||
743 | 2 | public function removeObsoleteDisabledBindingDescriptors() |
|
772 | |||
773 | /** |
||
774 | * {@inheritdoc} |
||
775 | */ |
||
776 | 3 | public function getBindingDescriptor(Uuid $uuid) |
|
786 | |||
787 | /** |
||
788 | * {@inheritdoc} |
||
789 | */ |
||
790 | 3 | public function getBindingDescriptors() |
|
796 | |||
797 | /** |
||
798 | * {@inheritdoc} |
||
799 | */ |
||
800 | 2 | View Code Duplication | public function findBindingDescriptors(Expression $expr) |
814 | |||
815 | /** |
||
816 | * {@inheritdoc} |
||
817 | */ |
||
818 | 2 | public function hasBindingDescriptor(Uuid $uuid) |
|
824 | |||
825 | /** |
||
826 | * {@inheritdoc} |
||
827 | */ |
||
828 | 3 | public function hasBindingDescriptors(Expression $expr = null) |
|
844 | |||
845 | /** |
||
846 | * {@inheritdoc} |
||
847 | */ |
||
848 | 8 | public function buildDiscovery() |
|
882 | |||
883 | /** |
||
884 | * {@inheritdoc} |
||
885 | */ |
||
886 | 1 | public function clearDiscovery() |
|
890 | |||
891 | 93 | private function assertPackagesLoaded() |
|
897 | |||
898 | 6 | private function assertBindingValid(BindingDescriptor $bindingDescriptor) |
|
908 | |||
909 | 93 | private function loadPackages() |
|
910 | { |
||
911 | 93 | $this->typeDescriptors = new BindingTypeDescriptorCollection(); |
|
912 | 93 | $this->bindingDescriptors = new BindingDescriptorCollection(); |
|
913 | |||
914 | // First load all the types |
||
915 | 93 | View Code Duplication | foreach ($this->packages as $package) { |
916 | 93 | if (null === $package->getPackageFile()) { |
|
917 | 1 | continue; |
|
918 | } |
||
919 | |||
920 | 93 | foreach ($package->getPackageFile()->getTypeDescriptors() as $typeDescriptor) { |
|
921 | 72 | $this->loadTypeDescriptor($typeDescriptor, $package)->execute(); |
|
922 | 93 | } |
|
923 | 93 | } |
|
924 | |||
925 | // Then the bindings for the loaded types |
||
926 | 93 | foreach ($this->packages as $package) { |
|
927 | 93 | if (null === $package->getPackageFile()) { |
|
928 | 1 | continue; |
|
929 | } |
||
930 | |||
931 | 93 | foreach ($package->getPackageFile()->getBindingDescriptors() as $bindingDescriptor) { |
|
932 | // This REALLY shouldn't happen |
||
933 | 48 | if ($this->bindingDescriptors->contains($bindingDescriptor->getUuid())) { |
|
934 | 1 | throw DuplicateBindingException::forUuid($bindingDescriptor->getUuid()); |
|
935 | } |
||
936 | |||
937 | 48 | $this->loadBindingDescriptor($bindingDescriptor, $package)->execute(); |
|
938 | 93 | } |
|
939 | 93 | } |
|
940 | 92 | } |
|
941 | |||
942 | 24 | private function emitWarningForDuplicateTypes() |
|
943 | { |
||
944 | 24 | foreach ($this->typeDescriptors->getTypeNames() as $typeName) { |
|
945 | 15 | $packageNames = $this->typeDescriptors->getPackageNames($typeName); |
|
946 | |||
947 | 15 | if (count($packageNames) > 1) { |
|
948 | 4 | $lastPackageName = array_pop($packageNames); |
|
949 | |||
950 | 4 | $this->logger->warning(sprintf( |
|
951 | 'The packages "%s" and "%s" contain type definitions for '. |
||
952 | 4 | 'the same type "%s". The type has been disabled.', |
|
953 | 4 | implode('", "', $packageNames), |
|
954 | 4 | $lastPackageName, |
|
955 | $typeName |
||
956 | 4 | )); |
|
957 | 4 | } |
|
958 | 24 | } |
|
959 | 24 | } |
|
960 | |||
961 | 8 | private function emitWarningForInvalidBindings() |
|
962 | { |
||
963 | 8 | foreach ($this->bindingDescriptors->toArray() as $binding) { |
|
964 | 5 | foreach ($binding->getLoadErrors() as $exception) { |
|
965 | 2 | $this->logger->warning(sprintf( |
|
966 | 2 | 'The binding "%s" in package "%s" is invalid: %s', |
|
967 | 2 | $binding->getUuid()->toString(), |
|
968 | 2 | $binding->getContainingPackage()->getName(), |
|
969 | 2 | $exception->getMessage() |
|
970 | 2 | )); |
|
971 | 5 | } |
|
972 | 8 | } |
|
973 | 8 | } |
|
974 | |||
975 | 17 | private function getUuidsByTypeName($typeName) |
|
976 | { |
||
977 | 17 | $uuids = array(); |
|
978 | |||
979 | 17 | foreach ($this->bindingDescriptors->getUuids() as $uuid) { |
|
980 | 5 | if ($typeName === $this->bindingDescriptors->get($uuid)->getTypeName()) { |
|
981 | 5 | $uuids[$uuid->toString()] = $uuid; |
|
982 | 5 | } |
|
983 | 17 | } |
|
984 | |||
985 | 17 | return $uuids; |
|
986 | } |
||
987 | |||
988 | 37 | private function saveRootPackageFile() |
|
989 | { |
||
990 | 37 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
991 | 26 | } |
|
992 | |||
993 | 4 | private function addTypeDescriptorToPackageFile(BindingTypeDescriptor $typeDescriptor) |
|
994 | { |
||
995 | 4 | return new AddTypeDescriptorToPackageFile($typeDescriptor, $this->rootPackageFile); |
|
996 | } |
||
997 | |||
998 | 13 | private function removeTypeDescriptorFromPackageFile($typeName) |
|
1002 | |||
1003 | 74 | View Code Duplication | private function loadTypeDescriptor(BindingTypeDescriptor $typeDescriptor, Package $package) |
1004 | { |
||
1005 | 74 | $typeName = $typeDescriptor->getTypeName(); |
|
1006 | |||
1007 | 74 | return new InterceptedOperation( |
|
1008 | 74 | new LoadTypeDescriptor($typeDescriptor, $package, $this->typeDescriptors), |
|
1009 | array( |
||
1010 | 74 | new UpdateDuplicateMarksForTypeName($typeName, $this->typeDescriptors), |
|
1011 | 74 | new ReloadBindingDescriptorsByTypeName($typeName, $this->bindingDescriptors, $this->typeDescriptors), |
|
1012 | ) |
||
1013 | 74 | ); |
|
1014 | } |
||
1015 | |||
1016 | 13 | View Code Duplication | private function unloadTypeDescriptor(BindingTypeDescriptor $typeDescriptor) |
1017 | { |
||
1018 | 13 | $typeName = $typeDescriptor->getTypeName(); |
|
1019 | |||
1020 | 13 | return new InterceptedOperation( |
|
1021 | 13 | new UnloadTypeDescriptor($typeDescriptor, $this->typeDescriptors), |
|
1022 | array( |
||
1023 | 13 | new UpdateDuplicateMarksForTypeName($typeName, $this->typeDescriptors), |
|
1024 | 13 | new ReloadBindingDescriptorsByTypeName($typeName, $this->bindingDescriptors, $this->typeDescriptors), |
|
1025 | ) |
||
1026 | 13 | ); |
|
1027 | } |
||
1028 | |||
1029 | 4 | private function addBindingType(BindingTypeDescriptor $typeDescriptor) |
|
1030 | { |
||
1031 | 4 | return new AddBindingType($typeDescriptor, $this->discovery); |
|
1032 | } |
||
1033 | |||
1034 | 17 | private function syncTypeName($typeName) |
|
1035 | { |
||
1036 | 17 | return new SyncTypeName($typeName, $this->discovery, $this->typeDescriptors); |
|
1037 | } |
||
1038 | |||
1039 | 5 | private function addBindingDescriptorToPackageFile(BindingDescriptor $bindingDescriptor) |
|
1040 | { |
||
1041 | 5 | return new AddBindingDescriptorToPackageFile($bindingDescriptor, $this->rootPackageFile); |
|
1042 | } |
||
1043 | |||
1044 | 7 | private function removeBindingDescriptorFromPackageFile(Uuid $uuid) |
|
1045 | { |
||
1046 | 7 | return new RemoveBindingDescriptorFromPackageFile($uuid, $this->rootPackageFile); |
|
1047 | } |
||
1048 | |||
1049 | 52 | private function loadBindingDescriptor(BindingDescriptor $bindingDescriptor, Package $package) |
|
1050 | { |
||
1051 | 52 | return new LoadBindingDescriptor($bindingDescriptor, $package, $this->bindingDescriptors, $this->typeDescriptors); |
|
1052 | } |
||
1053 | |||
1054 | 7 | private function unloadBindingDescriptor(BindingDescriptor $bindingDescriptor) |
|
1055 | { |
||
1056 | 7 | return new UnloadBindingDescriptor($bindingDescriptor, $this->bindingDescriptors); |
|
1057 | } |
||
1058 | |||
1059 | 3 | View Code Duplication | private function enableBindingUuid(Uuid $uuid, InstallInfo $installInfo) |
1066 | |||
1067 | 3 | View Code Duplication | private function disableBindingUuid(Uuid $uuid, InstallInfo $installInfo) |
1068 | { |
||
1069 | 3 | return new InterceptedOperation( |
|
1070 | 3 | new DisableBindingUuid($uuid, $installInfo), |
|
1071 | 3 | new ReloadBindingDescriptorsByUuid($uuid, $this->bindingDescriptors, $this->typeDescriptors) |
|
1072 | 3 | ); |
|
1073 | } |
||
1074 | |||
1075 | 2 | private function addBinding(BindingDescriptor $bindingDescriptor) |
|
1076 | { |
||
1077 | 2 | return new AddBinding($bindingDescriptor, $this->discovery); |
|
1078 | } |
||
1079 | |||
1080 | 24 | private function syncBindingUuid(Uuid $uuid) |
|
1081 | { |
||
1082 | 24 | return new SyncBindingUuid($uuid, $this->discovery, $this->bindingDescriptors); |
|
1084 | } |
||
1085 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.