Conditions | 32 |
Paths | 888 |
Total Lines | 199 |
Code Lines | 128 |
Lines | 141 |
Ratio | 70.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 |
||
130 | public function scheduleDateOccurrences($game) |
||
131 | { |
||
132 | $f = $game->getOccurrenceDrawFrequency(); |
||
133 | $today = new \DateTime("now"); |
||
134 | $end = new \DateTime("now"); |
||
135 | $interval = 'P10D'; |
||
136 | if ($game->getStartDate() && $game->getStartDate() > $today) { |
||
137 | $beginning = $game->getStartDate(); |
||
138 | } else { |
||
139 | $beginning = $today; |
||
140 | } |
||
141 | |||
142 | if ($game->getEndDate()) { |
||
143 | $end = $game->getEndDate(); |
||
144 | } else { |
||
145 | $end->add(new \DateInterval($interval)); |
||
146 | } |
||
147 | |||
148 | // Summertimes timezone management |
||
149 | $timezone = $today->getTimezone(); |
||
150 | $transitions = $timezone->getTransitions($beginning->getTimestamp(), $end->getTimestamp()); |
||
151 | |||
152 | // There is a time transition between these datetimes() |
||
153 | if (count($transitions) == 2) { |
||
154 | $shift = $transitions[0]['offset'] - $transitions[1]['offset']; |
||
155 | if ($shift > 0) { |
||
156 | $end->sub(new \DateInterval('PT'.abs($shift).'S')); |
||
157 | } else { |
||
158 | $end->add(new \DateInterval('PT'.abs($shift).'S')); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | // DateInterval takes the day @ 00:00 to calculate the difference between the dates, so 1 day is always missing |
||
163 | // as we consider the last day @ 23:59:59 in Playground :) |
||
164 | if ($end->format('His') == 0) { |
||
165 | $end->add(new \DateInterval('P1D')); |
||
166 | } |
||
167 | |||
168 | $dateInterval = (int)(($end->getTimestamp() - $beginning->getTimestamp())/60); |
||
169 | |||
170 | // Je recherche tous les IG non gagnés |
||
171 | $occurrences = $this->getInstantWinOccurrenceMapper()->findBy(array('instantwin' => $game)); |
||
172 | $nbExistingOccurrences = count($occurrences); |
||
173 | $nbOccurencesToCreate = 0; |
||
174 | |||
175 | switch ($f) { |
||
176 | case null: |
||
177 | case 'game': |
||
178 | $nbOccurencesToCreate = $game->getOccurrenceNumber() - $nbExistingOccurrences; |
||
179 | if ($nbOccurencesToCreate > 0) { |
||
180 | $this->createRandomOccurrences($game, $beginning, $end, $nbOccurencesToCreate); |
||
181 | } |
||
182 | |||
183 | break; |
||
184 | View Code Duplication | case 'hour': |
|
185 | $nbInterval = (int) ($dateInterval/60); |
||
186 | |||
187 | // If a hour don't last 60min, I consider it as a hour anyway. |
||
188 | if ($dateInterval%60 > 0) { |
||
189 | ++$nbInterval; |
||
190 | } |
||
191 | if ($nbInterval > 0) { |
||
192 | $nbOccurencesToCreate = $game->getOccurrenceNumber() - floor($nbExistingOccurrences/$nbInterval); |
||
193 | } |
||
194 | |||
195 | $beginningDrawDate = \DateTime::createFromFormat('m/d/Y H:i:s', $beginning->format('m/d/Y H:i:s')); |
||
196 | $endDrawDate = \DateTime::createFromFormat('m/d/Y H:i:s', $beginning->format('m/d/Y H'). ':59:59'); |
||
197 | |||
198 | if ($nbOccurencesToCreate > 0) { |
||
199 | for ($d=1; $d<=$nbInterval; $d++) { |
||
200 | $this->createRandomOccurrences( |
||
201 | $game, |
||
202 | $beginningDrawDate, |
||
203 | $endDrawDate, |
||
204 | $nbOccurencesToCreate |
||
205 | ); |
||
206 | $beginningDrawDate = \DateTime::createFromFormat( |
||
207 | 'm/d/Y H:i:s', |
||
208 | $beginningDrawDate->format('m/d/Y H'). ':00:00' |
||
209 | ); |
||
210 | $beginningDrawDate->add(new \DateInterval('PT1H')); |
||
211 | $endDrawDate->add(new \DateInterval('PT1H')); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | break; |
||
216 | View Code Duplication | case 'day': |
|
217 | $nbInterval = (int) ($dateInterval/(60*24)); |
||
218 | |||
219 | // Prise en compte des changements d'horaires |
||
220 | // If a day don't last 24h, I consider it as a day anyway |
||
221 | |||
222 | if ($dateInterval%(60*24) > 0) { |
||
223 | ++$nbInterval; |
||
224 | } |
||
225 | |||
226 | if ($nbInterval > 0) { |
||
227 | $nbOccurencesToCreate = $game->getOccurrenceNumber() - floor($nbExistingOccurrences/$nbInterval); |
||
228 | } |
||
229 | |||
230 | $beginningDrawDate = \DateTime::createFromFormat('m/d/Y H:i:s', $beginning->format('m/d/Y H:i:s')); |
||
231 | $endDrawDate = \DateTime::createFromFormat('m/d/Y H:i:s', $beginning->format('m/d/Y'). ' 23:59:59'); |
||
232 | |||
233 | if ($nbOccurencesToCreate > 0) { |
||
234 | for ($d=1; $d<=$nbInterval; $d++) { |
||
235 | $this->createRandomOccurrences( |
||
236 | $game, |
||
237 | $beginningDrawDate, |
||
238 | $endDrawDate, |
||
239 | $nbOccurencesToCreate |
||
240 | ); |
||
241 | // As the first beginning date was not @ midnight, |
||
242 | // I recreate the beginning date |
||
243 | $beginningDrawDate = \DateTime::createFromFormat( |
||
244 | 'm/d/Y H:i:s', |
||
245 | $beginningDrawDate->format('m/d/Y'). ' 00:00:00' |
||
246 | ); |
||
247 | $beginningDrawDate->add(new \DateInterval('P1D')); |
||
248 | $endDrawDate->add(new \DateInterval('P1D')); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | break; |
||
253 | View Code Duplication | case 'week': |
|
254 | $nbOccurencesToCreate = $game->getOccurrenceNumber() - $nbExistingOccurrences; |
||
255 | $nbWeeksInterval = (int) ($dateInterval/(60*24*7)); |
||
256 | // If a week don't last 7d, I consider it as a week anyway. |
||
257 | if ($dateInterval%(60*24*7) > 0) { |
||
258 | ++$nbWeeksInterval; |
||
259 | } |
||
260 | $beginningDrawDate = \DateTime::createFromFormat( |
||
261 | 'm/d/Y H:i:s', |
||
262 | $beginning->format('m/d/Y'). ' 00:00:00' |
||
263 | ); |
||
264 | $endDrawDate = \DateTime::createFromFormat( |
||
265 | 'm/d/Y H:i:s', |
||
266 | $beginning->format('m/d/Y'). ' 23:59:59' |
||
267 | ); |
||
268 | $endDrawDate->add(new \DateInterval('P6D')); |
||
269 | if ($endDrawDate > $end) { |
||
270 | $endDrawDate = $end; |
||
271 | } |
||
272 | |||
273 | if ($nbOccurencesToCreate > 0) { |
||
274 | for ($d=1; $d<=$nbWeeksInterval; $d++) { |
||
275 | $this->createRandomOccurrences( |
||
276 | $game, |
||
277 | $beginningDrawDate, |
||
278 | $endDrawDate, |
||
279 | $nbOccurencesToCreate |
||
280 | ); |
||
281 | $beginningDrawDate->add(new \DateInterval('P1W')); |
||
282 | $endDrawDate->add(new \DateInterval('P1W')); |
||
283 | if ($endDrawDate > $end) { |
||
284 | $endDrawDate = $end; |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | break; |
||
290 | View Code Duplication | case 'month': |
|
291 | $nbOccurencesToCreate = $game->getOccurrenceNumber() - $nbExistingOccurrences; |
||
292 | $nbMonthsInterval = (int) ($dateInterval/(60*24*30)); |
||
293 | // If a week don't last 30d, I consider it as a month anyway. |
||
294 | if ($dateInterval%(60*24*30) > 0) { |
||
295 | ++$nbMonthsInterval; |
||
296 | } |
||
297 | $beginningDrawDate = \DateTime::createFromFormat( |
||
298 | 'm/d/Y H:i:s', |
||
299 | $beginning->format('m/d/Y'). ' 00:00:00' |
||
300 | ); |
||
301 | $endDrawDate = \DateTime::createFromFormat('m/d/Y H:i:s', $beginning->format('m/d/Y'). ' 23:59:59'); |
||
302 | $endDrawDate->add(new \DateInterval('P1M')); |
||
303 | $endDrawDate->sub(new \DateInterval('P1D')); |
||
304 | if ($endDrawDate > $end) { |
||
305 | $endDrawDate = $end; |
||
306 | } |
||
307 | |||
308 | if ($nbOccurencesToCreate > 0) { |
||
309 | for ($d=1; $d<=$nbMonthsInterval; $d++) { |
||
310 | $this->createRandomOccurrences( |
||
311 | $game, |
||
312 | $beginningDrawDate, |
||
313 | $endDrawDate, |
||
314 | $nbOccurencesToCreate |
||
315 | ); |
||
316 | $beginningDrawDate->add(new \DateInterval('P1M')); |
||
317 | $endDrawDate->add(new \DateInterval('P1M')); |
||
318 | if ($endDrawDate > $end) { |
||
319 | $endDrawDate = $end; |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | |||
324 | break; |
||
325 | } |
||
326 | |||
327 | return true; |
||
328 | } |
||
329 | |||
622 |
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.