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 ( 1ba1f4...ea41a5 )
by Alexey
05:37
created

ModificationBehavior   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 143
Duplicated Lines 17.48 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 23
lcom 1
cbo 4
dl 25
loc 143
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 12 12 1
A isModification() 0 4 1
A getParentModel() 0 4 1
A getModifications() 0 4 1
A getChild() 0 7 2
A isParent() 0 4 1
A getFirstModification() 0 4 1
B getImageModification() 6 18 5
B getImagesModification() 7 20 5
A getNotEmptyProperty() 0 9 3
A getModificationUrl() 0 6 2

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 Alexey Tatarinov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2015 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 *
8
 * 'modificationBehavior' => array('class' => 'ModificationBehavior'),
9
 */
10
11
/**
12
 * Class ModificationBehavior
13
 * @property Product $owner
14
 * @property Product[] $modifications
15
 * @property Product $parentModel
16
 */
17
class ModificationBehavior extends SActiveRecordBehavior
18
{
19
  private $child;
20
21 View Code Duplication
  public function init()
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...
22
  {
23
    parent::init();
24
25
    $this->owner->getMetaData()->addRelation('modifications', array(
26
      FActiveRecord::HAS_MANY, 'Product', array('parent' => 'id'),
27
    ));
28
29
    $this->owner->getMetaData()->addRelation('parentModel', array(
30
      FActiveRecord::HAS_ONE, 'Product', array('id' => 'parent'),
31
    ));
32
  }
33
34
  /**
35
   * @return bool
36
   */
37
  public function isModification()
38
  {
39
    return !empty($this->owner->parent);
40
  }
41
42
  /**
43
   * @return Product
44
   */
45
  public function getParentModel()
46
  {
47
    return $this->owner->parentModel;
48
  }
49
50
  /**
51
   * @return Product[]
52
   */
53
  public function getModifications()
54
  {
55
    return $this->owner->modifications;
56
  }
57
58
  /**
59
   * @param $id
60
   *
61
   * @return Product|null
62
   */
63
  public function getChild($id)
64
  {
65
    if( !isset($this->child[$id]) )
66
      $this->child[$id] = Product::model()->findByAttributes(array('id' => $id, 'visible' => 1));
67
68
    return $this->child[$id];
69
  }
70
71
  /**
72
   * @return bool
73
   */
74
  public function isParent()
75
  {
76
    return count($this->getModifications()) > 0;
77
  }
78
79
  /**
80
   * @return Product|null
81
   */
82
  public function getFirstModification()
83
  {
84
    return Arr::reset($this->getModifications());
85
  }
86
87
  /**
88
   * Если у модификации нет изображения, метод вернет изображение родителя
89
   * @param string $type
90
   *
91
   * @return FActiveImage
92
   */
93
  public function getImageModification($type = 'main')
94
  {
95
    if( $this->isModification() )
96
    {
97
      if( empty($this->owner->asa('imagesBehavior')->getImage($type)->name) )
98
      {
99
        return $this->getParentModel()->asa('imagesBehavior')->getImage($type);
100
      }
101
    }
102 View Code Duplication
    else if( $this->isParent() )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
103
    {
104
      $modification = $this->getFirstModification();
105
      if( !empty($modification->asa('imagesBehavior')->getImage($type)->name) )
106
        return $modification->asa('imagesBehavior')->getImage($type);
107
    }
108
109
    return $this->owner->asa('imagesBehavior')->getImage($type);
110
  }
111
112
  /**
113
   * @param string $type
114
   *
115
   * @return FActiveImage
116
   */
117
  public function getImagesModification($type = 'main')
118
  {
119
    if( $this->isModification() )
120
    {
121
      $images = $this->owner->asa('imagesBehavior')->getImages($type);
122
      if( empty($images) )
123
      {
124
        return $images;
125
      }
126
    }
127 View Code Duplication
    else if( $this->isParent() )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
    {
129
      $modification = $this->getFirstModification();
130
      $images = $modification->asa('imagesBehavior')->getImages($type);
131
      if( !empty($images) )
132
        return $images;
133
    }
134
135
    return $this->owner->asa('imagesBehavior')->getImages($type);
136
  }
137
138
  /**
139
   * @param $property
140
   *
141
   * @return string
142
   */
143
  public function getNotEmptyProperty($property)
144
  {
145
    if( $this->isModification() && empty($this->owner->{$property}) )
146
    {
147
      return $this->getParentModel()->{$property};
148
    }
149
150
    return $this->owner->{$property};
151
  }
152
153
  public function getModificationUrl($absolute = false)
154
  {
155
156
    //$this->getPa
157
    return $absolute ? Yii::app()->createAbsoluteUrl('product/one', array('url' => $this->url)) : Yii::app()->createUrl('product/one', array('url' => $this->url));
158
  }
159
}