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