Conditions | 25 |
Paths | 54 |
Total Lines | 122 |
Lines | 23 |
Ratio | 18.85 % |
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 |
||
83 | public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) { |
||
84 | $stored = TRUE; |
||
85 | if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) { |
||
86 | $transaction = \Drupal\Core\Database\Database::getConnection()->startTransaction(); |
||
87 | } else { |
||
88 | $transaction = db_transaction(); |
||
89 | } |
||
90 | |||
91 | // Get existing event data from db |
||
92 | $existing_events = $this->getEventData($event->getStartDate(), $event->getEndDate(), array($event->getUnitId())); |
||
93 | |||
94 | try { |
||
95 | // Itemize an event so we can save it |
||
96 | $itemized = $event->itemize(new EventItemizer($event, $granularity)); |
||
97 | |||
98 | //Write days |
||
99 | foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
||
100 | foreach ($months as $month => $days) { |
||
101 | if ($granularity === Event::BAT_HOURLY) { |
||
102 | foreach ($days as $day => $value) { |
||
103 | $this->itemizeSplitDay($existing_events, $itemized, $value, $event->getUnitId(), $year, $month, $day); |
||
104 | } |
||
105 | } |
||
106 | if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) { |
||
107 | \Drupal\Core\Database\Database::getConnection()->merge($this->day_table_no_prefix) |
||
108 | ->key(array( |
||
109 | 'unit_id' => $event->getUnitId(), |
||
110 | 'year' => $year, |
||
111 | 'month' => $month |
||
112 | )) |
||
113 | ->fields($days) |
||
114 | ->execute(); |
||
115 | } else { |
||
116 | db_merge($this->day_table_no_prefix) |
||
117 | ->key(array( |
||
118 | 'unit_id' => $event->getUnitId(), |
||
119 | 'year' => $year, |
||
120 | 'month' => $month |
||
121 | )) |
||
122 | ->fields($days) |
||
123 | ->execute(); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if (($granularity == Event::BAT_HOURLY) && isset($itemized[Event::BAT_HOUR])) { |
||
129 | // Write Hours |
||
130 | foreach ($itemized[Event::BAT_HOUR] as $year => $months) { |
||
131 | foreach ($months as $month => $days) { |
||
132 | foreach ($days as $day => $hours) { |
||
133 | // Count required as we may receive empty hours for granular events that start and end on midnight |
||
134 | if (count($hours) > 0) { |
||
135 | foreach ($hours as $hour => $value){ |
||
136 | $this->itemizeSplitHour($existing_events, $itemized, $value, $event->getUnitId(), $year, $month, $day, $hour); |
||
137 | } |
||
138 | if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) { |
||
139 | \Drupal\Core\Database\Database::getConnection()->merge($this->hour_table_no_prefix) |
||
140 | ->key(array( |
||
141 | 'unit_id' => $event->getUnitId(), |
||
142 | 'year' => $year, |
||
143 | 'month' => $month, |
||
144 | 'day' => substr($day, 1) |
||
145 | )) |
||
146 | ->fields($hours) |
||
147 | ->execute(); |
||
148 | View Code Duplication | } else { |
|
149 | db_merge($this->hour_table_no_prefix) |
||
150 | ->key(array( |
||
151 | 'unit_id' => $event->getUnitId(), |
||
152 | 'year' => $year, |
||
153 | 'month' => $month, |
||
154 | 'day' => substr($day, 1) |
||
155 | )) |
||
156 | ->fields($hours) |
||
157 | ->execute(); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // If we have minutes write minutes |
||
165 | foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { |
||
166 | foreach ($months as $month => $days) { |
||
167 | foreach ($days as $day => $hours) { |
||
168 | foreach ($hours as $hour => $minutes) { |
||
169 | if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) { |
||
170 | \Drupal\Core\Database\Database::getConnection()->merge($this->minute_table_no_prefix) |
||
171 | ->key(array( |
||
172 | 'unit_id' => $event->getUnitId(), |
||
173 | 'year' => $year, |
||
174 | 'month' => $month, |
||
175 | 'day' => substr($day, 1), |
||
176 | 'hour' => substr($hour, 1) |
||
177 | )) |
||
178 | ->fields($minutes) |
||
179 | ->execute(); |
||
180 | View Code Duplication | } else { |
|
181 | db_merge($this->minute_table_no_prefix) |
||
182 | ->key(array( |
||
183 | 'unit_id' => $event->getUnitId(), |
||
184 | 'year' => $year, |
||
185 | 'month' => $month, |
||
186 | 'day' => substr($day, 1), |
||
187 | 'hour' => substr($hour, 1) |
||
188 | )) |
||
189 | ->fields($minutes) |
||
190 | ->execute(); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | } catch (\Exception $e) { |
||
198 | $stored = FALSE; |
||
199 | $transaction->rollback(); |
||
200 | watchdog_exception('BAT Event Save Exception', $e); |
||
201 | } |
||
202 | |||
203 | return $stored; |
||
204 | } |
||
205 | |||
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.