Conditions | 43 |
Paths | 84 |
Total Lines | 166 |
Code Lines | 159 |
Lines | 16 |
Ratio | 9.64 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
175 | protected function parseRule($rule, &$attributeData) |
||
176 | { |
||
177 | $faker = Factory::create(); |
||
178 | |||
179 | $parsedRule = $this->parseStringRule($rule); |
||
180 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
181 | list($rule, $parameters) = $parsedRule; |
||
182 | |||
183 | switch ($rule) { |
||
184 | case 'required': |
||
185 | $attributeData['required'] = true; |
||
186 | break; |
||
187 | case 'accepted': |
||
188 | $attributeData['required'] = true; |
||
189 | $attributeData['type'] = 'boolean'; |
||
190 | $attributeData['value'] = true; |
||
191 | break; |
||
192 | View Code Duplication | case 'after': |
|
193 | $attributeData['type'] = 'date'; |
||
194 | $attributeData['description'][] = 'Must be a date after: `'.date(DATE_RFC850, strtotime($parameters[0])).'`'; |
||
195 | $attributeData['value'] = date(DATE_RFC850, strtotime('+1 day', strtotime($parameters[0]))); |
||
196 | break; |
||
197 | case 'alpha': |
||
198 | $attributeData['description'][] = 'Only alphabetic characters allowed'; |
||
199 | $attributeData['value'] = $faker->word; |
||
200 | break; |
||
201 | case 'alpha_dash': |
||
202 | $attributeData['description'][] = 'Allowed: alpha-numeric characters, as well as dashes and underscores.'; |
||
203 | break; |
||
204 | case 'alpha_num': |
||
205 | $attributeData['description'][] = 'Only alpha-numeric characters allowed'; |
||
206 | break; |
||
207 | case 'in': |
||
208 | $attributeData['description'][] = $this->fancyImplode($parameters, ', ', ' or '); |
||
209 | $attributeData['value'] = $faker->randomElement($parameters); |
||
210 | break; |
||
211 | case 'not_in': |
||
212 | $attributeData['description'][] = 'Not in: '.$this->fancyImplode($parameters, ', ', ' or '); |
||
213 | $attributeData['value'] = $faker->word; |
||
214 | break; |
||
215 | case 'min': |
||
216 | $attributeData['description'][] = 'Minimum: `'.$parameters[0].'`'; |
||
217 | break; |
||
218 | case 'max': |
||
219 | $attributeData['description'][] = 'Maximum: `'.$parameters[0].'`'; |
||
220 | break; |
||
221 | case 'between': |
||
222 | $attributeData['type'] = 'numeric'; |
||
223 | $attributeData['description'][] = 'Between: `'.$parameters[0].'` and `'.$parameters[1].'`'; |
||
224 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
225 | break; |
||
226 | View Code Duplication | case 'before': |
|
227 | $attributeData['type'] = 'date'; |
||
228 | $attributeData['description'][] = 'Must be a date preceding: `'.date(DATE_RFC850, strtotime($parameters[0])).'`'; |
||
229 | $attributeData['value'] = date(DATE_RFC850, strtotime('-1 day', strtotime($parameters[0]))); |
||
230 | break; |
||
231 | case 'date_format': |
||
232 | $attributeData['type'] = 'date'; |
||
233 | $attributeData['description'][] = 'Date format: `'.$parameters[0].'`'; |
||
234 | break; |
||
235 | case 'different': |
||
236 | $attributeData['description'][] = 'Must have a different value than parameter: `'.$parameters[0].'`'; |
||
237 | break; |
||
238 | case 'digits': |
||
239 | $attributeData['type'] = 'numeric'; |
||
240 | $attributeData['description'][] = 'Must have an exact length of `'.$parameters[0].'`'; |
||
241 | $attributeData['value'] = $faker->randomNumber($parameters[0], true); |
||
242 | break; |
||
243 | case 'digits_between': |
||
244 | $attributeData['type'] = 'numeric'; |
||
245 | $attributeData['description'][] = 'Must have a length between `'.$parameters[0].'` and `'.$parameters[1].'`'; |
||
246 | break; |
||
247 | case 'image': |
||
248 | $attributeData['type'] = 'image'; |
||
249 | $attributeData['description'][] = 'Must be an image (jpeg, png, bmp, gif, or svg)'; |
||
250 | break; |
||
251 | case 'json': |
||
252 | $attributeData['type'] = 'string'; |
||
253 | $attributeData['description'][] = 'Must be a valid JSON string.'; |
||
254 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
255 | break; |
||
256 | case 'mimetypes': |
||
257 | case 'mimes': |
||
258 | $attributeData['description'][] = 'Allowed mime types: '.$this->fancyImplode($parameters, ', ', ' or '); |
||
259 | break; |
||
260 | View Code Duplication | case 'required_if': |
|
261 | $attributeData['description'][] = 'Required if `'.$parameters[0].'` is `'.$parameters[1].'`'; |
||
262 | break; |
||
263 | View Code Duplication | case 'required_unless': |
|
264 | $attributeData['description'][] = 'Required unless `'.$parameters[0].'` is `'.$parameters[1].'`'; |
||
265 | break; |
||
266 | case 'required_with': |
||
267 | $attributeData['description'][] = 'Required if the parameters '.$this->fancyImplode($parameters, ', ', ' or ').' are present.'; |
||
268 | break; |
||
269 | case 'required_with_all': |
||
270 | $attributeData['description'][] = 'Required if the parameters '.$this->fancyImplode($parameters, ', ', ' and ').' are present.'; |
||
271 | break; |
||
272 | case 'required_without': |
||
273 | $attributeData['description'][] = 'Required if the parameters '.$this->fancyImplode($parameters, ', ', ' or ').' are not present.'; |
||
274 | break; |
||
275 | case 'required_without_all': |
||
276 | $attributeData['description'][] = 'Required if the parameters '.$this->fancyImplode($parameters, ', ', ' and ').' are not present.'; |
||
277 | break; |
||
278 | case 'same': |
||
279 | $attributeData['description'][] = 'Must be the same as `'.$parameters[0].'`'; |
||
280 | break; |
||
281 | case 'size': |
||
282 | $attributeData['description'][] = 'Must have the size of `'.$parameters[0].'`'; |
||
283 | break; |
||
284 | case 'timezone': |
||
285 | $attributeData['description'][] = 'Must be a valid timezone identifier'; |
||
286 | $attributeData['value'] = $faker->timezone; |
||
287 | break; |
||
288 | case 'exists': |
||
289 | $attributeData['description'][] = 'Valid '.Str::singular($parameters[0]).' '.$parameters[1]; |
||
290 | break; |
||
291 | case 'active_url': |
||
292 | $attributeData['type'] = 'url'; |
||
293 | $attributeData['value'] = $faker->url; |
||
294 | break; |
||
295 | case 'regex': |
||
296 | $attributeData['type'] = 'string'; |
||
297 | $attributeData['description'][] = 'Must match this regular expression: `'.$parameters[0].'`'; |
||
298 | break; |
||
299 | case 'boolean': |
||
300 | $attributeData['value'] = true; |
||
301 | $attributeData['type'] = $rule; |
||
302 | break; |
||
303 | case 'array': |
||
304 | $attributeData['value'] = $faker->word; |
||
305 | $attributeData['type'] = $rule; |
||
306 | break; |
||
307 | case 'date': |
||
308 | $attributeData['value'] = $faker->date(); |
||
309 | $attributeData['type'] = $rule; |
||
310 | break; |
||
311 | case 'email': |
||
312 | $attributeData['value'] = $faker->safeEmail; |
||
313 | $attributeData['type'] = $rule; |
||
314 | break; |
||
315 | case 'string': |
||
316 | $attributeData['value'] = $faker->word; |
||
317 | $attributeData['type'] = $rule; |
||
318 | break; |
||
319 | case 'integer': |
||
320 | $attributeData['value'] = $faker->randomNumber(); |
||
321 | $attributeData['type'] = $rule; |
||
322 | break; |
||
323 | case 'numeric': |
||
324 | $attributeData['value'] = $faker->randomNumber(); |
||
325 | $attributeData['type'] = $rule; |
||
326 | break; |
||
327 | case 'url': |
||
328 | $attributeData['value'] = $faker->url; |
||
329 | $attributeData['type'] = $rule; |
||
330 | break; |
||
331 | case 'ip': |
||
332 | $attributeData['value'] = $faker->ipv4; |
||
333 | $attributeData['type'] = $rule; |
||
334 | break; |
||
335 | } |
||
336 | |||
337 | if ($attributeData['value'] === '') { |
||
338 | $attributeData['value'] = $faker->word; |
||
339 | } |
||
340 | } |
||
341 | |||
441 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.