Conditions | 7 |
Paths | 9 |
Total Lines | 52 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
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 |
||
37 | public function events( |
||
38 | \DateTime $start, |
||
39 | \DateTimeInterface $end, |
||
40 | string $rangeInput, |
||
41 | array $eventsInput |
||
42 | ): Events |
||
43 | { |
||
44 | if (empty($eventsInput)) { |
||
45 | throw new EmptyExtractEventsException(); |
||
46 | } |
||
47 | |||
48 | $start = $this->converter->immutable($start); |
||
49 | $end = $this->converter->immutable($end); |
||
50 | |||
51 | //Swap start and end dates if incorrect |
||
52 | if ($start > $end) { |
||
53 | list($start, $end) = [$end, $start]; |
||
54 | } |
||
55 | |||
56 | $range = new Range($rangeInput); |
||
57 | $events = new Events($eventsInput); |
||
58 | |||
59 | /** @var \DateTime $iteratedDatetime */ |
||
60 | $iteratedDatetime = clone $start; |
||
61 | |||
62 | //do-while allows to add events of last interval occurrence. |
||
63 | do { |
||
64 | $row = $events->addRow($iteratedDatetime->format($range->getFormat())); |
||
65 | |||
66 | $datetime = $this->converter->convert($iteratedDatetime, $rangeInput); |
||
67 | |||
68 | foreach ($this->source->findByGroupedInterval( |
||
69 | $range->getField(), |
||
70 | $datetime, |
||
71 | $start, |
||
72 | $end, |
||
73 | $eventsInput |
||
74 | ) as $occurrence) { |
||
75 | foreach ($occurrence->events as $event) { |
||
76 | if (!in_array($event->name, $eventsInput)) { |
||
77 | continue; |
||
78 | } |
||
79 | |||
80 | $row->addEvent($event->name, $event->value); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $iteratedDatetime = $iteratedDatetime->add($range->getInterval()); |
||
85 | } while ($iteratedDatetime <= $end); |
||
86 | |||
87 | return $events; |
||
88 | } |
||
89 | } |