Conditions | 22 |
Paths | 5760 |
Total Lines | 70 |
Lines | 11 |
Ratio | 15.71 % |
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 |
||
106 | public function toString() { |
||
107 | $text = ''; |
||
108 | |||
109 | // Min/max stay length constraint variables. |
||
110 | $minimum_stay = empty($this->min_days) ? '' : (($this->min_days == 1) ? $this->min_days . ' day' : $this->min_days . ' days'); |
||
111 | $maximum_stay = empty($this->max_days) ? '' : (($this->max_days == 1) ? $this->max_days . ' day' : $this->max_days . ' days'); |
||
112 | |||
113 | // Day of the week constraint variable. |
||
114 | $day_of_the_week = $this->getWeekDay($this->checkin_day); |
||
115 | |||
116 | $start_date = FALSE; |
||
117 | $end_date = FALSE; |
||
118 | |||
119 | // Date range constraint variables. |
||
120 | View Code Duplication | if ($this->start_date !== NULL && $this->start_date != (new \DateTime('1970-01-01'))) { |
|
|
|||
121 | $start_date = $this->start_date->format('Y-m-d'); |
||
122 | } |
||
123 | View Code Duplication | if ($this->end_date !== NULL && $this->end_date != (new \DateTime('2999-12-31'))) { |
|
124 | $end_date = $this->end_date->format('Y-m-d'); |
||
125 | } |
||
126 | |||
127 | // Next create replacement placeholders to be used in t() below. |
||
128 | $args = array( |
||
129 | '@minimum_stay' => $minimum_stay, |
||
130 | '@maximum_stay' => $maximum_stay, |
||
131 | '@day_of_the_week' => $day_of_the_week, |
||
132 | ); |
||
133 | |||
134 | // Finally, build out the constraint text string adding components |
||
135 | // as necessary. |
||
136 | |||
137 | // Specify a date range constraint. |
||
138 | View Code Duplication | if ($start_date && $end_date) { |
|
139 | $text = 'From @start_date to @end_date'; |
||
140 | $args['@start_date'] = $start_date; |
||
141 | $args['@end_date'] = $end_date; |
||
142 | } |
||
143 | |||
144 | // Specify the day of the week constraint. |
||
145 | if ($day_of_the_week) { |
||
146 | if ($start_date && $end_date) { |
||
147 | $text = 'From @start_date to @end_date, if booking starts on @day_of_the_week'; |
||
148 | } else { |
||
149 | $text = 'If booking starts on @day_of_the_week'; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // Specify the min/max stay length constraint. |
||
154 | if ($minimum_stay || $maximum_stay) { |
||
155 | if (empty($text)) { |
||
156 | $text = 'The stay '; |
||
157 | } else { |
||
158 | $text .= ' the stay '; |
||
159 | } |
||
160 | } |
||
161 | if ($minimum_stay && $maximum_stay) { |
||
162 | // Special case when min stay and max stay are the same. |
||
163 | if ($minimum_stay == $maximum_stay) { |
||
164 | $text .= 'must be for @minimum_stay'; |
||
165 | } else { |
||
166 | $text .= 'must be at least @minimum_stay and at most @maximum_stay'; |
||
167 | } |
||
168 | } elseif ($minimum_stay) { |
||
169 | $text .= 'must be for at least @minimum_stay'; |
||
170 | } elseif ($maximum_stay) { |
||
171 | $text .= 'cannot be more than @maximum_stay'; |
||
172 | } |
||
173 | |||
174 | return array('text' => $text, 'args' => $args); |
||
175 | } |
||
176 | |||
238 |
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.