Conditions | 11 |
Paths | 108 |
Total Lines | 47 |
Lines | 14 |
Ratio | 29.79 % |
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 |
||
29 | public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
||
30 | |||
31 | $queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
||
32 | |||
33 | $results = array(); |
||
34 | // Run each query and store results |
||
35 | foreach ($queries as $type => $query) { |
||
36 | if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) { |
||
37 | $results[$type] = \Drupal\Core\Database\Database::getConnection()->query($query); |
||
38 | } else { |
||
39 | $results[$type] = db_query($query); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | $db_events = array(); |
||
44 | |||
45 | // Cycle through day results and setup an event array |
||
46 | while( $data = $results[Event::BAT_DAY]->fetchAssoc()) { |
||
47 | // Figure out how many days the current month has |
||
48 | $temp_date = new \DateTime($data['year'] . "-" . $data['month']); |
||
49 | $days_in_month = (int)$temp_date->format('t'); |
||
50 | View Code Duplication | for ($i = 1; $i<=$days_in_month; $i++) { |
|
|
|||
51 | $db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i]; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | // With the day events taken care off let's cycle through hours |
||
56 | while( $data = $results[Event::BAT_HOUR]->fetchAssoc()) { |
||
57 | View Code Duplication | for ($i = 0; $i<=23; $i++) { |
|
58 | $db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']]['d' . $data['day']]['h'. $i] = $data['h'.$i]; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | // With the hour events taken care off let's cycle through minutes |
||
63 | while( $data = $results[Event::BAT_MINUTE]->fetchAssoc()) { |
||
64 | View Code Duplication | for ($i = 0; $i<=59; $i++) { |
|
65 | if ($i <= 9) { |
||
66 | $index = 'm0'.$i; |
||
67 | } else { |
||
68 | $index = 'm'.$i; |
||
69 | } |
||
70 | $db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']]['d' . $data['day']]['h' . $data['hour']][$index] = $data[$index]; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | return $db_events; |
||
75 | } |
||
76 | |||
207 |
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.