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 |
||
| 20 | class ConfigurationLoader implements LoaderInterface |
||
| 21 | { |
||
| 22 | /** @var ValidatorInterface */ |
||
| 23 | private $validator; |
||
| 24 | |||
| 25 | /** @var DispersalStrategyInterface */ |
||
| 26 | private $strategy; |
||
| 27 | |||
| 28 | /** @var array */ |
||
| 29 | private $options = []; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * constructor |
||
| 34 | * |
||
| 35 | * @param ValidatorInterface $validator validator |
||
| 36 | * @param LoggerInterface $logger Logger |
||
| 37 | */ |
||
| 38 | public function __construct(ValidatorInterface $validator, LoggerInterface $logger) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritDoc |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function setOptions($options) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @inheritDoc |
||
| 59 | */ |
||
| 60 | public function setDispersalStrategy(DispersalStrategyInterface $strategy) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @inheritDoc |
||
| 67 | */ |
||
| 68 | public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Determines, if the current loader is capable of handling the request. |
||
| 74 | * |
||
| 75 | * @param string $url |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function supports($url) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | public function load($url) |
||
| 100 | |||
| 101 | private function defineSchema(ApiDefinition $apiDef) |
||
| 123 | } |
||
| 124 |
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.