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 | abstract class ObjectAccessorByHandle extends ObjectAccessor |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var ObjectWithHandle[] |
||
25 | */ |
||
26 | protected $_cacheByHandle = []; |
||
27 | |||
28 | /** |
||
29 | * @param $identifier |
||
30 | * @param string $toScenario |
||
31 | * @return Object|ObjectWithHandle|null |
||
32 | */ |
||
33 | View Code Duplication | public function find($identifier, string $toScenario = null) |
|
45 | |||
46 | /******************************************* |
||
47 | * FIND/GET BY HANDLE |
||
48 | *******************************************/ |
||
49 | |||
50 | /** |
||
51 | * @param string $handle |
||
52 | * @param string|null $toScenario |
||
53 | * @return ObjectWithHandle|null |
||
54 | */ |
||
55 | View Code Duplication | public function findByHandle(string $handle, string $toScenario = null) |
|
80 | |||
81 | /** |
||
82 | * @param string $handle |
||
83 | * @param string|null $toScenario |
||
84 | * @return ObjectWithHandle|null |
||
85 | * @throws ObjectNotFoundException |
||
86 | */ |
||
87 | View Code Duplication | public function getByHandle(string $handle, string $toScenario = null): ObjectWithHandle |
|
99 | |||
100 | /** |
||
101 | * @param string $handle |
||
102 | * @param string|null $toScenario |
||
103 | * @return ObjectWithHandle|null |
||
104 | */ |
||
105 | public function freshFindByHandle(string $handle, string $toScenario = null) |
||
119 | |||
120 | /** |
||
121 | * @param string $handle |
||
122 | * @param string|null $toScenario |
||
123 | * @return ObjectWithHandle |
||
124 | * @throws ObjectNotFoundException |
||
125 | */ |
||
126 | View Code Duplication | public function freshGetByHandle(string $handle, string $toScenario = null): ObjectWithHandle |
|
138 | |||
139 | |||
140 | /******************************************* |
||
141 | * CACHE |
||
142 | *******************************************/ |
||
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | * @return Object|ObjectWithHandle|null |
||
147 | */ |
||
148 | public function findCache($identifier) |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | public function addToCache(Object $object) |
||
176 | |||
177 | /** |
||
178 | * Find an existing cache by handle |
||
179 | * |
||
180 | * @param string $handle |
||
181 | * @return null |
||
182 | */ |
||
183 | public function findCacheByHandle(string $handle) |
||
194 | |||
195 | /** |
||
196 | * Identify whether in cache by handle |
||
197 | * |
||
198 | * @param string $handle |
||
199 | * @return bool |
||
200 | */ |
||
201 | private function isCachedByHandle(string $handle): bool |
||
205 | |||
206 | /** |
||
207 | * @param ObjectWithHandle $object |
||
208 | * @return static |
||
209 | */ |
||
210 | protected function cacheByHandle(ObjectWithHandle $object) |
||
224 | |||
225 | /** |
||
226 | * @param Record $record |
||
227 | * @return Object|ObjectWithHandle|null |
||
228 | */ |
||
229 | View Code Duplication | public function findCacheByRecord(Record $record) |
|
246 | |||
247 | /******************************************* |
||
248 | * RECORD BY HANDLE |
||
249 | *******************************************/ |
||
250 | |||
251 | /** |
||
252 | * @param string $handle |
||
253 | * @param string|null $toScenario |
||
254 | * @return RecordWithHandle|null |
||
255 | */ |
||
256 | protected function findRecordByHandle(string $handle, string $toScenario = null) |
||
267 | |||
268 | /******************************************* |
||
269 | * EXCEPTIONS |
||
270 | *******************************************/ |
||
271 | |||
272 | /** |
||
273 | * @param string|null $handle |
||
274 | * @throws ObjectNotFoundException |
||
275 | */ |
||
276 | protected function notFoundByHandleException(string $handle = null) |
||
287 | |||
288 | } |
||
289 |
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.