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 |
||
20 | trait ElementByString |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var [ElementInterface[]] |
||
25 | */ |
||
26 | protected $_cacheByString = []; |
||
27 | |||
28 | /******************************************* |
||
29 | * ABSTRACTS |
||
30 | *******************************************/ |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | abstract protected function stringProperty(): string; |
||
36 | |||
37 | /** |
||
38 | * @param $string |
||
39 | * @param int|null $siteId |
||
40 | * @return BaseElement|ElementInterface|null |
||
41 | */ |
||
42 | abstract protected function freshFindByString(string $string, int $siteId = null); |
||
43 | |||
44 | /** |
||
45 | * @param ElementInterface $element |
||
46 | * @return string |
||
47 | */ |
||
48 | protected function stringValue(ElementInterface $element): string |
||
56 | |||
57 | /** |
||
58 | * @param string $string |
||
59 | * @param int|null $siteId |
||
60 | * @return BaseElement|ElementInterface|null |
||
61 | */ |
||
62 | public function findByString(string $string, int $siteId = null) |
||
86 | |||
87 | /** |
||
88 | * @param string $string |
||
89 | * @param int|null $siteId |
||
90 | * @return BaseElement|ElementInterface |
||
91 | * @throws ElementNotFoundException |
||
92 | */ |
||
93 | public function getByString(string $string, int $siteId = null) |
||
106 | |||
107 | |||
108 | /** |
||
109 | * @param string $string |
||
110 | * @param int|null $siteId |
||
111 | * @return BaseElement|ElementInterface |
||
112 | * @throws ElementNotFoundException |
||
113 | */ |
||
114 | public function freshGetByString(string $string, int $siteId = null) |
||
126 | |||
127 | |||
128 | /******************************************* |
||
129 | * CACHE |
||
130 | *******************************************/ |
||
131 | |||
132 | /** |
||
133 | * Find an existing cache by Handle |
||
134 | * |
||
135 | * @param string $string |
||
136 | * @param int|null $siteId |
||
137 | * @return BaseElement|ElementInterface|null |
||
138 | */ |
||
139 | public function findCacheByString(string $string, int $siteId = null) |
||
152 | |||
153 | /** |
||
154 | * Identify whether in cached by string |
||
155 | * |
||
156 | * @param $string |
||
157 | * @param int|null $siteId |
||
158 | * @return bool |
||
159 | */ |
||
160 | View Code Duplication | protected function isCachedByString($string, int $siteId = null) |
|
173 | |||
174 | /** |
||
175 | * @param ElementInterface $element |
||
176 | * @return $this |
||
177 | */ |
||
178 | protected function cacheByString(ElementInterface $element) |
||
203 | |||
204 | |||
205 | |||
206 | |||
207 | /******************************************* |
||
208 | * EXCEPTIONS |
||
209 | *******************************************/ |
||
210 | |||
211 | /** |
||
212 | * @param null $string |
||
213 | * @throws ElementNotFoundException |
||
214 | */ |
||
215 | protected function notFoundByStringException($string = null) |
||
226 | |||
227 | } |
||
228 |
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.