Conditions | 3 |
Paths | 3 |
Total Lines | 100 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
106 | public function save() |
||
107 | { |
||
108 | $db = static::getDatabase(); |
||
109 | $api = static::getApi(); |
||
110 | |||
111 | $select = $db->prepare( |
||
112 | "SELECT * |
||
113 | FROM `events` |
||
114 | WHERE |
||
115 | `event_hash` = :event_hash AND |
||
116 | `calendar` = :calendar" |
||
117 | ); |
||
118 | $update = $db->prepare( |
||
119 | "UPDATE `events` |
||
120 | SET |
||
121 | `synced` = :synced |
||
122 | WHERE |
||
123 | `event_hash` = :event_hash AND |
||
124 | `calendar` = :calendar" |
||
125 | ); |
||
126 | $insert = $db->prepare( |
||
127 | "INSERT INTO `events` |
||
128 | ( |
||
129 | `calendar`, |
||
130 | `calendar_event[id]`, |
||
131 | `event_hash`, |
||
132 | `synced` |
||
133 | ) VALUES ( |
||
134 | :calendar, |
||
135 | :calendar_event_id, |
||
136 | :event_hash, |
||
137 | :synced |
||
138 | )" |
||
139 | ); |
||
140 | |||
141 | $params = [ |
||
142 | 'calendar' => $this->getCalendar()->getId(), |
||
143 | 'event_hash' => $this->getHash(), |
||
144 | 'synced' => static::getTimestamp() |
||
145 | ]; |
||
146 | |||
147 | $select->execute($params); |
||
148 | if ($select->fetch() !== false) { |
||
149 | $update->execute($params); |
||
150 | } else { |
||
151 | /* |
||
152 | * FIXME: how sure are we of this? issue:14 |
||
153 | */ |
||
154 | /* |
||
155 | * multi-day event instance start times need to be changed to |
||
156 | * _this_ date |
||
157 | */ |
||
158 | $start = new DateTime( |
||
159 | iCalUtilityFunctions::_date2strdate( |
||
160 | $this->getProperty('DTSTART') |
||
161 | ) |
||
162 | ); |
||
163 | $end = new DateTime( |
||
164 | iCalUtilityFunctions::_date2strdate( |
||
165 | $this->getProperty('DTEND') |
||
166 | ) |
||
167 | ); |
||
168 | if ($this->getProperty('X-RECURRENCE')) { |
||
169 | $start = new DateTime($this->getProperty('X-CURRENT-DTSTART')[1]); |
||
170 | $end = new DateTime($this->getProperty('X-CURRENT-DTEND')[1]); |
||
171 | } |
||
172 | $start->setTimeZone(new DateTimeZone(self::LOCAL_TIMEZONE)); |
||
173 | $end->setTimeZone(new DateTimeZone(self::LOCAL_TIMEZONE)); |
||
174 | |||
175 | $calendarEvent = $api->post( |
||
176 | "/calendar_events", |
||
177 | [ |
||
178 | 'calendar_event' => [ |
||
179 | 'context_code' => $this->getCalendar()->getContextCode(), |
||
180 | /* |
||
181 | * TODO this should be configurable issue:5 |
||
182 | */ |
||
183 | /* removing trailing [TAGS] from event title */ |
||
184 | 'title' => preg_replace( |
||
185 | '%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', |
||
186 | '\\1', |
||
187 | strip_tags($this->getProperty('SUMMARY')) |
||
188 | ), |
||
189 | 'description' => Markdown::defaultTransform( |
||
190 | str_replace( |
||
191 | '\n', |
||
192 | "\n\n", |
||
193 | $this->getProperty('DESCRIPTION', 1) |
||
194 | ) |
||
195 | ), |
||
196 | 'start_at' => $start->format(self::CANVAS_TIMESTAMP_FORMAT), |
||
197 | 'end_at' => $end->format(self::CANVAS_TIMESTAMP_FORMAT), |
||
198 | 'location_name' => $this->getProperty('LOCATION') |
||
199 | ] |
||
200 | ] |
||
201 | ); |
||
202 | $params['calendar_event_id'] = $calendarEvent['id']; |
||
203 | $insert->execute($params); |
||
204 | } |
||
205 | } |
||
206 | |||
250 |