Conditions | 23 |
Paths | 15360 |
Total Lines | 133 |
Code Lines | 71 |
Lines | 15 |
Ratio | 11.28 % |
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 |
||
14 | public function createFromCdbCalendar(\CultureFeed_Cdb_Data_Calendar $cdbCalendar) |
||
15 | { |
||
16 | // |
||
17 | // Get the calendar type. |
||
18 | // |
||
19 | $calendarType = ''; |
||
20 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_Permanent) { |
||
21 | $calendarType = 'permanent'; |
||
22 | } else if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
23 | $calendarType = 'periodic'; |
||
24 | } else if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
25 | $calendarType = 'single'; |
||
26 | if (iterator_count($cdbCalendar) > 1) { |
||
27 | $calendarType = 'multiple'; |
||
28 | } |
||
29 | } |
||
30 | |||
31 | // |
||
32 | // Get the start day. |
||
33 | // |
||
34 | $cdbCalendar->rewind(); |
||
35 | $startDateString = ''; |
||
36 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
37 | /** @var \CultureFeed_Cdb_Data_Calendar_Period $period */ |
||
38 | $period = $cdbCalendar->current(); |
||
39 | $startDateString = $period->getDateFrom() . 'T00:00:00'; |
||
40 | View Code Duplication | } else if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
|
|
|||
41 | /** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */ |
||
42 | $timestamp = $cdbCalendar->current(); |
||
43 | if ($timestamp->getStartTime()) { |
||
44 | $startDateString = $timestamp->getDate() . 'T' . $timestamp->getStartTime(); |
||
45 | } else { |
||
46 | $startDateString = $timestamp->getDate() . 'T00:00:00'; |
||
47 | } |
||
48 | } |
||
49 | $startDate = !empty($startDateString) ? DateTimeFactory::dateTimeFromDateString($startDateString) : null; |
||
50 | |||
51 | // |
||
52 | // Get the end day. |
||
53 | // |
||
54 | $cdbCalendar->rewind(); |
||
55 | $endDateString = ''; |
||
56 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
57 | /** @var \CultureFeed_Cdb_Data_Calendar_Period $period */ |
||
58 | $period = $cdbCalendar->current(); |
||
59 | $endDateString = $period->getDateTo() . 'T00:00:00'; |
||
60 | } else if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
61 | $firstTimestamp = $cdbCalendar->current(); |
||
62 | /** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */ |
||
63 | $cdbCalendarAsArray = iterator_to_array($cdbCalendar); |
||
64 | $timestamp = iterator_count($cdbCalendar) > 1 ? end($cdbCalendarAsArray) : $firstTimestamp; |
||
65 | View Code Duplication | if ($timestamp->getEndTime()) { |
|
66 | $endDateString = $timestamp->getDate() . 'T' . $timestamp->getEndTime(); |
||
67 | } else { |
||
68 | $endTime = $timestamp->getStartTime() ? $timestamp->getStartTime() : '00:00:00'; |
||
69 | $endDateString = $timestamp->getDate() . 'T' . $endTime; |
||
70 | } |
||
71 | } |
||
72 | $endDate = !empty($endDateString) ? DateTimeFactory::dateTimeFromDateString($endDateString) : null; |
||
73 | |||
74 | // |
||
75 | // Get the time stamps. |
||
76 | // |
||
77 | $cdbCalendar->rewind(); |
||
78 | $timestamps = []; |
||
79 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) { |
||
80 | while ($cdbCalendar->valid()) { |
||
81 | /** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */ |
||
82 | $timestamp = $cdbCalendar->current(); |
||
83 | $cdbCalendar->next(); |
||
84 | |||
85 | $startTime = $timestamp->getStartTime() ? $timestamp->getStartTime() : '00:00:00'; |
||
86 | $startDateString = $timestamp->getDate() . 'T' . $startTime; |
||
87 | |||
88 | if ($timestamp->getEndTime()) { |
||
89 | $endDateString = $timestamp->getDate() . 'T' . $timestamp->getEndTime(); |
||
90 | } else { |
||
91 | $endDateString = $timestamp->getDate() . 'T' . $startTime; |
||
92 | } |
||
93 | |||
94 | $timestamps[] = $this->createTimestamp( |
||
95 | $startDateString, |
||
96 | $endDateString |
||
97 | ); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | // |
||
102 | // Get the opening hours. |
||
103 | // |
||
104 | $cdbCalendar->rewind(); |
||
105 | $openingHoursAsArray = []; |
||
106 | |||
107 | $weekSchema = null; |
||
108 | if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) { |
||
109 | $period = $cdbCalendar->current(); |
||
110 | $weekSchema = $period->getWeekScheme(); |
||
111 | } else if ($cdbCalendar instanceof \CultureFeed_Cdb_Data_Calendar_Permanent) { |
||
112 | $weekSchema = $cdbCalendar->getWeekScheme(); |
||
113 | } |
||
114 | |||
115 | if ($weekSchema) { |
||
116 | $openingHours = $this->createOpeningHoursFromWeekScheme($weekSchema); |
||
117 | $openingHoursAsArray = $this->openingHoursToArray($openingHours); |
||
118 | } |
||
119 | |||
120 | // End date might be before start date in cdbxml when event takes place |
||
121 | // between e.g. 9 PM and 3 AM (the next day). UDB3 does not support this |
||
122 | // and gracefully ignores the end time. |
||
123 | // |
||
124 | // Example cdbxml: |
||
125 | // |
||
126 | // <timestamp> |
||
127 | // <date>2016-12-16</date> |
||
128 | // <timestart>21:00:00</timestart> |
||
129 | // <timeend>05:00:00</timeend> |
||
130 | // </timestamp> |
||
131 | // |
||
132 | if ($endDate < $startDate) { |
||
133 | $endDate = $startDate; |
||
134 | } |
||
135 | |||
136 | // |
||
137 | // Create the calendar value object. |
||
138 | // |
||
139 | return new Calendar( |
||
140 | CalendarType::fromNative($calendarType), |
||
141 | $startDate, |
||
142 | $endDate, |
||
143 | $timestamps, |
||
144 | $openingHoursAsArray |
||
145 | ); |
||
146 | } |
||
147 | |||
292 |
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.