| Conditions | 7 |
| Paths | 64 |
| Total Lines | 104 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 36 | public function __construct( |
||
| 37 | ValidatorInterface $uniqueBookingValidator, |
||
| 38 | ValidatorInterface $userExistsValidator, |
||
| 39 | BookingOptionsInterface $bookingOptions |
||
| 40 | ) { |
||
| 41 | |||
| 42 | //user |
||
| 43 | $user = new Input('user'); |
||
| 44 | $user->setRequired(true); |
||
| 45 | $user->getFilterChain() |
||
| 46 | ->attach(new StripTags()) |
||
| 47 | ->attach(new StringTrim()); |
||
| 48 | |||
| 49 | $user->getValidatorChain() |
||
| 50 | ->attach(new StringLength([ |
||
| 51 | 'encoding' => 'UTF-8', |
||
| 52 | 'min' => 1, |
||
| 53 | 'max' => 512, |
||
| 54 | ])) |
||
| 55 | ->attach($userExistsValidator); |
||
| 56 | |||
| 57 | $this->add($user); |
||
| 58 | |||
| 59 | //date |
||
| 60 | $date = new Input('date'); |
||
| 61 | $date->setRequired(true); |
||
| 62 | $date->getFilterChain() |
||
| 63 | ->attach(new DateTimeFormatter(['format' => 'd-m-Y'])); |
||
| 64 | $date->getValidatorChain() |
||
| 65 | ->attach($uniqueBookingValidator) |
||
| 66 | ->attach(new Date(['format' => 'd-m-Y'])); |
||
| 67 | |||
| 68 | $this->add($date); |
||
| 69 | |||
| 70 | $startTimeValidators = $this->getStartTimeValidators(); |
||
| 71 | |||
| 72 | if ($bookingOptions->getMinStartTime()) { |
||
| 73 | $startTimeValidators[] = new GreaterThan([ |
||
| 74 | 'min' => $bookingOptions->getMinStartTime(), |
||
| 75 | 'inclusive' => true, |
||
| 76 | ]); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($bookingOptions->getMaxStartTime()) { |
||
| 80 | $startTimeValidators[] = new LessThan([ |
||
| 81 | 'max' => $bookingOptions->getMaxStartTime(), |
||
| 82 | 'inclusive' => true, |
||
| 83 | ]); |
||
| 84 | } |
||
| 85 | |||
| 86 | //start time |
||
| 87 | $startTime = new Input('startTime'); |
||
| 88 | $startTime->setRequired(true); |
||
| 89 | $startTime->getFilterChain() |
||
| 90 | ->attach(new StringTrim()); |
||
| 91 | |||
| 92 | foreach ($startTimeValidators as $validator) { |
||
| 93 | $startTime->getValidatorChain()->attach($validator); |
||
| 94 | } |
||
| 95 | $this->add($startTime); |
||
| 96 | |||
| 97 | $endTimeValidators = $this->getEndTimeValidators(); |
||
| 98 | |||
| 99 | if ($bookingOptions->getMinEndTime()) { |
||
| 100 | $endTimeValidators[] = new GreaterThan([ |
||
| 101 | 'min' => $bookingOptions->getMinEndTime(), |
||
| 102 | 'inclusive' => true, |
||
| 103 | ]); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($bookingOptions->getMaxEndTime()) { |
||
| 107 | $endTimeValidators[] = new LessThan([ |
||
| 108 | 'max' => $bookingOptions->getMaxEndTime(), |
||
| 109 | 'inclusive' => true, |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | |||
| 113 | //end time |
||
| 114 | $endTime = new Input('endTime'); |
||
| 115 | $endTime->setRequired(true); |
||
| 116 | $endTime->getFilterChain() |
||
| 117 | ->attach(new StringTrim()); |
||
| 118 | |||
| 119 | foreach ($endTimeValidators as $validator) { |
||
| 120 | $endTime->getValidatorChain()->attach($validator); |
||
| 121 | } |
||
| 122 | $this->add($endTime); |
||
| 123 | |||
| 124 | //notes |
||
| 125 | $notes = new Input('notes'); |
||
| 126 | $notes->setRequired(false); |
||
| 127 | $notes->getFilterChain() |
||
| 128 | ->attach(new StripTags()) |
||
| 129 | ->attach(new StringTrim()); |
||
| 130 | |||
| 131 | $notes->getValidatorChain() |
||
| 132 | ->attach(new StringLength([ |
||
| 133 | 'encoding' => 'UTF-8', |
||
| 134 | 'min' => 1, |
||
| 135 | 'max' => 512, |
||
| 136 | ])); |
||
| 137 | |||
| 138 | $this->add($notes); |
||
| 139 | } |
||
| 140 | |||
| 175 |