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 |
||
10 | class FilterEvent extends AbstractEvent |
||
11 | { |
||
12 | /** |
||
13 | * @var FilterInterface[] |
||
14 | */ |
||
15 | private $filters = []; |
||
16 | |||
17 | /** |
||
18 | * @var FormInterface[] |
||
19 | */ |
||
20 | private $forms = []; |
||
21 | |||
22 | /** |
||
23 | * @param FormInterface $form |
||
24 | * @param string $identifier |
||
25 | * |
||
26 | * @throws Exception |
||
27 | */ |
||
28 | View Code Duplication | public function addForm(FormInterface $form, string $identifier) |
|
35 | |||
36 | /** |
||
37 | * @return FormInterface[] |
||
38 | */ |
||
39 | public function getForms() |
||
43 | |||
44 | /** |
||
45 | * Add a filter and its value. |
||
46 | * |
||
47 | * @param FilterInterface $filter |
||
48 | */ |
||
49 | public function addFilter(FilterInterface $filter): void |
||
53 | |||
54 | /** |
||
55 | * @return FilterInterface[] |
||
56 | */ |
||
57 | public function getFilters(): array |
||
61 | } |
||
62 |
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.