Conditions | 19 |
Paths | 25 |
Total Lines | 86 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
86 | public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { |
||
87 | $stored = TRUE; |
||
88 | |||
89 | // Get existing event data from db |
||
90 | $existing_events = $this->getEventData($event->getStartDate(), $event->getEndDate(), array($event->getUnitId())); |
||
91 | |||
92 | |||
93 | try { |
||
94 | // Itemize an event so we can save it |
||
95 | $itemized = $event->itemizeEvent($granularity); |
||
96 | |||
97 | // Write days |
||
98 | foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
||
99 | foreach ($months as $month => $days) { |
||
100 | $values = array_values($days); |
||
101 | $keys = array_keys($days); |
||
102 | // Because SQLite does not have a nice merge first we have to check if a row exists to determine whether to do an insert or an update |
||
103 | if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month])) { |
||
104 | $command = "UPDATE $this->day_table SET "; |
||
105 | foreach ($days as $day => $value){ |
||
106 | $command .= "$day = $value,"; |
||
107 | } |
||
108 | $command = rtrim($command, ','); |
||
109 | $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month"; |
||
110 | $this->pdo->exec($command); |
||
111 | } else { |
||
112 | $this->pdo->exec("INSERT INTO $this->day_table (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . implode(', ', $values) . ")"); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | if ($granularity == Event::BAT_HOURLY) { |
||
118 | // Write Hours |
||
119 | foreach ($itemized[Event::BAT_HOUR] as $year => $months) { |
||
120 | foreach ($months as $month => $days) { |
||
121 | foreach ($days as $day => $hours) { |
||
122 | // Count required as we may receive empty hours for granular events that start and end on midnight |
||
123 | if (count($hours) > 0) { |
||
124 | $values = array_values($hours); |
||
125 | $keys = array_keys($hours); |
||
126 | if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month][$day])) { |
||
127 | $command = "UPDATE $this->hour_table SET "; |
||
128 | foreach ($hours as $hour => $value){ |
||
129 | $command .= "$hour = $value,"; |
||
130 | } |
||
131 | $command = rtrim($command, ','); |
||
132 | $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month AND day = ". substr($day,1); |
||
133 | $this->pdo->exec($command); |
||
134 | } else { |
||
135 | $this->pdo->exec("INSERT INTO $this->hour_table (unit_id, year, month, day, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . implode(', ', $values) . ")"); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | // If we have minutes write minutes |
||
143 | foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { |
||
144 | foreach ($months as $month => $days) { |
||
145 | foreach ($days as $day => $hours) { |
||
146 | foreach ($hours as $hour => $minutes) { |
||
147 | $values = array_values($minutes); |
||
148 | $keys = array_keys($minutes); |
||
149 | if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month][$day][$hour])) { |
||
150 | $command = "UPDATE $this->minute_table SET "; |
||
151 | foreach ($minutes as $minute => $value){ |
||
152 | $command .= "$minute = $value,"; |
||
153 | } |
||
154 | $command = rtrim($command, ','); |
||
155 | $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month AND day = ". substr($day,1) . " AND hour = " . substr($hour,1); |
||
156 | $this->pdo->exec($command); |
||
157 | } else { |
||
158 | $this->pdo->exec("INSERT INTO $this->minute_table (unit_id, year, month, day, hour, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . substr($hour, 1) . ", " . implode(', ', $values) . ")"); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | catch (\Exception $e) { |
||
167 | $stored = FALSE; |
||
168 | } |
||
169 | |||
170 | return $stored; |
||
171 | } |
||
172 | |||
174 |
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.