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 |
||
| 21 | class CollectionLoader |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * possible return value when adding entities to a collection. |
||
| 26 | * denotes that the entity was NOT ADDED to the collection |
||
| 27 | */ |
||
| 28 | const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * possible return value when adding entities to a collection. |
||
| 32 | * denotes that the entity was SUCCESSFULLY ADDED to the collection |
||
| 33 | */ |
||
| 34 | const ENTITY_ADDED = 'entity-added-to-collection'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * possible return value when adding entities to a collection. |
||
| 38 | * denotes that the entity was ALREADY ADDED to the collection, |
||
| 39 | * and therefore could not be added again. |
||
| 40 | */ |
||
| 41 | const ENTITY_EXISTS = 'entity-already-in-collection'; |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @var CollectionDetailsInterface $collection_details |
||
| 46 | */ |
||
| 47 | protected $collection_details; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var CollectionInterface $collection |
||
| 51 | */ |
||
| 52 | protected $collection; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var FileLocator $file_locator |
||
| 56 | */ |
||
| 57 | protected $file_locator; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * CollectionLoader constructor. |
||
| 62 | * |
||
| 63 | * @param CollectionDetailsInterface $collection_details |
||
| 64 | * @param CollectionInterface $collection |
||
| 65 | * @param LocatorInterface $file_locator |
||
| 66 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 67 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
| 68 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 69 | * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
||
| 70 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @access public |
||
| 90 | * @return \EventEspresso\core\services\collections\CollectionInterface |
||
| 91 | */ |
||
| 92 | public function getCollection() |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @access protected |
||
| 100 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
| 101 | * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
||
| 102 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 103 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 104 | */ |
||
| 105 | protected function loadAllFromFilepaths() |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * loadClassFromFilepath |
||
| 131 | * |
||
| 132 | * @access protected |
||
| 133 | * @param string $filepath |
||
| 134 | * @return string |
||
| 135 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 136 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 137 | * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
||
| 138 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
| 139 | */ |
||
| 140 | protected function loadClassFromFilepath($filepath) |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * addEntityToCollection |
||
| 162 | * |
||
| 163 | * @access protected |
||
| 164 | * @param $entity |
||
| 165 | * @param mixed $identifier |
||
| 166 | * @return string |
||
| 167 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 168 | */ |
||
| 169 | protected function addEntityToCollection($entity, $identifier) |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * setIdentifier |
||
| 208 | * |
||
| 209 | * @access protected |
||
| 210 | * @param $entity |
||
| 211 | * @param mixed $identifier |
||
| 212 | * @return string |
||
| 213 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 214 | */ |
||
| 215 | protected function setIdentifier($entity, $identifier) |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * loadFromFQCNs |
||
| 258 | * |
||
| 259 | * @access protected |
||
| 260 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
| 261 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 262 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 263 | */ |
||
| 264 | protected function loadFromFQCNs() |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * loadClassFromFQCN |
||
| 281 | * |
||
| 282 | * @access protected |
||
| 283 | * @param string $FQCN Fully Qualified Class Name |
||
| 284 | * @return string |
||
| 285 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
| 286 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 287 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
| 288 | */ |
||
| 289 | protected function loadClassFromFQCN($FQCN) |
||
| 302 | } |
||
| 303 |