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 | abstract class ElementByString extends Element |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var [ElementInterface[]] |
||
24 | */ |
||
25 | protected $_cacheByString = []; |
||
26 | |||
27 | /******************************************* |
||
28 | * ABSTRACTS |
||
29 | *******************************************/ |
||
30 | |||
31 | /** |
||
32 | * @param $string |
||
33 | * @param int|null $siteId |
||
34 | * @return BaseElement|ElementInterface|null |
||
35 | */ |
||
36 | abstract protected function freshFindByString(string $string, int $siteId = null); |
||
37 | |||
38 | /** |
||
39 | * @param ElementInterface $element |
||
40 | * @return string |
||
41 | */ |
||
42 | abstract protected function stringValue(ElementInterface $element); |
||
43 | |||
44 | |||
45 | /** |
||
46 | * @param $string |
||
47 | * @param int|null $siteId |
||
48 | * @return BaseElement|ElementInterface |
||
49 | * @throws ElementNotFoundException |
||
50 | */ |
||
51 | public function freshGetByString($string, int $siteId = null) |
||
63 | |||
64 | |||
65 | /******************************************* |
||
66 | * FIND |
||
67 | *******************************************/ |
||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | public function find($identifier, int $siteId = null) |
||
84 | |||
85 | /** |
||
86 | * @param $string |
||
87 | * @param int|null $siteId |
||
88 | * @return BaseElement|ElementInterface|null |
||
89 | */ |
||
90 | public function findByString($string, int $siteId = null) |
||
114 | |||
115 | |||
116 | /******************************************* |
||
117 | * GET |
||
118 | *******************************************/ |
||
119 | |||
120 | /** |
||
121 | * @param $string |
||
122 | * @param int|null $siteId |
||
123 | * @return BaseElement|ElementInterface |
||
124 | * @throws ElementNotFoundException |
||
125 | */ |
||
126 | public function getByString($string, int $siteId = null) |
||
139 | |||
140 | |||
141 | /******************************************* |
||
142 | * CACHE |
||
143 | *******************************************/ |
||
144 | |||
145 | /** |
||
146 | * Find an existing cache by Handle |
||
147 | * |
||
148 | * @param $string |
||
149 | * @param int|null $siteId |
||
150 | * @return BaseElement|ElementInterface|null |
||
151 | */ |
||
152 | public function findCacheByString($string, int $siteId = null) |
||
165 | |||
166 | /** |
||
167 | * Identify whether in cached by string |
||
168 | * |
||
169 | * @param $string |
||
170 | * @param int|null $siteId |
||
171 | * @return bool |
||
172 | */ |
||
173 | View Code Duplication | protected function isCachedByString($string, int $siteId = null) |
|
186 | |||
187 | /** |
||
188 | * @param ElementInterface $element |
||
189 | * @return $this |
||
190 | */ |
||
191 | protected function cacheByString(ElementInterface $element) |
||
212 | |||
213 | /** |
||
214 | * @inheritdoc |
||
215 | */ |
||
216 | public function findCache($identifier, int $siteId = null) |
||
228 | |||
229 | /** |
||
230 | * @inheritdoc |
||
231 | */ |
||
232 | public function addToCache(ElementInterface $element) |
||
241 | |||
242 | /******************************************* |
||
243 | * EXCEPTIONS |
||
244 | *******************************************/ |
||
245 | |||
246 | /** |
||
247 | * @param null $string |
||
248 | * @throws ElementNotFoundException |
||
249 | */ |
||
250 | protected function notFoundByStringException($string = null) |
||
261 | |||
262 | } |
||
263 |
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.