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 |
||
| 9 | class LazyModelCollection implements ModelCollectionInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var \Closure; |
||
| 13 | */ |
||
| 14 | private $resolver; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var ModelInterface[]|array |
||
| 18 | */ |
||
| 19 | private $initialModels; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var ModelInterface[]|array |
||
| 23 | */ |
||
| 24 | private $models; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param \Closure $resolver |
||
| 28 | */ |
||
| 29 | public function __construct(\Closure $resolver) |
||
| 33 | |||
| 34 | private function loadModels() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param ModelInterface[]|array $models |
||
| 50 | * |
||
| 51 | * @return ModelInterface[]|array |
||
| 52 | */ |
||
| 53 | View Code Duplication | private function modelsWithIdKey(array $models): array |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @param ModelInterface[]|array $models |
||
| 71 | */ |
||
| 72 | public function set(array $models) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return ModelInterface[]|array |
||
| 81 | */ |
||
| 82 | public function get(): array |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return ModelInterface[]|array |
||
| 91 | */ |
||
| 92 | public function getInitial(): array |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function jsonSerialize(): array |
||
| 113 | } |
||
| 114 |
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.