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 |
||
| 35 | abstract class AbstractEavSubject extends AbstractSubject implements EavSubjectInterface |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $attributes = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The available user defined EAV attributes, grouped by their entity type. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $userDefinedAttributes = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The attribute set of the entity that has to be created. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $attributeSet = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The available EAV attribute sets. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $attributeSets = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The mapping for the supported backend types (for the EAV entity) => persist methods. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $backendTypes = array( |
||
| 72 | 'datetime' => array('persistDatetimeAttribute', 'loadDatetimeAttribute'), |
||
| 73 | 'decimal' => array('persistDecimalAttribute', 'loadDecimalAttribute'), |
||
| 74 | 'int' => array('persistIntAttribute', 'loadIntAttribute'), |
||
| 75 | 'text' => array('persistTextAttribute', 'loadTextAttribute'), |
||
| 76 | 'varchar' => array('persistVarcharAttribute', 'loadVarcharAttribute') |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The default mappings for the user defined attributes, based on the attributes frontend input type. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $defaultFrontendInputCallbackMappings = array( |
||
| 85 | 'select' => 'TechDivision\\Import\\Callbacks\\SelectCallback', |
||
| 86 | 'multiselect' => 'TechDivision\\Import\\Callbacks\\MultiselectCallback', |
||
| 87 | 'boolean' => 'TechDivision\\Import\\Callbacks\\BooleanCallback' |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Return's the default callback frontend input mappings for the user defined attributes. |
||
| 92 | * |
||
| 93 | * @return array The default frontend input callback mappings |
||
| 94 | */ |
||
| 95 | public function getDefaultFrontendInputCallbackMappings() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
| 105 | */ |
||
| 106 | public function setUp() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
| 143 | * |
||
| 144 | * @return array The mapping for the supported backend types |
||
| 145 | */ |
||
| 146 | public function getBackendTypes() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set's the attribute set of the product that has to be created. |
||
| 153 | * |
||
| 154 | * @param array $attributeSet The attribute set |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | public function setAttributeSet(array $attributeSet) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Return's the attribute set of the product that has to be created. |
||
| 165 | * |
||
| 166 | * @return array The attribute set |
||
| 167 | */ |
||
| 168 | public function getAttributeSet() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return's the store ID of the actual row, or of the default store |
||
| 175 | * if no store view code is set in the CSV file. |
||
| 176 | * |
||
| 177 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 178 | * |
||
| 179 | * @return integer The ID of the actual store |
||
| 180 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 181 | */ |
||
| 182 | public function getRowStoreId($default = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Cast's the passed value based on the backend type information. |
||
| 212 | * |
||
| 213 | * @param string $backendType The backend type to cast to |
||
| 214 | * @param mixed $value The value to be casted |
||
| 215 | * |
||
| 216 | * @return mixed The casted value |
||
| 217 | */ |
||
| 218 | public function castValueByBackendType($backendType, $value) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return's the entity type code to be used. |
||
| 242 | * |
||
| 243 | * @return string The entity type code to be used |
||
| 244 | */ |
||
| 245 | public function getEntityTypeCode() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Return's the attribute set with the passed attribute set name. |
||
| 252 | * |
||
| 253 | * @param string $attributeSetName The name of the requested attribute set |
||
| 254 | * |
||
| 255 | * @return array The attribute set data |
||
| 256 | * @throws \Exception Is thrown, if the attribute set with the passed name is not available |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function getAttributeSetByAttributeSetName($attributeSetName) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Return's the attributes for the attribute set of the product that has to be created. |
||
| 285 | * |
||
| 286 | * @return array The attributes |
||
| 287 | * @throws \Exception Is thrown if the attributes for the actual attribute set are not available |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function getAttributes() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Return's an array with the available user defined EAV attributes for the actual entity type. |
||
| 316 | * |
||
| 317 | * @return array The array with the user defined EAV attributes |
||
| 318 | */ |
||
| 319 | public function getEavUserDefinedAttributes() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Return's the EAV attribute with the passed attribute code. |
||
| 333 | * |
||
| 334 | * @param string $attributeCode The attribute code |
||
| 335 | * |
||
| 336 | * @return array The array with the EAV attribute |
||
| 337 | * @throws \Exception Is thrown if the attribute with the passed code is not available |
||
| 338 | */ |
||
| 339 | public function getEavAttributeByAttributeCode($attributeCode) |
||
| 360 | } |
||
| 361 |