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 |
||
19 | View Code Duplication | class NoResultsSearchWidget extends AdminWidget |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * @var DateTimeInterface $startDate |
||
23 | */ |
||
24 | private $startDate; |
||
25 | |||
26 | /** |
||
27 | * @var DateTimeInterface $endDate |
||
28 | */ |
||
29 | private $endDate; |
||
30 | |||
31 | private $collectionLoader; |
||
32 | private $noResultsSearches; |
||
33 | |||
34 | public function setDependencies(Container $container) |
||
40 | |||
41 | /** |
||
42 | * @param string|DateTimeInterface|null $date The starting date-time. |
||
43 | * @throws InvalidArgumentException If the date format is not valid. |
||
44 | * @return TopSearchWidget Chainable |
||
45 | */ |
||
46 | public function setStartDate($date) |
||
63 | |||
64 | /** |
||
65 | * @return DateTimeInterface |
||
66 | */ |
||
67 | public function startDate() |
||
74 | |||
75 | /** |
||
76 | * @param string|DateTimeInterface|null $date The ending date-time. |
||
77 | * @throws InvalidArgumentException If the date format is not valid. |
||
78 | * @return TopSearchWidget Chainable |
||
79 | */ |
||
80 | public function setEndDate($date) |
||
97 | |||
98 | |||
99 | /** |
||
100 | * @return DateTimeInterface |
||
101 | */ |
||
102 | public function endDate() |
||
109 | |||
110 | public function noResultsSearches() |
||
118 | |||
119 | public function hasNoResultsSearches() |
||
124 | |||
125 | public function loadNoResultsSearches() |
||
160 | } |
||
161 |
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.