| Conditions | 13 | 
| Paths | 13 | 
| Total Lines | 81 | 
| Code Lines | 45 | 
| 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  | 
            ||
| 40 | public function validate($validator)  | 
            ||
| 41 |     { | 
            ||
| 42 | // If no discount is set continue doing default validation  | 
            ||
| 43 |         if (!isset($this->value) || empty($this->value)) { | 
            ||
| 44 | return parent::validate($validator);  | 
            ||
| 45 | }  | 
            ||
| 46 | |||
| 47 | /** @var Discount $discount */  | 
            ||
| 48 | // Check if the discount exists  | 
            ||
| 49 |         if (!$discount = Discount::get()->find('Code', $this->value)) { | 
            ||
| 50 | $validator->validationError($this->name, _t(  | 
            ||
| 51 | 'DiscountField.VALIDATION_NOT_FOUND',  | 
            ||
| 52 | 'The entered coupon is not found'  | 
            ||
| 53 | ), 'validation');  | 
            ||
| 54 | |||
| 55 | return false;  | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | // Check if the discount is already used  | 
            ||
| 59 |         if ($discount->validateUses()) { | 
            ||
| 60 | $validator->validationError($this->name, _t(  | 
            ||
| 61 | 'DiscountField.VALIDATION_USED_CHECK',  | 
            ||
| 62 | 'The entered coupon is already used'  | 
            ||
| 63 | ), 'validation');  | 
            ||
| 64 | |||
| 65 | return false;  | 
            ||
| 66 | }  | 
            ||
| 67 | |||
| 68 | /** @var DiscountForm form */  | 
            ||
| 69 | //$this->form;  | 
            ||
| 70 | |||
| 71 | // Check if the coupon is expired  | 
            ||
| 72 |         if (!$checkDate = $discount->validateDate()) { | 
            ||
| 73 | $validator->validationError($this->name, _t(  | 
            ||
| 74 | 'DiscountField.VALIDATION_DATE_CHECK',  | 
            ||
| 75 | 'The coupon is expired'  | 
            ||
| 76 | ), 'validation');  | 
            ||
| 77 | |||
| 78 | return false;  | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 | // Check if the coupon is allowed on this event  | 
            ||
| 82 |         if (!$checkEvent = $discount->validateEvents($this->form->getReservation()->Event())) { | 
            ||
| 83 | $validator->validationError($this->name, _t(  | 
            ||
| 84 | 'DiscountField.VALIDATION_EVENT_CHECK',  | 
            ||
| 85 | 'The coupon is not allowed on this event'  | 
            ||
| 86 | ), 'validation');  | 
            ||
| 87 | |||
| 88 | return false;  | 
            ||
| 89 | }  | 
            ||
| 90 | |||
| 91 | // If groups are required check if one of the attendees is in the required group  | 
            ||
| 92 |         if (!$checkMember = $discount->validateGroups()) { | 
            ||
| 93 |             foreach ($this->form->getReservation()->Attendees() as $attendee) { | 
            ||
| 94 | /** @var Attendee $attendee */  | 
            ||
| 95 |                 if ($attendee->Member()->exists() && $member = $attendee->Member()) { | 
            ||
| 96 |                     if ($checkMember = $discount->validateGroups($member)) { | 
            ||
| 97 | // If one of the member is part of the group validate the discount  | 
            ||
| 98 | break;  | 
            ||
| 99 |                     } else { | 
            ||
| 100 | $checkMember = false;  | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | }  | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 |         if (!$checkMember) { | 
            ||
| 107 | $validator->validationError($this->name, _t(  | 
            ||
| 108 | 'DiscountField.VALIDATION_MEMBER_CHECK',  | 
            ||
| 109 | 'None of the attendees is allowed to use this coupon'  | 
            ||
| 110 | ), 'validation');  | 
            ||
| 111 | |||
| 112 | return false;  | 
            ||
| 113 | }  | 
            ||
| 114 | |||
| 115 | $discount->write();  | 
            ||
| 116 | $this->form->getReservation()->PriceModifiers()->add($discount);  | 
            ||
| 117 | $this->form->getReservation()->calculateTotal();  | 
            ||
| 118 | $this->form->getReservation()->write();  | 
            ||
| 119 | return true;  | 
            ||
| 120 | }  | 
            ||
| 121 | }  |