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:
Complex classes like Import often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Import, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class Import extends Component |
||
| 22 | { |
||
| 23 | protected $object; |
||
| 24 | protected $properties = null; |
||
| 25 | public $filename; |
||
| 26 | public $addPropertyGroups = []; |
||
| 27 | public $createIfNotExists = false; |
||
| 28 | public $multipleValuesDelimiter = '|'; |
||
| 29 | public $additionalFields = []; |
||
| 30 | |||
| 31 | /* |
||
| 32 | * Export method |
||
| 33 | */ |
||
| 34 | abstract public function getData($header, $data); |
||
| 35 | /* |
||
| 36 | * Import method |
||
| 37 | */ |
||
| 38 | abstract public function setData(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param array $config |
||
| 42 | * @return ImportCsv |
||
| 43 | * @throws \Exception |
||
| 44 | */ |
||
| 45 | public static function createInstance($config) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param $objectId |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public static function getFields($objectId) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $objectId |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | protected static function getProperties($objectId) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return Object |
||
| 104 | */ |
||
| 105 | public function getObject() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param array $config |
||
| 112 | */ |
||
| 113 | public function __construct($config = []) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param $objectId |
||
| 130 | * @param $object |
||
| 131 | * @param array $objectFields |
||
| 132 | * @param array $properties |
||
| 133 | * @param array $propertiesFields |
||
| 134 | * @param array $row |
||
| 135 | * @param array $titleFields |
||
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | protected function save($objectId, $object, $objectFields = [], $properties = [], $propertiesFields = [], $row=[], $titleFields=[], $columnsCount = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param array $exportFields |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getAllFields($fields = []) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param $exportFields |
||
| 319 | * @param array $conditions |
||
| 320 | * @param int $batchSize |
||
| 321 | * @return bool |
||
| 322 | * @throws \Exception |
||
| 323 | */ |
||
| 324 | public function processExport($exportFields = [], $conditions = [], $batchSize = 100) |
||
| 325 | { |
||
| 326 | $fields = $this->getAllFields($exportFields); |
||
| 327 | |||
| 328 | $class = $this->object->object_class; |
||
| 329 | /** @var $select ActiveQuery */ |
||
| 330 | $select = $class::find(); |
||
| 331 | |||
| 332 | $representationConversions = [ |
||
| 333 | 'text' => 'name', |
||
| 334 | 'value' => 'value', |
||
| 335 | 'id' => 'psv_id', |
||
| 336 | ]; |
||
| 337 | $product = Yii::$container->get(Product::class); |
||
| 338 | if ( |
||
| 339 | isset($conditions['category']) && |
||
| 340 | is_array($conditions['category']) && |
||
| 341 | $this->object->id == Object::getForClass(get_class($product))->id |
||
| 342 | ) { |
||
| 343 | foreach ($conditions['category'] as $condition) { |
||
| 344 | $joinTableName = 'Category'.$condition['value']; |
||
| 345 | |||
| 346 | $select->innerJoin( |
||
| 347 | "{{%product_category}} " . $joinTableName, |
||
| 348 | "$joinTableName.object_model_id = product.id" |
||
| 349 | ); |
||
| 350 | $select->andWhere( |
||
| 351 | new Expression( |
||
| 352 | '`' . $joinTableName . '`.`category_id` = "'.$condition['value'].'"' |
||
| 353 | ) |
||
| 354 | ); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | if (isset($conditions['field']) && is_array($conditions['field'])) { |
||
| 359 | foreach ($conditions['field'] as $condition) { |
||
| 360 | $conditionOptions = [$condition['operators'], $condition['value'], $condition['option']]; |
||
| 361 | if ($condition['comparison'] == 'AND') { |
||
| 362 | $select->andWhere($conditionOptions); |
||
| 363 | } elseif ($condition['comparison'] == 'OR') { |
||
| 364 | $select->orWhere($conditionOptions); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | if (isset($conditions['property']) && is_array($conditions['property'])) { |
||
| 369 | foreach ($conditions['property'] as $condition) { |
||
| 370 | $property = Property::findById($condition['value']); |
||
| 371 | |||
| 372 | if ($property && isset($condition['option']) && !empty($condition['option'])) { |
||
| 373 | if ($property->is_eav) { |
||
| 374 | $joinTableName = 'EAVJoinTable'.$property->id; |
||
| 375 | |||
| 376 | $select->innerJoin( |
||
| 377 | $this->object->eav_table_name . " " . $joinTableName, |
||
| 378 | "$joinTableName.object_model_id = " . |
||
| 379 | Yii::$app->db->quoteTableName($this->object->object_table_name) . ".id " |
||
| 380 | ); |
||
| 381 | $select->andWhere( |
||
| 382 | new Expression( |
||
| 383 | '`' . $joinTableName . '`.`value` '.$condition['operators'].' "'.$condition['option'].'" AND `' . |
||
| 384 | $joinTableName . '`.`key` = "'. $property->key.'"' |
||
| 385 | ) |
||
| 386 | ); |
||
| 387 | } elseif ($property->has_static_values) { |
||
| 388 | $joinTableName = 'OSVJoinTable'.$property->id; |
||
| 389 | $propertyStaticValue = PropertyStaticValues::find()->where(['value'=>$condition['option']])->one(); |
||
| 390 | |||
| 391 | if ($propertyStaticValue) { |
||
| 392 | $select->innerJoin( |
||
| 393 | ObjectStaticValues::tableName() . " " . $joinTableName, |
||
| 394 | "$joinTableName.object_id = " . intval($this->object->id) . |
||
| 395 | " AND $joinTableName.object_model_id = " . |
||
| 396 | Yii::$app->db->quoteTableName($this->object->object_table_name) . ".id " |
||
| 397 | ); |
||
| 398 | |||
| 399 | $select->andWhere( |
||
| 400 | new Expression( |
||
| 401 | '`' . $joinTableName . '`.`property_static_value_id` ="'.$propertyStaticValue->id.'"' |
||
| 402 | ) |
||
| 403 | ); |
||
| 404 | } |
||
| 405 | } else { |
||
| 406 | throw new \Exception("Wrong property type for ".$property->id); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | $data = []; |
||
| 413 | $batchSize = intval($batchSize) <= 0 ? 100 : intval($batchSize); |
||
| 414 | foreach ($select->each($batchSize) as $object) { |
||
| 415 | $row = []; |
||
| 416 | |||
| 417 | foreach ($fields['fields_object'] as $field) { |
||
| 418 | if ('internal_id' === $field) { |
||
| 419 | $row[] = $object->id; |
||
| 420 | } else { |
||
| 421 | $row[] = isset($object->$field) ? $object->$field : ''; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | foreach ($fields['fields_property'] as $field_id => $field) { |
||
| 426 | $value = $object->getPropertyValuesByPropertyId($field_id); |
||
| 427 | |||
| 428 | if (!is_object($value)) { |
||
| 429 | $value = ''; |
||
| 430 | } elseif (count($value->values) > 1 && isset($fields_property[$field_id])) { |
||
| 431 | if (isset($fields_property[$field_id]['processValuesAs'])) { |
||
| 432 | $attributeToGet = $representationConversions[$fields_property[$field_id]['processValuesAs']]; |
||
| 433 | $newValues = []; |
||
| 434 | foreach ($value->values as $val) { |
||
| 435 | $newValues[] = $val[$attributeToGet]; |
||
| 436 | } |
||
| 437 | $value = implode($this->multipleValuesDelimiter, $newValues); |
||
| 438 | } |
||
| 439 | } else { |
||
| 440 | $value = (string) $value; |
||
| 441 | } |
||
| 442 | |||
| 443 | $row[] = $value; |
||
| 444 | } |
||
| 445 | |||
| 446 | if (!empty($fields['fields_additional']) && $object->hasMethod('getAdditionalFields')) { |
||
| 447 | $fieldsFromModel = $object->getAdditionalFields($fields['fields_additional']); |
||
| 448 | foreach ($fields['fields_additional'] as $key => $configuration) { |
||
| 449 | if (!isset($fieldsFromModel[$key])) { |
||
| 450 | $fieldsFromModel[$key] = ''; |
||
| 451 | } |
||
| 452 | |||
| 453 | if (!empty($fieldsFromModel[$key])) { |
||
| 454 | $value = (array)$fieldsFromModel[$key]; |
||
| 455 | $row[] = implode($this->multipleValuesDelimiter, $value); |
||
| 456 | } else { |
||
| 457 | $row[] = ''; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | $data[] = $row; |
||
| 463 | } |
||
| 464 | |||
| 465 | unset($value, $row, $object, $select, $class); |
||
| 466 | |||
| 467 | return $this->getData($fields['fields_header'], $data); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param array $importFields |
||
| 472 | * @return bool |
||
| 473 | * @throws \Exception |
||
| 474 | * @throws \yii\db\Exception |
||
| 475 | */ |
||
| 476 | public function processImport($importFields = []) |
||
| 548 | } |
||
| 549 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.