Conditions | 27 |
Paths | 9600 |
Total Lines | 143 |
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 |
||
19 | public function createFromCdbCalendar(\CultureFeed_Cdb_Data_Calendar $cdbCalendar) |
||
20 | { |
||
21 | // |
||
22 | // Get the start day. |
||
23 | // |
||
24 | $cdbCalendar->rewind(); |
||
25 | $startDateString = ''; |
||
26 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
27 | /** @var \CultureFeed_Cdb_Data_Calendar_Period $period */ |
||
28 | $period = $cdbCalendar->current(); |
||
29 | $startDateString = $period->getDateFrom() . 'T00:00:00'; |
||
30 | } elseif ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
31 | $firstTimestamp = $cdbCalendar->current(); |
||
32 | $cdbCalendarAsArray = iterator_to_array($cdbCalendar); |
||
33 | $timestamp = $this->getFirstTimestamp($cdbCalendarAsArray, $firstTimestamp); |
||
34 | if ($timestamp->getStartTime()) { |
||
35 | $startDateString = $timestamp->getDate() . 'T' . substr($timestamp->getStartTime(), 0, 5) . ':00'; |
||
36 | } else { |
||
37 | $startDateString = $timestamp->getDate() . 'T00:00:00'; |
||
38 | } |
||
39 | } |
||
40 | $startDate = !empty($startDateString) ? DateTimeFactory::dateTimeFromDateString($startDateString) : null; |
||
41 | |||
42 | // |
||
43 | // Get the end day. |
||
44 | // |
||
45 | $cdbCalendar->rewind(); |
||
46 | $endDateString = ''; |
||
47 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
48 | /** @var \CultureFeed_Cdb_Data_Calendar_Period $period */ |
||
49 | $period = $cdbCalendar->current(); |
||
50 | $endDateString = $period->getDateTo() . 'T00:00:00'; |
||
51 | } elseif ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
52 | $firstTimestamp = $cdbCalendar->current(); |
||
53 | /** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */ |
||
54 | $cdbCalendarAsArray = iterator_to_array($cdbCalendar); |
||
55 | $timestamp = $this->getLastTimestamp($cdbCalendarAsArray, $firstTimestamp); |
||
56 | if ($timestamp->getEndTime()) { |
||
57 | $endDateString = $timestamp->getDate() . 'T' . $timestamp->getEndTime(); |
||
58 | } else { |
||
59 | $endTime = $timestamp->getStartTime() ? $timestamp->getStartTime() : '00:00:00'; |
||
60 | $endDateString = $timestamp->getDate() . 'T' . $endTime; |
||
61 | } |
||
62 | } |
||
63 | $endDate = !empty($endDateString) ? DateTimeFactory::dateTimeFromDateString($endDateString) : null; |
||
64 | |||
65 | // |
||
66 | // Get the time stamps. |
||
67 | // |
||
68 | $cdbCalendar->rewind(); |
||
69 | $timestamps = []; |
||
70 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
71 | $splitPeriods = []; |
||
72 | while ($cdbCalendar->valid()) { |
||
73 | /** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */ |
||
74 | $timestamp = $cdbCalendar->current(); |
||
75 | $cdbCalendar->next(); |
||
76 | |||
77 | $startTime = $timestamp->getStartTime() ? $timestamp->getStartTime() : '00:00:00'; |
||
78 | $startDateString = $timestamp->getDate() . 'T' . $startTime; |
||
79 | |||
80 | if ($timestamp->getEndTime()) { |
||
81 | $endDateString = $timestamp->getDate() . 'T' . $timestamp->getEndTime(); |
||
82 | } else { |
||
83 | $endDateString = $timestamp->getDate() . 'T' . $startTime; |
||
84 | } |
||
85 | |||
86 | $timestamp = $this->createTimestamp( |
||
87 | $startDateString, |
||
88 | $endDateString |
||
89 | ); |
||
90 | |||
91 | $index = intval($timestamp->getStartDate()->format('s')); |
||
92 | if ($index > 0) { |
||
93 | $splitPeriods[$index][] = $timestamp; |
||
94 | } else { |
||
95 | $timestamps[] = $timestamp; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $periods = array_map( |
||
100 | function (array $periodParts) { |
||
101 | $firstPart = array_shift($periodParts); |
||
102 | $lastPart = array_pop($periodParts); |
||
103 | return new Timestamp( |
||
104 | Chronos::instance($firstPart->getStartDate())->second(0), |
||
105 | $lastPart ? $lastPart->getEndDate() : $firstPart->getEndDate() |
||
106 | ); |
||
107 | }, |
||
108 | $splitPeriods |
||
109 | ); |
||
110 | |||
111 | $timestamps = array_merge($timestamps, $periods); |
||
112 | } |
||
113 | |||
114 | // |
||
115 | // Get the opening hours. |
||
116 | // |
||
117 | $cdbCalendar->rewind(); |
||
118 | $openingHours = []; |
||
119 | |||
120 | $weekSchema = null; |
||
121 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
122 | $period = $cdbCalendar->current(); |
||
123 | $weekSchema = $period->getWeekScheme(); |
||
124 | } elseif ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_Permanent) { |
||
125 | $weekSchema = $cdbCalendar->getWeekScheme(); |
||
126 | } |
||
127 | |||
128 | if ($weekSchema) { |
||
129 | $openingHours = $this->createOpeningHoursFromWeekScheme($weekSchema); |
||
130 | } |
||
131 | |||
132 | if (isset($startDate) && isset($endDate)) { |
||
133 | $calendarTimeSpan = $this->createChronologicalTimestamp($startDate, $endDate); |
||
134 | } |
||
135 | |||
136 | // |
||
137 | // Get the calendar type. |
||
138 | // |
||
139 | $calendarType = null; |
||
140 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_Permanent) { |
||
141 | $calendarType = CalendarType::PERMANENT(); |
||
142 | } elseif ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
143 | $calendarType = CalendarType::PERIODIC(); |
||
144 | } elseif ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
145 | $calendarType = CalendarType::SINGLE(); |
||
146 | if (count($timestamps) > 1) { |
||
147 | $calendarType = CalendarType::MULTIPLE(); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | // |
||
152 | // Create the calendar value object. |
||
153 | // |
||
154 | return new Calendar( |
||
155 | $calendarType, |
||
|
|||
156 | isset($calendarTimeSpan) ? $calendarTimeSpan->getStartDate() : null, |
||
157 | isset($calendarTimeSpan) ? $calendarTimeSpan->getEndDate() : null, |
||
158 | $timestamps, |
||
159 | $openingHours |
||
160 | ); |
||
161 | } |
||
162 | |||
334 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: