Conditions | 8 |
Paths | 4 |
Total Lines | 105 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
75 | public function rules() |
||
76 | { |
||
77 | return [ |
||
78 | [ |
||
79 | [ |
||
80 | 'title', |
||
81 | 'content', |
||
82 | 'active', |
||
83 | 'metaKeys', |
||
84 | 'metaDescription', |
||
85 | ], |
||
86 | 'required', |
||
87 | ], |
||
88 | [ |
||
89 | [ |
||
90 | 'description', |
||
91 | 'content', |
||
92 | ], |
||
93 | 'string', |
||
94 | ], |
||
95 | [ |
||
96 | [ |
||
97 | 'icon', |
||
98 | 'title', |
||
99 | 'metaKeys', |
||
100 | 'alias', |
||
101 | ], |
||
102 | 'string', |
||
103 | 'max' => 128, |
||
104 | ], |
||
105 | [ |
||
106 | [ |
||
107 | 'metaDescription', |
||
108 | ], |
||
109 | 'string', |
||
110 | 'max' => 255, |
||
111 | ], |
||
112 | [ |
||
113 | [ |
||
114 | 'parentId', |
||
115 | 'active' |
||
116 | ], |
||
117 | 'integer', |
||
118 | ], |
||
119 | [ |
||
120 | 'parentId', |
||
121 | 'filter', |
||
122 | 'filter' => function ($value) { |
||
123 | if (empty($value)) { |
||
124 | return null; |
||
125 | } else { |
||
126 | return MenuWidget::checkNewParentId($this, $value) ? $value : $this->getOldAttribute('parentId'); |
||
127 | } |
||
128 | } |
||
129 | ], |
||
130 | [ |
||
131 | 'alias', |
||
132 | 'filter', |
||
133 | 'filter' => function ($value) { |
||
134 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
135 | } |
||
136 | ], |
||
137 | [ |
||
138 | 'alias', |
||
139 | 'unique', |
||
140 | 'skipOnError' => true, |
||
141 | 'targetClass' => static::class, |
||
142 | 'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
143 | ], |
||
144 | [ |
||
145 | UploadModelInterface::FILE_TYPE_THUMB, |
||
146 | function($attribute){ |
||
147 | if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
||
148 | $this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
||
149 | } |
||
150 | }, |
||
151 | 'skipOnError' => false, |
||
152 | ], |
||
153 | [ |
||
154 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
155 | function($attribute) { |
||
156 | if (!is_array($this->{$attribute})) { |
||
157 | $this->addError($attribute, 'Image field content must be an array.'); |
||
158 | } |
||
159 | }, |
||
160 | 'skipOnError' => false, |
||
161 | ], |
||
162 | [ |
||
163 | 'albums', |
||
164 | 'each', |
||
165 | 'rule' => ['integer'], |
||
166 | ], |
||
167 | [ |
||
168 | 'title', |
||
169 | 'unique', |
||
170 | 'skipOnError' => true, |
||
171 | 'targetClass' => static::class, |
||
172 | 'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
173 | ], |
||
174 | [ |
||
175 | [ |
||
176 | 'created_at', |
||
177 | 'updated_at', |
||
178 | ], |
||
179 | 'safe', |
||
180 | ], |
||
299 |