Conditions | 9 |
Paths | 12 |
Total Lines | 75 |
Lines | 36 |
Ratio | 48 % |
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 |
||
47 | public function validate($ticketCode = null) |
||
48 | { |
||
49 | if (filter_var($ticketCode, FILTER_VALIDATE_URL)) { |
||
50 | $asURL = explode('/', parse_url($ticketCode, PHP_URL_PATH)); |
||
51 | $ticketCode = end($asURL); |
||
52 | } |
||
53 | |||
54 | // Check if a code is given to the validator |
||
55 | View Code Duplication | if (!isset($ticketCode)) { |
|
|
|||
56 | return $result = array( |
||
57 | 'Code' => self::MESSAGE_NO_CODE, |
||
58 | 'Message' => self::message(self::MESSAGE_NO_CODE, $ticketCode), |
||
59 | 'Type' => self::MESSAGE_TYPE_BAD, |
||
60 | 'Ticket' => $ticketCode, |
||
61 | 'Attendee' => null |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | // Check if a ticket exists with the given ticket code |
||
66 | if (!$this->attendee = Attendee::get()->find('TicketCode', $ticketCode)) { |
||
67 | return $result = array( |
||
68 | 'Code' => self::MESSAGE_CODE_NOT_FOUND, |
||
69 | 'Message' => self::message(self::MESSAGE_CODE_NOT_FOUND, $ticketCode), |
||
70 | 'Type' => self::MESSAGE_TYPE_BAD, |
||
71 | 'Ticket' => $ticketCode, |
||
72 | 'Attendee' => null |
||
73 | ); |
||
74 | } else { |
||
75 | $name = $this->attendee->getName(); |
||
76 | } |
||
77 | |||
78 | // Check if the reservation is not canceled |
||
79 | if (!(bool)$this->attendee->Event()->getGuestList()->find('ID', $this->attendee->ID)) { |
||
80 | return $result = array( |
||
81 | 'Code' => self::MESSAGE_TICKET_CANCELLED, |
||
82 | 'Message' => self::message(self::MESSAGE_TICKET_CANCELLED, $name), |
||
83 | 'Type' => self::MESSAGE_TYPE_BAD, |
||
84 | 'Ticket' => $ticketCode, |
||
85 | 'Attendee' => $this->attendee |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | // Check if the ticket is already checked in and not allowed to check out |
||
90 | View Code Duplication | elseif ((bool)$this->attendee->CheckedIn && !(bool)self::config()->get('allow_checkout')) { |
|
91 | return $result = array( |
||
92 | 'Code' => self::MESSAGE_ALREADY_CHECKED_IN, |
||
93 | 'Message' => self::message(self::MESSAGE_ALREADY_CHECKED_IN, $name), |
||
94 | 'Type' => self::MESSAGE_TYPE_BAD, |
||
95 | 'Ticket' => $ticketCode, |
||
96 | 'Attendee' => $this->attendee |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | // Successfully checked out |
||
101 | View Code Duplication | elseif ((bool)$this->attendee->CheckedIn && (bool)self::config()->get('allow_checkout')) { |
|
102 | return $result = array( |
||
103 | 'Code' => self::MESSAGE_CHECK_OUT_SUCCESS, |
||
104 | 'Message' => self::message(self::MESSAGE_CHECK_OUT_SUCCESS, $name), |
||
105 | 'Type' => self::MESSAGE_TYPE_WARNING, |
||
106 | 'Ticket' => $ticketCode, |
||
107 | 'Attendee' => $this->attendee |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | // Successfully checked in |
||
112 | View Code Duplication | else { |
|
113 | return $result = array( |
||
114 | 'Code' => self::MESSAGE_CHECK_IN_SUCCESS, |
||
115 | 'Message' => self::message(self::MESSAGE_CHECK_IN_SUCCESS, $name), |
||
116 | 'Type' => self::MESSAGE_TYPE_GOOD, |
||
117 | 'Ticket' => $ticketCode, |
||
118 | 'Attendee' => $this->attendee |
||
119 | ); |
||
120 | } |
||
121 | } |
||
122 | |||
145 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.