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