Conditions | 8 |
Paths | 45 |
Total Lines | 77 |
Code Lines | 45 |
Lines | 14 |
Ratio | 18.18 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
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 |
||
169 | public function readHistorySummary(array $post) |
||
170 | { |
||
171 | $redisKey = "{$this->getCacheNamespace()}:{$this->getType()}:History"; |
||
172 | $redisKey = $this->appendRedisKey($post, $redisKey); |
||
173 | $post = $this->processPostVars($post); |
||
174 | |||
175 | $this->getLogDriver()->addDebug($redisKey); |
||
176 | |||
177 | $queryObject = new QueryObject; |
||
178 | $queryObject = $this->setupQueryObject($queryObject, $post); |
||
179 | $queryObject->addSelect('FROM_UNIXTIME(ResultEndTime) AS ResultEndTime'); |
||
180 | $queryObject->addSelect('ResultWinner'); |
||
181 | $queryObject->addWhere([ |
||
182 | 'col' => 'InProgress', |
||
183 | 'value' => 0 |
||
184 | ]); |
||
185 | $queryObject->setLimit('unlimited'); |
||
186 | |||
187 | $minDate = '2014-10-29'; // Beginning of tracking |
||
188 | $maxDate = date('Y-m-d'); // Today unless set |
||
189 | |||
190 | // If there is a minimum date set |
||
191 | View Code Duplication | if (! empty($post['wheres']['morethan']['ResultEndTime'])) { |
|
192 | if (is_integer($post['wheres']['morethan']['ResultEndTime'])) { |
||
193 | $minDate = date('Y-m-d', $post['wheres']['morethan']['ResultEndTime']); |
||
194 | } else { |
||
195 | $minDate = date('Y-m-d', strtotime($post['wheres']['morethan']['ResultEndTime'])); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | // If there is a maximum date set |
||
200 | View Code Duplication | if (! empty($post['wheres']['lessthan']['ResultEndTime'])) { |
|
201 | if (is_integer($post['wheres']['lessthan']['ResultEndTime'])) { |
||
202 | $maxDate = date('Y-m-d', $post['wheres']['lessthan']['ResultEndTime']); |
||
203 | } else { |
||
204 | $maxDate = date('Y-m-d', strtotime($post['wheres']['lessthan']['ResultEndTime'])); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | $redisKey .= "/min-{$minDate}/max-{$maxDate}"; |
||
209 | |||
210 | if ($this->checkRedis($redisKey)) { |
||
211 | $this->getLogDriver()->addDebug("CACHE PULL"); |
||
212 | return $this->getFromRedis($redisKey); |
||
213 | } |
||
214 | |||
215 | // Get the data to parse |
||
216 | $alerts = $this->repository->read($queryObject); |
||
217 | |||
218 | // Generate the range of dates |
||
219 | $dates = $this->dataFormatter->createDateRangeArray($minDate, $maxDate); |
||
220 | $dateRange = []; |
||
221 | |||
222 | // Generates the victory totals for each date |
||
223 | foreach ($dates as $date) { |
||
224 | $dateRange[$date] = [ |
||
225 | 'vs' => 0, |
||
226 | 'nc' => 0, |
||
227 | 'tr' => 0, |
||
228 | 'draw' => 0 |
||
229 | ]; |
||
230 | } |
||
231 | |||
232 | // Calculate metrics |
||
233 | foreach ($alerts as $alert) { |
||
234 | $date = date('Y-m-d', strtotime($alert['ResultEndTime'])); |
||
235 | $winner = strtolower($alert['ResultWinner']); |
||
236 | |||
237 | $dateRange[$date][$winner]++; |
||
238 | } |
||
239 | |||
240 | // Commit to Redis |
||
241 | return $this->cacheAndReturn( |
||
242 | $dateRange, |
||
243 | $redisKey |
||
244 | ); |
||
245 | } |
||
246 | } |
||
247 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.