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 |
||
18 | abstract class AbstractSearchHistoryWidget extends AdminWidget |
||
19 | { |
||
20 | /** |
||
21 | * The default lower bound to filter searches by. |
||
22 | * |
||
23 | * @const string |
||
24 | */ |
||
25 | const DEFAULT_FROM_DATE = '30 days ago'; |
||
26 | |||
27 | /** |
||
28 | * The default upper bound to filter searches by. |
||
29 | * |
||
30 | * @const string |
||
31 | */ |
||
32 | const DEFAULT_UNTIL_DATE = 'now'; |
||
33 | |||
34 | /** |
||
35 | * The lower bound (exclusive) for a search's timestamp to filter by. |
||
36 | * |
||
37 | * The default is to filter by 30 days ago (@see self::DEFAULT_FROM_DATE). |
||
38 | * |
||
39 | * @var DateTimeInterface |
||
40 | */ |
||
41 | private $startDate; |
||
42 | |||
43 | /** |
||
44 | * The upper bound (inclusive) for a search's timestamp to filter by. |
||
45 | * |
||
46 | * The default is to filter by the current time (@see self::DEFAULT_UNTIL_DATE). |
||
47 | * |
||
48 | * @var DateTimeInterface |
||
49 | */ |
||
50 | private $endDate; |
||
51 | |||
52 | /** |
||
53 | * The latest search requests. |
||
54 | * |
||
55 | * @var SearchLog[] |
||
56 | */ |
||
57 | private $searchHistory; |
||
58 | |||
59 | /** |
||
60 | * Store the collection loader for the current class. |
||
61 | * |
||
62 | * @var CollectionLoader |
||
63 | */ |
||
64 | private $collectionLoader; |
||
65 | |||
66 | /** |
||
67 | * Inject dependencies from a DI Container. |
||
68 | * |
||
69 | * @param Container $container A dependencies container instance. |
||
70 | * @return void |
||
71 | */ |
||
72 | public function setDependencies(Container $container) |
||
78 | |||
79 | /** |
||
80 | * Set the lower bound (exclusive) for a search's timestamp to filter by. |
||
81 | * |
||
82 | * @param string|DateTimeInterface|null $date The starting date/time. |
||
83 | * @throws InvalidArgumentException If the date/time is invalid. |
||
84 | * @return TopSearchWidget Chainable |
||
85 | */ |
||
86 | View Code Duplication | public function setStartDate($date) |
|
107 | |||
108 | /** |
||
109 | * Retrieve the lower bound (exclusive) for a search's timestamp to filter by. |
||
110 | * |
||
111 | * @return DateTimeInterface |
||
112 | */ |
||
113 | public function startDate() |
||
121 | |||
122 | /** |
||
123 | * Set the upper bound (inclusive) for a search's timestamp to filter by. |
||
124 | * |
||
125 | * @param string|DateTimeInterface|null $date The ending date/time. |
||
126 | * @throws InvalidArgumentException If the date/time is invalid. |
||
127 | * @return TopSearchWidget Chainable |
||
128 | */ |
||
129 | View Code Duplication | public function setEndDate($date) |
|
150 | |||
151 | /** |
||
152 | * Retrieve the upper bound (inclusive) for a search's timestamp to filter by. |
||
153 | * |
||
154 | * @return DateTimeInterface |
||
155 | */ |
||
156 | public function endDate() |
||
164 | |||
165 | /** |
||
166 | * Retrieve the search history. |
||
167 | * |
||
168 | * @return SearchLog[] |
||
169 | */ |
||
170 | public function searchHistory() |
||
178 | |||
179 | /** |
||
180 | * Determine if there's a search history. |
||
181 | * |
||
182 | * @return boolean |
||
183 | */ |
||
184 | public function hasSearchHistory() |
||
188 | |||
189 | /** |
||
190 | * Load the search history from the source. |
||
191 | * |
||
192 | * @return SearchLog[] |
||
193 | */ |
||
194 | abstract public function loadSearchHistory(); |
||
195 | } |
||
196 |
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.