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 |
||
| 26 | class ConfigurationLoader implements LoaderInterface |
||
| 27 | { |
||
| 28 | /** @var ValidatorInterface */ |
||
| 29 | private $validator; |
||
| 30 | |||
| 31 | /** @var DispersalStrategyInterface */ |
||
| 32 | private $strategy; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | private $options = []; |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * constructor |
||
| 40 | * |
||
| 41 | * @param ValidatorInterface $validator validator |
||
| 42 | * @param LoggerInterface $logger Logger |
||
| 43 | */ |
||
| 44 | public function __construct(ValidatorInterface $validator, LoggerInterface $logger) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @inheritDoc |
||
| 52 | * |
||
| 53 | * @param array $options Configuration options ['prefix'] |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function setOptions($options) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritDoc |
||
| 69 | * |
||
| 70 | * @param DispersalStrategyInterface $strategy Strategy to be used |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function setDispersalStrategy(DispersalStrategyInterface $strategy) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritDoc |
||
| 81 | * |
||
| 82 | * @param CacheProvider $cache Cache layer to be used |
||
| 83 | * @param string $cacheNamespace Name of the cache to be used |
||
| 84 | * @param int $cacheLifetime Cache time to life |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Determines, if the current loader is capable of handling the request. |
||
| 94 | * |
||
| 95 | * @param string $url Current url |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public function supports($url) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritDoc |
||
| 108 | * |
||
| 109 | * @param string $url Current Url |
||
| 110 | * |
||
| 111 | * @return ApiDefinition |
||
| 112 | */ |
||
| 113 | public function load($url) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritDoc |
||
| 127 | * |
||
| 128 | * @param ApiDefinition $apiDef Api definition the Schema to be defined in. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | private function defineSchema(ApiDefinition $apiDef) |
||
| 153 | } |
||
| 154 |
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.