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 |
||
| 42 | class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface, FileUploadSubjectInterface |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The trait that implements the export functionality. |
||
| 47 | * |
||
| 48 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
| 49 | */ |
||
| 50 | use ExportableTrait; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The trait that provides file upload functionality. |
||
| 54 | * |
||
| 55 | * @var \TechDivision\Import\Subjects\FileUploadTrait |
||
| 56 | */ |
||
| 57 | use FileUploadTrait; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The array with the pre-loaded entity IDs. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $preLoadedEntityIds = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Mappings for the table column => CSV column header. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $headerStockMappings = array( |
||
| 72 | 'qty' => array('qty', 'float'), |
||
| 73 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
| 74 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
| 75 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
| 76 | 'backorders' => array('allow_backorders', 'int'), |
||
| 77 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
| 78 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
| 79 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
| 80 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
| 81 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
| 82 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
| 83 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
| 84 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
| 85 | 'manage_stock' => array('manage_stock', 'int'), |
||
| 86 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
| 87 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
| 88 | 'qty_increments' => array('qty_increments', 'float'), |
||
| 89 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
| 90 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
| 91 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The array with the available visibility keys. |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $availableVisibilities = array( |
||
| 100 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
| 101 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
| 102 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
| 103 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The category IDs the product is related with. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $productCategoryIds = array(); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The default callback mappings for the Magento standard product attributes. |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | protected $defaultCallbackMappings = array( |
||
| 119 | 'visibility' => array('import_product.callback.visibility'), |
||
| 120 | 'tax_class_id' => array('import_product.callback.tax.class'), |
||
| 121 | 'bundle_price_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 122 | 'bundle_sku_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 123 | 'bundle_weight_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 124 | 'bundle_price_view' => array('import_product_bundle.callback.bundle.price.view'), |
||
| 125 | 'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type') |
||
| 126 | ); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The used URL keys. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $usedUrlKeys = array(); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The available entity types. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | protected $entityTypes = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 144 | * |
||
| 145 | * @param string $serial The serial of the actual import |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | 18 | public function setUp($serial) |
|
| 150 | { |
||
| 151 | |||
| 152 | // load the status of the actual import |
||
| 153 | 18 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
| 154 | |||
| 155 | // load the global data we've prepared initially |
||
| 156 | 18 | $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES]; |
|
| 157 | |||
| 158 | // initialize the flag whether to copy images or not |
||
| 159 | 18 | if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) { |
|
| 160 | $this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)); |
||
|
|
|||
| 161 | } |
||
| 162 | |||
| 163 | // initialize media directory => can be absolute or relative |
||
| 164 | 18 | if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
|
| 165 | $this->setMediaDir( |
||
| 166 | $this->resolvePath( |
||
| 167 | $this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY) |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | // initialize images directory => can be absolute or relative |
||
| 173 | 18 | if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
|
| 174 | $this->setImagesFileDir( |
||
| 175 | $this->resolvePath( |
||
| 176 | $this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY) |
||
| 177 | ) |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | // invoke the parent method |
||
| 182 | 18 | parent::setUp($serial); |
|
| 183 | 18 | } |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Clean up the global data after importing the bunch. |
||
| 187 | * |
||
| 188 | * @param string $serial The serial of the actual import |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function tearDown($serial) |
||
| 193 | { |
||
| 194 | |||
| 195 | // invoke the parent method |
||
| 196 | parent::tearDown($serial); |
||
| 197 | |||
| 198 | // load the registry processor |
||
| 199 | $registryProcessor = $this->getRegistryProcessor(); |
||
| 200 | |||
| 201 | // update the status |
||
| 202 | $registryProcessor->mergeAttributesRecursive( |
||
| 203 | RegistryKeys::STATUS, |
||
| 204 | array( |
||
| 205 | RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds, |
||
| 206 | ) |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return's the default callback mappings. |
||
| 212 | * |
||
| 213 | * @return array The default callback mappings |
||
| 214 | */ |
||
| 215 | 18 | public function getDefaultCallbackMappings() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return's the mappings for the table column => CSV column header. |
||
| 222 | * |
||
| 223 | * @return array The header stock mappings |
||
| 224 | */ |
||
| 225 | 1 | public function getHeaderStockMappings() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Return's the visibility key for the passed visibility string. |
||
| 232 | * |
||
| 233 | * @param string $visibility The visibility string to return the key for |
||
| 234 | * |
||
| 235 | * @return integer The requested visibility key |
||
| 236 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
| 237 | */ |
||
| 238 | public function getVisibilityIdByValue($visibility) |
||
| 239 | { |
||
| 240 | |||
| 241 | // query whether or not, the requested visibility is available |
||
| 242 | if (isset($this->availableVisibilities[$visibility])) { |
||
| 243 | // load the visibility ID, add the mapping and return the ID |
||
| 244 | return $this->availableVisibilities[$visibility]; |
||
| 245 | } |
||
| 246 | |||
| 247 | // throw an exception, if not |
||
| 248 | throw new \Exception( |
||
| 249 | $this->appendExceptionSuffix( |
||
| 250 | sprintf('Found invalid visibility %s', $visibility) |
||
| 251 | ) |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Add the passed category ID to the product's category list. |
||
| 257 | * |
||
| 258 | * @param integer $categoryId The category ID to add |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function addProductCategoryId($categoryId) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Pre-load the entity ID for the passed product. |
||
| 269 | * |
||
| 270 | * @param array $product The product to be pre-loaded |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | public function preLoadEntityId(array $product) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return's the list with category IDs the product is related with. |
||
| 281 | * |
||
| 282 | * @return array The product's category IDs |
||
| 283 | */ |
||
| 284 | public function getProductCategoryIds() |
||
| 285 | { |
||
| 286 | |||
| 287 | // initialize the array with the product's category IDs |
||
| 288 | $categoryIds = array(); |
||
| 289 | |||
| 290 | // query whether or not category IDs are available for the actual product entity |
||
| 291 | if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) { |
||
| 292 | $categoryIds = $this->productCategoryIds[$lastEntityId]; |
||
| 293 | } |
||
| 294 | |||
| 295 | // return the array with the product's category IDs |
||
| 296 | return $categoryIds; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return's the entity type for the configured entity type code. |
||
| 301 | * |
||
| 302 | * @return array The requested entity type |
||
| 303 | * @throws \Exception Is thrown, if the requested entity type is not available |
||
| 304 | */ |
||
| 305 | 1 | View Code Duplication | public function getEntityType() |
| 306 | { |
||
| 307 | |||
| 308 | // query whether or not the entity type with the passed code is available |
||
| 309 | 1 | if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) { |
|
| 310 | 1 | return $this->entityTypes[$entityTypeCode]; |
|
| 311 | } |
||
| 312 | |||
| 313 | // throw a new exception |
||
| 314 | throw new \Exception( |
||
| 315 | $this->appendExceptionSuffix( |
||
| 316 | sprintf('Requested entity type "%s" is not available', $entityTypeCode) |
||
| 317 | ) |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Return's TRUE, if the passed URL key varchar value IS related with the actual PK. |
||
| 323 | * |
||
| 324 | * @param array $productVarcharAttribute The varchar value to check |
||
| 325 | * |
||
| 326 | * @return boolean TRUE if the URL key is related, else FALSE |
||
| 327 | */ |
||
| 328 | public function isUrlKeyOf(array $productVarcharAttribute) |
||
| 333 | } |
||
| 334 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: