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 |
||
| 25 | class ConfigurationLoader implements LoaderInterface |
||
| 26 | { |
||
| 27 | /** @var ValidatorInterface */ |
||
| 28 | private $validator; |
||
| 29 | |||
| 30 | /** @var DispersalStrategyInterface */ |
||
| 31 | private $strategy; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $options = []; |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * constructor |
||
| 39 | * |
||
| 40 | * @param ValidatorInterface $validator validator |
||
| 41 | * @param LoggerInterface $logger Logger |
||
| 42 | */ |
||
| 43 | public function __construct(ValidatorInterface $validator, LoggerInterface $logger) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritDoc |
||
| 51 | * |
||
| 52 | * @param array $options |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function setOptions($options) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @inheritDoc |
||
| 66 | * |
||
| 67 | * @param DispersalStrategyInterface $strategy |
||
| 68 | */ |
||
| 69 | public function setDispersalStrategy(DispersalStrategyInterface $strategy) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritDoc |
||
| 76 | * |
||
| 77 | * @param CacheProvider $cache Cache layer to be used |
||
| 78 | * @param string $cacheNamespace Name of the cache to be used |
||
| 79 | * @param int $cacheLifetime Cache time to life |
||
| 80 | */ |
||
| 81 | public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Determines, if the current loader is capable of handling the request. |
||
| 87 | * |
||
| 88 | * @param string $url Current url |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function supports($url) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritDoc |
||
| 101 | * |
||
| 102 | * @param string $url Current Url |
||
| 103 | * |
||
| 104 | * @return ApiDefinition |
||
| 105 | */ |
||
| 106 | public function load($url) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritDoc |
||
| 120 | * |
||
| 121 | * @param ApiDefinition $apiDef Api definition the Schema to be defined in. |
||
| 122 | */ |
||
| 123 | private function defineSchema(ApiDefinition $apiDef) |
||
| 144 | } |
||
| 145 |
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.