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:
| 1 | <?php |
||
| 18 | class HookCollector implements HookCollectorInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var HookProviderInterface[] |
||
| 22 | * e.g. [<areaName> => <serviceObject>] |
||
| 23 | */ |
||
| 24 | private $providerHooks = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | * e.g. [<moduleName> => [<areaName> => <serviceObject>, <areaName> => <serviceObject>, ...]] |
||
| 29 | */ |
||
| 30 | private $providersByOwner = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var HookSubscriberInterface[] |
||
| 34 | * e.g. [<areaName> => <serviceObject>] |
||
| 35 | */ |
||
| 36 | private $subscriberHooks = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | * e.g. [<moduleName> => [<areaName> => <serviceObject>, <areaName> => <serviceObject>, ...]] |
||
| 41 | */ |
||
| 42 | private $subscribersByOwner = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function addProvider($areaName, $serviceId, HookProviderInterface $service) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function getProvider($areaName) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function hasProvider($areaName) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function getProviders() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function getProviderAreas() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function getProviderAreasByOwner($owner) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | public function addSubscriber($areaName, HookSubscriberInterface $service) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function getSubscriber($areaName) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function hasSubscriber($areaName) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function getSubscribers() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function getSubscriberAreas() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function getSubscriberAreasByOwner($owner) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function isCapable($moduleName, $type = self::HOOK_SUBSCRIBER) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function getOwnersCapableOf($type = self::HOOK_SUBSCRIBER) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Does $moduleName contain at least one SelfAllowedProvider? |
||
| 182 | * @param $moduleName |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | private function containsSelfAllowedProvider($moduleName) |
||
| 197 | } |
||
| 198 |
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.