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 |
||
| 37 | class LinkObserver extends AbstractProductImportObserver |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The product link processor instance. |
||
| 42 | * |
||
| 43 | * @var \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface |
||
| 44 | */ |
||
| 45 | protected $productLinkProcessor; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Initialize the observer with the passed product link processor instance. |
||
| 49 | * |
||
| 50 | * @param \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface $productLinkProcessor The product link processor instance |
||
| 51 | */ |
||
| 52 | public function __construct(ProductLinkProcessorInterface $productLinkProcessor) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return's the product link processor instance. |
||
| 59 | * |
||
| 60 | * @return \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface The product link processor instance |
||
| 61 | */ |
||
| 62 | protected function getProductLinkProcessor() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Process the observer's business logic. |
||
| 69 | * |
||
| 70 | * @return array The processed row |
||
| 71 | */ |
||
| 72 | protected function process() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Prepare the attributes of the entity that has to be persisted. |
||
| 87 | * |
||
| 88 | * @return array The prepared attributes |
||
| 89 | */ |
||
| 90 | protected function prepareAttributes() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Initialize the product link with the passed attributes and returns an instance. |
||
| 134 | * |
||
| 135 | * @param array $attr The product link attributes |
||
| 136 | * |
||
| 137 | * @return array The initialized product link |
||
| 138 | */ |
||
| 139 | View Code Duplication | protected function initializeProductLink(array $attr) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Load's the link with the passed product/linked product/link type ID. |
||
| 158 | * |
||
| 159 | * @param integer $productId The product ID of the link to load |
||
| 160 | * @param integer $linkedProductId The linked product ID of the link to load |
||
| 161 | * @param integer $linkTypeId The link type ID of the product to load |
||
| 162 | * |
||
| 163 | * @return array The link |
||
| 164 | */ |
||
| 165 | protected function loadProductLink($productId, $linkedProductId, $linkTypeId) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Temporary persist the last link ID. |
||
| 172 | * |
||
| 173 | * @param integer $lastLinkId The last link ID |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | protected function setLastLinkId($lastLinkId) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Persist's the passed product link data and return's the ID. |
||
| 184 | * |
||
| 185 | * @param array $productLink The product link data to persist |
||
| 186 | * |
||
| 187 | * @return string The ID of the persisted entity |
||
| 188 | */ |
||
| 189 | protected function persistProductLink($productLink) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return the entity ID for the passed SKU. |
||
| 196 | * |
||
| 197 | * @param string $sku The SKU to return the entity ID for |
||
| 198 | * |
||
| 199 | * @return integer The mapped entity ID |
||
| 200 | * @throws \TechDivision\Import\Product\Link\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
||
| 201 | */ |
||
| 202 | protected function mapSkuToEntityId($sku) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return the entity ID for the passed SKU. |
||
| 209 | * |
||
| 210 | * @param string $sku The SKU to return the entity ID for |
||
| 211 | * |
||
| 212 | * @return integer The mapped entity ID |
||
| 213 | * @throws \TechDivision\Import\Product\Link\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
||
| 214 | */ |
||
| 215 | protected function mapSku($sku) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return the link type ID for the passed link type code. |
||
| 222 | * |
||
| 223 | * @param string $linkTypeCode The link type code to return the link type ID for |
||
| 224 | * |
||
| 225 | * @return integer The mapped link type ID |
||
| 226 | * @throws \TechDivision\Import\Product\Link\Exceptions\MapLinkTypeCodeToIdException Is thrown if the link type code is not mapped yet |
||
| 227 | */ |
||
| 228 | protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
| 235 | * |
||
| 236 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
| 237 | */ |
||
| 238 | protected function isDebugMode() |
||
| 242 | } |
||
| 243 |
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: