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 |
||
11 | View Code Duplication | class CriteriaBuilderCollection implements CriteriaBuilderCollectionInterface |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * @var CriteriaBuilderInterface[] |
||
15 | */ |
||
16 | private $criteriaBuilders; |
||
17 | |||
18 | /** |
||
19 | * @param CriteriaBuilderInterface[] $builders array or \Traversable object |
||
20 | */ |
||
21 | 8 | public function __construct($builders = []) |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 3 | public function addCriteriaBuilder(CriteriaBuilderInterface $criteriaBuilder) |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 3 | public function getCriteriaBuilders() |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 2 | public function getCriteriaBuildersForContext( |
|
63 | |||
64 | /** |
||
65 | * Will ensure that provided criteria are array or traversable object. |
||
66 | * |
||
67 | * @param mixed $criteriaBuilders |
||
68 | */ |
||
69 | 8 | private function checkType($criteriaBuilders) |
|
81 | } |
||
82 |
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.