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