Conditions | 22 |
Paths | 5762 |
Total Lines | 123 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 defined('SYSPATH') or die('No direct access allowed.'); |
||
129 | public function notify($data) |
||
130 | { |
||
131 | // Split the date and current status |
||
132 | list($month, $day, $year, $week, $current) = $data; |
||
133 | |||
134 | // Get a timestamp for the day |
||
135 | $timestamp = mktime(0, 0, 0, $month, $day, $year); |
||
136 | |||
137 | // Date conditionals |
||
138 | $condition = array( |
||
139 | 'timestamp' => (int) $timestamp, |
||
140 | 'day' => (int) date('j', $timestamp), |
||
141 | 'week' => (int) $week, |
||
142 | 'month' => (int) date('n', $timestamp), |
||
143 | 'year' => (int) date('Y', $timestamp), |
||
144 | 'day_of_week' => (int) date('w', $timestamp), |
||
145 | 'current' => (bool) $current, |
||
146 | ); |
||
147 | |||
148 | // Tested conditions |
||
149 | $tested = array(); |
||
150 | |||
151 | foreach ($condition as $key => $value) { |
||
152 | // Timestamps need to be handled carefully |
||
153 | if ($key === 'timestamp' and isset($this->conditions['timestamp'])) { |
||
154 | // This adds 23 hours, 59 minutes and 59 seconds to today's timestamp, as 24 hours |
||
155 | // is classed as a new day |
||
156 | $next_day = $timestamp + 86399; |
||
157 | |||
158 | if ($this->conditions['timestamp'] < $timestamp or $this->conditions['timestamp'] > $next_day) { |
||
159 | return false; |
||
160 | } |
||
161 | } |
||
162 | // Test basic conditions first |
||
163 | elseif (isset($this->conditions[$key]) and $this->conditions[$key] !== $value) { |
||
164 | return false; |
||
165 | } |
||
166 | |||
167 | // Condition has been tested |
||
168 | $tested[$key] = true; |
||
169 | } |
||
170 | |||
171 | if (isset($this->conditions['weekend'])) { |
||
172 | // Weekday vs Weekend |
||
173 | $condition['weekend'] = ($condition['day_of_week'] === 0 or $condition['day_of_week'] === 6); |
||
174 | } |
||
175 | |||
176 | if (isset($this->conditions['first_day'])) { |
||
177 | // First day of month |
||
178 | $condition['first_day'] = ($condition['day'] === 1); |
||
179 | } |
||
180 | |||
181 | if (isset($this->conditions['last_day'])) { |
||
182 | // Last day of month |
||
183 | $condition['last_day'] = ($condition['day'] === (int) date('t', $timestamp)); |
||
184 | } |
||
185 | |||
186 | if (isset($this->conditions['occurrence'])) { |
||
187 | // Get the occurance of the current day |
||
188 | $condition['occurrence'] = $this->day_occurrence($timestamp); |
||
189 | } |
||
190 | |||
191 | if (isset($this->conditions['last_occurrence'])) { |
||
192 | // Test if the next occurance of this date is next month |
||
193 | $condition['last_occurrence'] = ((int) date('n', $timestamp + 604800) !== $condition['month']); |
||
194 | } |
||
195 | |||
196 | if (isset($this->conditions['easter'])) { |
||
197 | if ($condition['month'] === 3 or $condition['month'] === 4) { |
||
198 | // This algorithm is from Practical Astronomy With Your Calculator, 2nd Edition by Peter |
||
199 | // Duffett-Smith. It was originally from Butcher's Ecclesiastical Calendar, published in |
||
200 | // 1876. This algorithm has also been published in the 1922 book General Astronomy by |
||
201 | // Spencer Jones; in The Journal of the British Astronomical Association (Vol.88, page |
||
202 | // 91, December 1977); and in Astronomical Algorithms (1991) by Jean Meeus. |
||
203 | |||
204 | $a = $condition['year'] % 19; |
||
|
|||
205 | $b = (int) ($condition['year'] / 100); |
||
206 | $c = $condition['year'] % 100; |
||
207 | $d = (int) ($b / 4); |
||
208 | $e = $b % 4; |
||
209 | $f = (int) (($b + 8) / 25); |
||
210 | $g = (int) (($b - $f + 1) / 3); |
||
211 | $h = (19 * $a + $b - $d - $g + 15) % 30; |
||
212 | $i = (int) ($c / 4); |
||
213 | $k = $c % 4; |
||
214 | $l = (32 + 2 * $e + 2 * $i - $h - $k) % 7; |
||
215 | $m = (int) (($a + 11 * $h + 22 * $l) / 451); |
||
216 | $p = ($h + $l - 7 * $m + 114) % 31; |
||
217 | |||
218 | $month = (int) (($h + $l - 7 * $m + 114) / 31); |
||
219 | $day = $p + 1; |
||
220 | |||
221 | $condition['easter'] = ($condition['month'] === $month and $condition['day'] === $day); |
||
222 | } else { |
||
223 | // Easter can only happen in March or April |
||
224 | $condition['easter'] = false; |
||
225 | } |
||
226 | } |
||
227 | |||
228 | if (isset($this->conditions['callback'])) { |
||
229 | // Use a callback to determine validity |
||
230 | $condition['callback'] = call_user_func($this->conditions['callback'], $condition, $this); |
||
231 | } |
||
232 | |||
233 | $conditions = array_diff_key($this->conditions, $tested); |
||
234 | |||
235 | foreach ($conditions as $key => $value) { |
||
236 | if ($key === 'callback') { |
||
237 | // Callbacks are tested on a TRUE/FALSE basis |
||
238 | $value = true; |
||
239 | } |
||
240 | |||
241 | // Test advanced conditions |
||
242 | if ($condition[$key] !== $value) { |
||
243 | return false; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | $this->caller->add_data(array( |
||
248 | 'classes' => $this->classes, |
||
249 | 'output' => $this->output, |
||
250 | )); |
||
251 | } |
||
252 | |||
285 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.