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 |
||
| 14 | class OrderStrategy extends AbstractImportStrategy |
||
| 15 | { |
||
| 16 | const CONTEXT_ORDER_POST_PROCESS_IDS = 'postProcessOrderIds'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Order |
||
| 20 | */ |
||
| 21 | protected $existingEntity; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param Order $entity |
||
| 25 | * |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | protected function beforeProcessEntity($entity) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param Order $entity |
||
| 40 | * |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | protected function afterProcessEntity($entity) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param Order $order |
||
| 70 | * @param Customer $customer |
||
| 71 | */ |
||
| 72 | protected function processCustomer(Order $order, Customer $customer = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * If cart exists then add relation to it, |
||
| 99 | * do nothing otherwise |
||
| 100 | * |
||
| 101 | * @param Order $entity |
||
| 102 | */ |
||
| 103 | protected function processCart(Order $entity) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param Order $order |
||
| 122 | * |
||
| 123 | * @return OrderStrategy |
||
| 124 | */ |
||
| 125 | protected function processItems(Order $order) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param Order $order |
||
| 137 | * |
||
| 138 | * @return OrderStrategy |
||
| 139 | */ |
||
| 140 | protected function processAddresses(Order $order) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * BC layer to find existing collection items by old identity filed values |
||
| 152 | * |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | protected function findExistingEntity($entity, array $searchContext = []) |
||
| 156 | { |
||
| 157 | $existingEntity = parent::findExistingEntity($entity, $searchContext); |
||
| 158 | |||
| 159 | if (!$existingEntity && $entity instanceof OrderAddress) { |
||
| 160 | /** @var OrderAddress $existingEntity */ |
||
| 161 | $existingEntity = $this->existingEntity->getAddresses() |
||
| 162 | ->filter( |
||
| 163 | function (OrderAddress $address) use ($entity) { |
||
| 164 | $isMatched = true; |
||
| 165 | $fieldsToMatch = ['street', 'city', 'postalCode', 'country', 'region']; |
||
| 166 | |||
| 167 | foreach ($fieldsToMatch as $fieldToMatch) { |
||
| 168 | $addressValue = $this->getPropertyAccessor()->getValue($address, $fieldToMatch); |
||
| 169 | $entityValue = $this->getPropertyAccessor()->getValue($entity, $fieldToMatch); |
||
| 170 | $isMatched = $isMatched && ($addressValue === $entityValue); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $isMatched; |
||
| 174 | } |
||
| 175 | ) |
||
| 176 | ->first(); |
||
| 177 | |||
| 178 | if ($existingEntity && $entity->getOriginId()) { |
||
| 179 | $existingEntity->setOriginId($entity->getOriginId()); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($entity instanceof OrderItem && is_null($entity->getName())) { |
||
| 184 | //name can't be null, so to avoid import job failing empty string is used |
||
| 185 | $entity->setName(''); |
||
| 186 | } |
||
| 187 | |||
| 188 | $existingEntity = $this->findRegionEntity($entity, $existingEntity); |
||
| 189 | |||
| 190 | return $existingEntity; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param $entity |
||
| 195 | * @param $existingEntity |
||
| 196 | * |
||
| 197 | * @return null|object |
||
| 198 | */ |
||
| 199 | View Code Duplication | protected function findRegionEntity($entity, $existingEntity) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Add special identifier for entities not existing in Magento |
||
| 224 | * Add customer Email to search context for processing related entity Guest Customer for Order |
||
| 225 | * |
||
| 226 | * @param string $entityName |
||
| 227 | * @param array $identityValues |
||
| 228 | * @return null|object |
||
| 229 | */ |
||
| 230 | protected function findEntityByIdentityValues($entityName, array $identityValues) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Add special search context for entities not existing in Magento |
||
| 244 | * Add customer Email to search context for Order related entity Guest Customer |
||
| 245 | * |
||
| 246 | * @param object $entity |
||
| 247 | * @param string $entityClass |
||
| 248 | * @param array $searchContext |
||
| 249 | * @return array|null |
||
| 250 | */ |
||
| 251 | protected function combineIdentityValues($entity, $entityClass, array $searchContext) |
||
| 259 | } |
||
| 260 |
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: