| Conditions | 51 |
| Paths | 188 |
| Total Lines | 185 |
| Code Lines | 173 |
| Lines | 67 |
| Ratio | 36.22 % |
| 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 |
||
| 214 | protected function parseRule($rule, $ruleName, &$attributeData, $seed) |
||
| 215 | { |
||
| 216 | $faker = Factory::create(); |
||
| 217 | $faker->seed(crc32($seed)); |
||
| 218 | |||
| 219 | $parsedRule = $this->parseStringRule($rule); |
||
| 220 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
| 221 | list($rule, $parameters) = $parsedRule; |
||
| 222 | |||
| 223 | switch ($rule) { |
||
| 224 | case 'required': |
||
| 225 | $attributeData['required'] = true; |
||
| 226 | break; |
||
| 227 | case 'accepted': |
||
| 228 | $attributeData['required'] = true; |
||
| 229 | $attributeData['type'] = 'boolean'; |
||
| 230 | $attributeData['value'] = true; |
||
| 231 | break; |
||
| 232 | View Code Duplication | case 'after': |
|
| 233 | $attributeData['type'] = 'date'; |
||
| 234 | $attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription(); |
||
| 235 | $attributeData['value'] = date(DATE_RFC850, strtotime('+1 day', strtotime($parameters[0]))); |
||
| 236 | break; |
||
| 237 | View Code Duplication | case 'alpha': |
|
| 238 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 239 | $attributeData['value'] = $faker->word; |
||
| 240 | break; |
||
| 241 | case 'alpha_dash': |
||
| 242 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 243 | break; |
||
| 244 | case 'alpha_num': |
||
| 245 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 246 | break; |
||
| 247 | View Code Duplication | case 'in': |
|
| 248 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 249 | $attributeData['value'] = $faker->randomElement($parameters); |
||
| 250 | break; |
||
| 251 | View Code Duplication | case 'not_in': |
|
| 252 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 253 | $attributeData['value'] = $faker->word; |
||
| 254 | break; |
||
| 255 | View Code Duplication | case 'min': |
|
| 256 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 257 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 258 | $attributeData['value'] = $faker->numberBetween($parameters[0]); |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | View Code Duplication | case 'max': |
|
| 262 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 263 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 264 | $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); |
||
| 265 | } |
||
| 266 | break; |
||
| 267 | case 'between': |
||
| 268 | if (! isset($attributeData['type'])) { |
||
| 269 | $attributeData['type'] = 'numeric'; |
||
| 270 | } |
||
| 271 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 272 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
| 273 | break; |
||
| 274 | View Code Duplication | case 'before': |
|
| 275 | $attributeData['type'] = 'date'; |
||
| 276 | $attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription(); |
||
| 277 | $attributeData['value'] = date(DATE_RFC850, strtotime('-1 day', strtotime($parameters[0]))); |
||
| 278 | break; |
||
| 279 | case 'date_format': |
||
| 280 | $attributeData['type'] = 'date'; |
||
| 281 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 282 | $attributeData['value'] = date($parameters[0]); |
||
| 283 | break; |
||
| 284 | case 'different': |
||
| 285 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 286 | break; |
||
| 287 | case 'digits': |
||
| 288 | $attributeData['type'] = 'numeric'; |
||
| 289 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 290 | $attributeData['value'] = $faker->randomNumber($parameters[0], true); |
||
| 291 | break; |
||
| 292 | case 'digits_between': |
||
| 293 | $attributeData['type'] = 'numeric'; |
||
| 294 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 295 | break; |
||
| 296 | View Code Duplication | case 'file': |
|
| 297 | $attributeData['type'] = 'file'; |
||
| 298 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 299 | break; |
||
| 300 | View Code Duplication | case 'image': |
|
| 301 | $attributeData['type'] = 'image'; |
||
| 302 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 303 | break; |
||
| 304 | case 'json': |
||
| 305 | $attributeData['type'] = 'string'; |
||
| 306 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 307 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
| 308 | break; |
||
| 309 | case 'mimetypes': |
||
| 310 | View Code Duplication | case 'mimes': |
|
| 311 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 312 | break; |
||
| 313 | View Code Duplication | case 'required_if': |
|
| 314 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 315 | break; |
||
| 316 | View Code Duplication | case 'required_unless': |
|
| 317 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 318 | break; |
||
| 319 | View Code Duplication | case 'required_with': |
|
| 320 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 321 | break; |
||
| 322 | View Code Duplication | case 'required_with_all': |
|
| 323 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 324 | break; |
||
| 325 | View Code Duplication | case 'required_without': |
|
| 326 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 327 | break; |
||
| 328 | View Code Duplication | case 'required_without_all': |
|
| 329 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 330 | break; |
||
| 331 | case 'same': |
||
| 332 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 333 | break; |
||
| 334 | case 'size': |
||
| 335 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 336 | break; |
||
| 337 | View Code Duplication | case 'timezone': |
|
| 338 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 339 | $attributeData['value'] = $faker->timezone; |
||
| 340 | break; |
||
| 341 | case 'exists': |
||
| 342 | $fieldName = isset($parameters[1]) ? $parameters[1] : $ruleName; |
||
| 343 | $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); |
||
| 344 | break; |
||
| 345 | case 'active_url': |
||
| 346 | $attributeData['type'] = 'url'; |
||
| 347 | $attributeData['value'] = $faker->url; |
||
| 348 | break; |
||
| 349 | case 'regex': |
||
| 350 | $attributeData['type'] = 'string'; |
||
| 351 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 352 | break; |
||
| 353 | case 'boolean': |
||
| 354 | $attributeData['value'] = true; |
||
| 355 | $attributeData['type'] = $rule; |
||
| 356 | break; |
||
| 357 | case 'array': |
||
| 358 | $attributeData['value'] = $faker->word; |
||
| 359 | $attributeData['type'] = $rule; |
||
| 360 | break; |
||
| 361 | case 'date': |
||
| 362 | $attributeData['value'] = $faker->date(); |
||
| 363 | $attributeData['type'] = $rule; |
||
| 364 | break; |
||
| 365 | case 'email': |
||
| 366 | $attributeData['value'] = $faker->safeEmail; |
||
| 367 | $attributeData['type'] = $rule; |
||
| 368 | break; |
||
| 369 | case 'string': |
||
| 370 | $attributeData['value'] = $faker->word; |
||
| 371 | $attributeData['type'] = $rule; |
||
| 372 | break; |
||
| 373 | case 'integer': |
||
| 374 | $attributeData['value'] = $faker->randomNumber(); |
||
| 375 | $attributeData['type'] = $rule; |
||
| 376 | break; |
||
| 377 | case 'numeric': |
||
| 378 | $attributeData['value'] = $faker->randomNumber(); |
||
| 379 | $attributeData['type'] = $rule; |
||
| 380 | break; |
||
| 381 | case 'url': |
||
| 382 | $attributeData['value'] = $faker->url; |
||
| 383 | $attributeData['type'] = $rule; |
||
| 384 | break; |
||
| 385 | case 'ip': |
||
| 386 | $attributeData['value'] = $faker->ipv4; |
||
| 387 | $attributeData['type'] = $rule; |
||
| 388 | break; |
||
| 389 | } |
||
| 390 | |||
| 391 | if ($attributeData['value'] === '') { |
||
| 392 | $attributeData['value'] = $faker->word; |
||
| 393 | } |
||
| 394 | |||
| 395 | if (is_null($attributeData['type'])) { |
||
| 396 | $attributeData['type'] = 'string'; |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 499 |
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
$aand$care used. There was no need to assign$b.Instead, the list call could have been.