Conditions | 6 |
Paths | 1 |
Total Lines | 62 |
Code Lines | 38 |
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 |
||
13 | public function boot() |
||
14 | { |
||
15 | Validator::extendImplicit('required_exclusive', function ($attribute, $value, $parameters, $validator) { |
||
16 | $validator->requireParameterCount(1, $parameters, 'required_exclusive'); |
||
17 | |||
18 | $data = $validator->getData(); |
||
19 | $count = collect($parameters) |
||
20 | ->filter(function ($parameter) use ($data) { |
||
21 | return array_key_exists($parameter, $data); |
||
22 | }) |
||
23 | ->filter(function ($parameter) use ($validator, $data) { |
||
24 | return $validator->validateRequired($parameter, $data[$parameter]); |
||
25 | })->count(); |
||
26 | |||
27 | if (! $validator->validateRequired($attribute, $value)) { |
||
28 | return $count === 1; |
||
29 | } |
||
30 | |||
31 | if ($validator->validateRequired($attribute, $value)) { |
||
32 | return $count === 0; |
||
33 | } |
||
34 | }); |
||
35 | |||
36 | Validator::replacer('required_exclusive', function ($message, $attribute, $rule, $parameters) { |
||
37 | // dd($message, $attribute, $rule, $parameters); |
||
38 | $fields = collect($parameters) |
||
39 | ->push($attribute) |
||
40 | ->sort() |
||
41 | ->implode(', '); |
||
42 | |||
43 | return "You must submit exactly one of: {$fields}"; |
||
44 | }); |
||
45 | |||
46 | Validator::extendImplicit('optional_exclusive', function ($attribute, $value, $parameters, $validator) { |
||
47 | $validator->requireParameterCount(1, $parameters, 'required_exclusive'); |
||
48 | |||
49 | $data = $validator->getData(); |
||
50 | $count = collect($parameters) |
||
51 | ->filter(function ($parameter) use ($data) { |
||
52 | return array_key_exists($parameter, $data); |
||
53 | }) |
||
54 | ->filter(function ($parameter) use ($validator, $data) { |
||
55 | return $validator->validateRequired($parameter, $data[$parameter]); |
||
56 | })->count(); |
||
57 | |||
58 | if (! $validator->validateRequired($attribute, $value)) { |
||
59 | return $count === 0 || $count === 1; |
||
60 | } |
||
61 | |||
62 | if ($validator->validateRequired($attribute, $value)) { |
||
63 | return $count === 0; |
||
64 | } |
||
65 | }); |
||
66 | |||
67 | Validator::replacer('optional_exclusive', function ($message, $attribute, $rule, $parameters) { |
||
68 | // dd($message, $attribute, $rule, $parameters); |
||
69 | $fields = collect($parameters) |
||
70 | ->push($attribute) |
||
71 | ->sort() |
||
72 | ->implode(', '); |
||
73 | |||
74 | return "You must submit exactly one or none of: {$fields}"; |
||
75 | }); |
||
86 |