Conditions | 46 |
Paths | 1057 |
Total Lines | 179 |
Code Lines | 90 |
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 |
||
83 | public function convert() : string |
||
84 | { |
||
85 | if(!isset($this->dateFrom)) |
||
86 | { |
||
87 | throw new ConvertHelper_Exception( |
||
88 | 'No date from has been specified.', |
||
89 | null, |
||
90 | self::ERROR_NO_DATE_FROM_SET |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | // no date to set? Assume we want to use today. |
||
95 | if(!isset($this->dateTo)) |
||
96 | { |
||
97 | $this->dateTo = time(); |
||
98 | } |
||
99 | |||
100 | // Calculate the difference in seconds betweeen |
||
101 | // the two timestamps |
||
102 | |||
103 | $difference = $this->dateTo - $this->dateFrom; |
||
104 | |||
105 | $interval = ""; |
||
106 | |||
107 | $future = false; |
||
108 | if($difference < 0) |
||
109 | { |
||
110 | $difference = $difference * -1; |
||
111 | $future = true; |
||
112 | } |
||
113 | |||
114 | // If difference is less than 60 seconds, |
||
115 | // seconds is a good interval of choice |
||
116 | |||
117 | if ($difference < 60) { |
||
118 | $interval = "s"; |
||
119 | } |
||
120 | |||
121 | // If difference is between 60 seconds and |
||
122 | // 60 minutes, minutes is a good interval |
||
123 | elseif ($difference >= 60 && $difference < 60 * 60) { |
||
124 | $interval = "n"; |
||
125 | } |
||
126 | |||
127 | // If difference is between 1 hour and 24 hours |
||
128 | // hours is a good interval |
||
129 | elseif ($difference >= 60 * 60 && $difference < 60 * 60 * 24) { |
||
130 | $interval = "h"; |
||
131 | } |
||
132 | |||
133 | // If difference is between 1 day and 7 days |
||
134 | // days is a good interval |
||
135 | elseif ($difference >= 60 * 60 * 24 && $difference < 60 * 60 * 24 * 7) { |
||
136 | $interval = "d"; |
||
137 | } |
||
138 | |||
139 | // If difference is between 1 week and 30 days |
||
140 | // weeks is a good interval |
||
141 | elseif ($difference >= 60 * 60 * 24 * 7 && $difference < 60 * 60 * 24 * 30) { |
||
142 | $interval = "ww"; |
||
143 | } |
||
144 | |||
145 | // If difference is between 30 days and 365 days |
||
146 | // months is a good interval, again, the same thing |
||
147 | // applies, if the 29th February happens to exist |
||
148 | // between your 2 dates, the function will return |
||
149 | // the 'incorrect' value for a day |
||
150 | elseif ($difference >= 60 * 60 * 24 * 30 && $difference < 60 * 60 * 24 * 365) { |
||
151 | $interval = "m"; |
||
152 | } |
||
153 | |||
154 | // If difference is greater than or equal to 365 |
||
155 | // days, return year. This will be incorrect if |
||
156 | // for example, you call the function on the 28th April |
||
157 | // 2008 passing in 29th April 2007. It will return |
||
158 | // 1 year ago when in actual fact (yawn!) not quite |
||
159 | // a year has gone by |
||
160 | elseif ($difference >= 60 * 60 * 24 * 365) { |
||
161 | $interval = "y"; |
||
162 | } |
||
163 | |||
164 | $result = ''; |
||
165 | |||
166 | // Based on the interval, determine the |
||
167 | // number of units between the two dates |
||
168 | // From this point on, you would be hard |
||
169 | // pushed telling the difference between |
||
170 | // this function and DateDiff. If the $datediff |
||
171 | // returned is 1, be sure to return the singular |
||
172 | // of the unit, e.g. 'day' rather 'days' |
||
173 | switch ($interval) |
||
174 | { |
||
175 | case "m": |
||
176 | $months_difference = (int)floor($difference / 60 / 60 / 24 / 29); |
||
177 | $hour = (int)date("H", $this->dateFrom); |
||
178 | $min = (int)date("i", $this->dateFrom); |
||
179 | $sec = (int)date("s", $this->dateFrom); |
||
180 | $month = (int)date("n", $this->dateFrom); |
||
181 | $day = (int)date("j", $this->dateTo); |
||
182 | $year = (int)date("Y", $this->dateFrom); |
||
183 | |||
184 | while(mktime($hour, $min, $sec, $month + ($months_difference), $day, $year) < $this->dateTo) |
||
185 | { |
||
186 | $months_difference++; |
||
187 | } |
||
188 | |||
189 | $datediff = $months_difference; |
||
190 | |||
191 | // We need this in here because it is possible |
||
192 | // to have an 'm' interval and a months |
||
193 | // difference of 12 because we are using 29 days |
||
194 | // in a month |
||
195 | if ($datediff == 12) { |
||
196 | $datediff--; |
||
197 | } |
||
198 | |||
199 | if($future) { |
||
200 | $result = ($datediff == 1) ? t('In one month', $datediff) : t('In %1s months', $datediff); |
||
201 | } else { |
||
202 | $result = ($datediff == 1) ? t('One month ago', $datediff) : t('%1s months ago', $datediff); |
||
203 | } |
||
204 | break; |
||
205 | |||
206 | case "y": |
||
207 | $datediff = floor($difference / 60 / 60 / 24 / 365); |
||
208 | if($future) { |
||
209 | $result = ($datediff == 1) ? t('In one year', $datediff) : t('In %1s years', $datediff); |
||
210 | } else { |
||
211 | $result = ($datediff == 1) ? t('One year ago', $datediff) : t('%1s years ago', $datediff); |
||
212 | } |
||
213 | break; |
||
214 | |||
215 | case "d": |
||
216 | $datediff = floor($difference / 60 / 60 / 24); |
||
217 | if($future) { |
||
218 | $result = ($datediff == 1) ? t('In one day', $datediff) : t('In %1s days', $datediff); |
||
219 | } else { |
||
220 | $result = ($datediff == 1) ? t('One day ago', $datediff) : t('%1s days ago', $datediff); |
||
221 | } |
||
222 | break; |
||
223 | |||
224 | case "ww": |
||
225 | $datediff = floor($difference / 60 / 60 / 24 / 7); |
||
226 | if($future) { |
||
227 | $result = ($datediff == 1) ? t('In one week', $datediff) : t('In %1s weeks', $datediff); |
||
228 | } else { |
||
229 | $result = ($datediff == 1) ? t('One week ago', $datediff) : t('%1s weeks ago', $datediff); |
||
230 | } |
||
231 | break; |
||
232 | |||
233 | case "h": |
||
234 | $datediff = floor($difference / 60 / 60); |
||
235 | if($future) { |
||
236 | $result = ($datediff == 1) ? t('In one hour', $datediff) : t('In %1s hours', $datediff); |
||
237 | } else { |
||
238 | $result = ($datediff == 1) ? t('One hour ago', $datediff) : t('%1s hours ago', $datediff); |
||
239 | } |
||
240 | break; |
||
241 | |||
242 | case "n": |
||
243 | $datediff = floor($difference / 60); |
||
244 | if($future) { |
||
245 | $result = ($datediff == 1) ? t('In one minute', $datediff) : t('In %1s minutes', $datediff); |
||
246 | } else { |
||
247 | $result = ($datediff == 1) ? t('One minute ago', $datediff) : t('%1s minutes ago', $datediff); |
||
248 | } |
||
249 | break; |
||
250 | |||
251 | case "s": |
||
252 | $datediff = $difference; |
||
253 | if($future) { |
||
254 | $result = ($datediff == 1) ? t('In one second', $datediff) : t('In %1s seconds', $datediff); |
||
255 | } else { |
||
256 | $result = ($datediff == 1) ? t('One second ago', $datediff) : t('%1s seconds ago', $datediff); |
||
257 | } |
||
258 | break; |
||
259 | } |
||
260 | |||
261 | return $result; |
||
262 | } |
||
264 |