Conditions | 9 |
Paths | 32 |
Total Lines | 58 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 14 | ||
Bugs | 5 | Features | 6 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
57 | public function behaviors() |
||
58 | { |
||
59 | /*Sources: |
||
60 | * https://yii2framework.wordpress.com/2014/11/15/yii-2-behaviors-blameable-and-timestamp/comment-page-1/ |
||
61 | * https://toster.ru/q/82962 |
||
62 | * */ |
||
63 | // If table not have fields, then behavior not use |
||
64 | $behaviors = []; |
||
65 | //Check timestamp |
||
66 | if ($this->hasAttribute($this->createdAtAttribute) && $this->hasAttribute($this->updatedAtAttribute)) { |
||
67 | $behaviors['timestamp'] = [ |
||
68 | 'class' => TimestampBehavior::className(), |
||
69 | 'attributes' => [ |
||
70 | ActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], |
||
71 | ActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, |
||
72 | ], |
||
73 | |||
74 | ]; |
||
75 | } |
||
76 | |||
77 | //Check blameable |
||
78 | if ($this->hasAttribute($this->createdByAttribute) && $this->hasAttribute($this->updatedByAttribute)) { |
||
79 | $behaviors['blameable'] = [ |
||
80 | 'class' => UserDataBehavior::className(), |
||
81 | 'attributes' => [ |
||
82 | ActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute], |
||
83 | ActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute, |
||
84 | ], |
||
85 | ]; |
||
86 | } |
||
87 | |||
88 | //Check trash |
||
89 | if ($this->hasAttribute($this->removedAttribute)) { |
||
90 | $behaviors['trash'] = [ |
||
91 | 'class' => TrashBehavior::className(), |
||
92 | 'trashAttribute' => $this->removedAttribute, |
||
93 | ]; |
||
94 | } |
||
95 | |||
96 | //Check locked |
||
97 | if ($this->hasAttribute($this->lockedAttribute)) { |
||
98 | $behaviors['locked'] = [ |
||
99 | 'class' => LockedBehavior::className(), |
||
100 | 'lockedAttribute' => $this->lockedAttribute, |
||
101 | ]; |
||
102 | } |
||
103 | |||
104 | if($this->isNestedSet()){ |
||
105 | $behaviors['tree'] = ArrayHelper::merge([ |
||
106 | 'class' => \creocoder\nestedsets\NestedSetsBehavior::className(), |
||
107 | 'leftAttribute' => 'lft', |
||
108 | 'rightAttribute' => 'rgt', |
||
109 | 'depthAttribute' => 'depth', |
||
110 | ], ($this->hasAttribute('tree')?['treeAttribute' => 'tree']:[])); |
||
111 | } |
||
112 | |||
113 | return $behaviors; |
||
114 | } |
||
115 | |||
137 |