Conditions | 9 |
Paths | 1 |
Total Lines | 61 |
Code Lines | 36 |
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 |
||
25 | public function rules() |
||
26 | { |
||
27 | return [ |
||
28 | 'action' => [ |
||
29 | 'required', |
||
30 | 'in:paid,reserved,free' |
||
31 | ], |
||
32 | 'tickets' => [ |
||
33 | 'required', |
||
34 | 'array', |
||
35 | function ($attribute, $value, $fail) { |
||
36 | foreach ($value as $category => $count) { |
||
37 | if (!PriceCategory::find($category)) { |
||
38 | $fail('Please select only offered price categories!'); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | $ticketSum = array_sum($value); |
||
43 | if ($ticketSum === 0) { |
||
44 | $fail('Please select at least one or up to 8 tickets!'); |
||
45 | } |
||
46 | $event = $this->route('event'); |
||
47 | if ($event->freeTickets() < $ticketSum) { |
||
48 | $fail('There are not as many tickets as you chose left. Please only choose a valid amount of tickets!'); |
||
49 | } |
||
50 | } |
||
51 | ], |
||
52 | // CustomerData is always required unless the action is sell --> then no contact information is required |
||
53 | 'customer-name' => [ |
||
54 | 'required_if:action,free,reserved', |
||
55 | 'max:255' |
||
56 | ], |
||
57 | 'selected-seats' => [ |
||
58 | 'sometimes', |
||
59 | 'required', |
||
60 | 'array', |
||
61 | function ($attribute, $value, $fail) { |
||
62 | // We do not check if the amount of selected seats, because we already do this in the checks |
||
63 | // for the amount of tickets. Since the amount of tickets and seats |
||
64 | // has to be equal, the previous check implicits that the seats are |
||
65 | // free by the amount. |
||
66 | $seatsSum = count($value); |
||
67 | $event = $this->route('event'); |
||
68 | |||
69 | // Check if number of tickets and seats is equal |
||
70 | $numberOfTickets = array_sum($this->validator->getData()['tickets']); |
||
71 | if ($seatsSum != $numberOfTickets) { |
||
72 | $fail('Please select as much seats as tickets!'); |
||
73 | } |
||
74 | |||
75 | // Check if the seatIds are in the range of 1 - event's seats amount |
||
76 | $validSeats = array_filter($value, function ($arrItem) use ($event) { |
||
77 | return $arrItem > 0 && $arrItem <= $event->seatMap->seats; |
||
78 | }); |
||
79 | if (count($validSeats) != $seatsSum) { |
||
80 | $fail('Please only select seats displayed on the map!'); |
||
81 | } |
||
82 | |||
83 | // Check if the selected seats are still free |
||
84 | if (!$event->areSeatsFree($value)) { |
||
85 | $fail('There are not as many tickets as you chose left. Please only choose a valid amount of tickets!'); |
||
86 | } |
||
92 |