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 |
||
| 24 | abstract class Element extends BaseComponent |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var [ElementInterface[]] |
||
| 29 | */ |
||
| 30 | protected $_cacheById = []; |
||
| 31 | |||
| 32 | /******************************************* |
||
| 33 | * ELEMENT CLASSES |
||
| 34 | *******************************************/ |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | public abstract static function elementClass(): string; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The element instance that this class interacts with |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public static function elementClassInstance(): string |
||
| 50 | |||
| 51 | /******************************************* |
||
| 52 | * CREATE |
||
| 53 | *******************************************/ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param array $config |
||
| 57 | * @return BaseElement|ElementInterface |
||
| 58 | */ |
||
| 59 | public function create(array $config = []) |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $identifier |
||
| 76 | * @param int|null $siteId |
||
| 77 | * @return BaseElement|ElementInterface|null |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function find($identifier, int $siteId = null) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param int $id |
||
| 106 | * @param int|null $siteId |
||
| 107 | * @return BaseElement|ElementInterface|null |
||
| 108 | */ |
||
| 109 | public function findById(int $id, int $siteId = null) |
||
| 133 | |||
| 134 | /******************************************* |
||
| 135 | * GET |
||
| 136 | *******************************************/ |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $identifier |
||
| 140 | * @param int|null $siteId |
||
| 141 | * @return BaseElement|ElementInterface |
||
| 142 | * @throws ElementNotFoundException |
||
| 143 | */ |
||
| 144 | public function get($identifier, int $siteId = null) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param int $id |
||
| 160 | * @param int|null $siteId |
||
| 161 | * @return BaseElement|ElementInterface |
||
| 162 | * @throws ElementNotFoundException |
||
| 163 | */ |
||
| 164 | public function getById(int $id, int $siteId = null) |
||
| 177 | |||
| 178 | /******************************************* |
||
| 179 | * FRESH FIND |
||
| 180 | *******************************************/ |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param int $id |
||
| 184 | * @param int|null $siteId |
||
| 185 | * @return BaseElement|ElementInterface|null |
||
| 186 | */ |
||
| 187 | public function freshFindById(int $id, int $siteId = null) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param $id |
||
| 194 | * @param int|null $siteId |
||
| 195 | * @return BaseElement|ElementInterface |
||
| 196 | * @throws ElementNotFoundException |
||
| 197 | */ |
||
| 198 | public function freshGetById($id, int $siteId = null) |
||
| 210 | |||
| 211 | |||
| 212 | /******************************************* |
||
| 213 | * QUERY |
||
| 214 | *******************************************/ |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get query |
||
| 218 | * |
||
| 219 | * @param $criteria |
||
| 220 | * @return ElementQueryInterface |
||
| 221 | */ |
||
| 222 | public function getQuery($criteria = []) |
||
| 240 | |||
| 241 | /******************************************* |
||
| 242 | * CACHE |
||
| 243 | *******************************************/ |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param $identifier |
||
| 247 | * @param int|null $siteId |
||
| 248 | * @return BaseElement|ElementInterface|null |
||
| 249 | */ |
||
| 250 | public function findCache($identifier, int $siteId = null) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param ElementInterface $element |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function addToCache(ElementInterface $element) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Find an existing cache by ID |
||
| 278 | * |
||
| 279 | * @param int $id |
||
| 280 | * @param int|null $siteId |
||
| 281 | * @return BaseElement|ElementInterface|null |
||
| 282 | */ |
||
| 283 | public function findCacheById(int $id, int $siteId = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Identify whether in cached by ID |
||
| 302 | * |
||
| 303 | * @param int $id |
||
| 304 | * @param int|null $siteId |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | View Code Duplication | protected function isCachedById(int $id, int $siteId = null) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param ElementInterface $element |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | protected function cacheById(ElementInterface $element) |
||
| 343 | |||
| 344 | /******************************************* |
||
| 345 | * EXCEPTIONS |
||
| 346 | *******************************************/ |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @throws ElementNotFoundException |
||
| 350 | */ |
||
| 351 | protected function notFoundException() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param int|null $id |
||
| 364 | * @throws ElementNotFoundException |
||
| 365 | */ |
||
| 366 | protected function notFoundByIdException(int $id = null) |
||
| 377 | |||
| 378 | } |
||
| 379 |