GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 951ad1...29ba67 )
by Alexey
08:55
created

BProductAssignment   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 264
Duplicated Lines 2.65 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 46.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 39
c 1
b 0
f 0
lcom 1
cbo 8
dl 7
loc 264
ccs 59
cts 126
cp 0.4683
rs 8.2857

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 7 1
A getDepends() 0 10 1
A getListOptions() 0 10 2
A getCheckBoxListOptions() 0 7 1
A renderAjaxHtml() 0 18 4
A getDependedModels() 0 8 1
A saveAssignments() 0 13 2
A toProductAttribute() 0 4 1
A toToAssignmentAttribute() 0 4 1
A getSections() 0 4 1
A getTypes() 0 4 1
C calculateAttributes() 0 33 7
A prepareAttributes() 0 14 4
A saveAssignment() 0 13 3
A deleteAssignments() 7 7 1
A getCriteria() 0 18 1
C beforeSave() 0 31 7

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * @author Sergey Glagolev <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package backend.modules.product.models
8
 *
9
 * @method static BProductAssignment model(string $class = __CLASS__)
10
 *
11
 * @property integer $id
12
 * @property integer $product_id
13
 * @property integer $visible
14
 */
15
class BProductAssignment extends BActiveRecord
16
{
17 25
  public function getFields()
18
  {
19 25
    $fields = $this->getMetaData()->columns;
20 25
    unset($fields['id'], $fields['product_id'], $fields['visible']);
21
22 25
    return $fields;
23
  }
24
25
  /**
26
   * Получаем данные зависимого атрибута
27
   *
28
   * @param $attribute
29
   * @param $dependedAttribute
30
   *
31
   * @return array
32
   */
33 1
  public function getDepends($attribute, $dependedAttribute)
34
  {
35 1
    $attribute = $this->toToAssignmentAttribute($attribute);
36 1
    $dependedAttribute = $this->toToAssignmentAttribute($dependedAttribute);
37
38 1
    $criteria = $this->getCriteria($attribute, $dependedAttribute);
39 1
    $data = $this->getDependedModels($dependedAttribute, $criteria);
40
41 1
    return $data;
42
  }
43
44
  /**
45
   * Формируем данные для DropDownList, чтобы перестроить список при ajax запросе
46
   *
47
   * @param $model
48
   * @param $attribute
49
   * @param $data
50
   *
51
   * @return string
52
   */
53 1
  public function getListOptions($model, $attribute, $data)
54
  {
55 1
    $data = CHtml::listData($data, 'id', 'name');
56 1
    $options = CHtml::tag('option', array('value' => ''), 'Не задано', true);
57
58 1
    foreach($data as $value => $name)
59 1
      $options .= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
60
61 1
    return $options;
62
  }
63
64
  public function getCheckBoxListOptions($model, $attribute, $data)
65
  {
66
    $data = CHtml::listData($data, 'id', 'name');
67
68
    $form = new BActiveForm();
69
    return $form->checkBoxList($model, $attribute, $data);
70
  }
71
72 1
  public function renderAjaxHtml($model, $attribute, $depended)
73
  {
74 1
    $data = $this->getDepends($attribute, $depended);
75
76 1
    $relationName = BProductStructure::getRelationName(BProductStructure::getModelName($depended));
77 1
    $relation = $model->getActiveRelation($relationName);
78 1
    $type = $relation instanceof CHasManyRelation ? 'checkboxlist' : 'dropdown';
0 ignored issues
show
Bug introduced by
The class CHasManyRelation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
79
80
    switch($type)
81
    {
82 1
      case 'dropdown':
83 1
        return $this->getListOptions($model, $depended, $data);
84
        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
85
      case 'checkboxlist':
86
        return $this->getCheckBoxListOptions($model, $depended, $data);
87
        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
88
    }
89
  }
90
91
  /**
92
   * @param $modelClass
93
   * @param null|CDbCriteria $criteria
94
   */
95 1
  public function getDependedModels($modelClass, $criteria = null)
96
  {
97 1
    $modelClass = $this->toToAssignmentAttribute($modelClass);
98 1
    $modelClass = 'BProduct'.ucfirst($modelClass);
99 1
    $model      = $modelClass::model();
100
101 1
    return $model->findAll($criteria);
102
  }
103
104
  public function saveAssignments($product, $assignments)
105
  {
106
    $saved = array();
107
    $attributes = $this->calculateAttributes($assignments);
108
109
    foreach($attributes as $assignment)
110
    {
111
      $model = $this->saveAssignment($product, $assignment);
112
      $saved[] = $model->id;
113
    }
114
115
    $this->deleteAssignments($product, $saved);
116
  }
117
118 1
  public function toProductAttribute($attribute)
119
  {
120 1
    return $attribute.'_id';
121
  }
122
123 3
  public function toToAssignmentAttribute($attribute)
124
  {
125 3
    return str_replace("_id", "", $attribute);
126
  }
127
128
  public function getSections($criteria = null)
129
  {
130
    return BProductSection::model()->findAll($criteria);
131
  }
132
133
  public function getTypes($criteria = null)
134
  {
135
    return BProductType::model()->findAll($criteria);
136
  }
137
138
  /**
139
   * @param $assignments
140
   *
141
   * @return array
142
   */
143
  protected function calculateAttributes($assignments)
144
  {
145
    $this->prepareAttributes($assignments);
146
147
    $arrays = $digits = array();
148
149
    foreach($assignments as $key => $item)
150
      is_array($item) ? $arrays[$key] = $item : $digits[$key] = $item;
151
152
    if( !empty($arrays) )
153
    {
154
      $statistics = new Statistics($arrays);
155
      $combinations = $statistics->getCombinations();
156
157
      $attributes = array();
158
      foreach($combinations as $i => $combination)
159
      {
160
        foreach($this->getFields() as $field)
161
        {
162
          if( in_array($field->name, array_keys($digits)) )
163
            $attributes[$i][$field->name] = $digits[$field->name];
164
          else
165
            $attributes[$i][$field->name] = $combination[array_flip(array_keys($arrays))[$field->name]];
166
        }
167
      }
168
    }
169
    else
170
    {
171
      $attributes = array($digits);
172
    }
173
174
    return $attributes;
175
  }
176
177
  protected function prepareAttributes(&$assignments)
178
  {
179
    foreach($assignments as $field => $value)
180
    {
181
      if( empty($value) )
182
      {
183
        $assignments[$field] = null;
184
      }
185
      else if( is_array($value) )
186
      {
187
        $assignments[$field] = array_combine(range(0, count($value) - 1), $value);
188
      }
189
    }
190
  }
191
192
  /**
193
   * @param BProduct $product
194
   * @param array $attributes
195
   * @return BProductAssignment|CActiveRecord
196
   * @throws CHttpException
197
   */
198
  protected function saveAssignment(BProduct $product, array $attributes)
199
  {
200
    $attributes['product_id'] = $product->id;
201
202
    if( !$model = $this->findByAttributes($attributes) )
203
      $model = new BProductAssignment();
204
205
    $model->setAttributes($attributes, false);
206
    if( !$model->save() )
207
      throw new CHttpException(500, 'Не удается сохранить модель привязок продукта');
208
209
    return $model;
210
  }
211
212 View Code Duplication
  protected function deleteAssignments($product, array $savedIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
  {
214
    $criteria = new CDbCriteria();
215
    $criteria->compare('product_id', $product->id);
216
    $criteria->addNotInCondition('id', $savedIds);
217
    $this->deleteAll($criteria);
218
  }
219
220
  /**
221
   * Формируем критерий для выборки - массив id подходящих по зависимости атрибутов
222
   *
223
   * @param $attribute
224
   * @param $dependedAttribute
225
   *
226
   * @return CDbCriteria
227
   */
228 1
  protected function getCriteria($attribute, $dependedAttribute)
229
  {
230 1
    $dstAttribute = $this->toProductAttribute($attribute);
231
232
    $attributes = array(
233 1
      'src' => $dependedAttribute,
234 1
      'dst' => $attribute,
235 1
      'dst_id' => $this->{$dstAttribute}
236 1
    );
237
238 1
    $assignments = BProductTreeAssignment::model()->findAllByAttributes($attributes);
239
    $ids = array_map(function($model){return $model->src_id;}, $assignments);
240
241 1
    $criteria = new CDbCriteria();
242 1
    $criteria->addInCondition("id", $ids);
243
244 1
    return $criteria;
245
  }
246
247 2
  protected function beforeSave()
248
  {
249 2
    $this->visible = true;
250
251 2
    foreach(array_keys($this->getFields()) as $row)
252
    {
253 2
      $class = 'BProduct'.ucfirst($this->toToAssignmentAttribute($row));
254
      /**
255
       * @var BActiveRecord $model
256
       */
257 2
      $model = new $class;
258
259 2
      if( !($model instanceof BProductStructure) )
260 2
      {
261
        throw new CException('Model '.get_class($model).' must implement BProductStructure class');
262
      }
263
264 2
      if( $this->{$row} && $model = $model->findByPk($this->{$row}) )
265 2
      {
266 2
        $this->visible = $this->visible && $model->visible;
267 2
      }
268
269 2
      if( !$this->visible )
270 2
      {
271 2
        break;
272
      }
273 2
    }
274
275 2
    $this->visible = intval($this->visible);
276 2
    return parent::beforeSave();
277
  }
278
}