| Conditions | 18 |
| Paths | 320 |
| Total Lines | 86 |
| 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 |
||
| 236 | private function getIcalForItem( array $row ) { |
||
| 237 | $result = ''; |
||
| 238 | |||
| 239 | $subjectDI = $row[0]->getResultSubject(); // get the object |
||
| 240 | $subjectDV = DataValueFactory::getInstance()->newDataValueByItem( $subjectDI, null ); |
||
| 241 | |||
| 242 | $params = [ |
||
| 243 | 'summary' => $subjectDV->getShortWikiText() |
||
| 244 | ]; |
||
| 245 | |||
| 246 | $from = null; |
||
| 247 | $to = null; |
||
| 248 | foreach ( $row as /* SMWResultArray */ |
||
| 249 | $field ) { |
||
| 250 | // later we may add more things like a generic |
||
| 251 | // mechanism to add whatever you want :) |
||
| 252 | // could include funny things like geo, description etc. though |
||
| 253 | $req = $field->getPrintRequest(); |
||
| 254 | $label = strtolower( $req->getLabel() ); |
||
| 255 | |||
| 256 | switch ( $label ) { |
||
| 257 | case 'start': |
||
| 258 | case 'end': |
||
| 259 | if ( $req->getTypeID() == '_dat' ) { |
||
| 260 | $dataValue = $field->getNextDataValue(); |
||
| 261 | |||
| 262 | if ( $dataValue === false ) { |
||
| 263 | unset( $params[$label] ); |
||
| 264 | } else { |
||
| 265 | $params[$label] = $this->dateParser->parseDate( $dataValue, $label == 'end' ); |
||
| 266 | |||
| 267 | $timestamp = strtotime( $params[$label] ); |
||
| 268 | if ( $from === null || $timestamp < $from ) { |
||
| 269 | $from = $timestamp; |
||
| 270 | } |
||
| 271 | if ( $to === null || $timestamp > $to ) { |
||
| 272 | $to = $timestamp; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | break; |
||
| 277 | case 'location': |
||
| 278 | case 'description': |
||
| 279 | case 'summary': |
||
| 280 | $value = $field->getNextDataValue(); |
||
| 281 | if ( $value !== false ) { |
||
| 282 | $params[$label] = $value->getShortWikiText(); |
||
| 283 | } |
||
| 284 | break; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | $this->icalTimezoneFormatter->calcTransitions( $from, $to ); |
||
| 289 | |||
| 290 | $title = $subjectDI->getTitle(); |
||
| 291 | $timestamp = WikiPage::factory( $title )->getTimestamp(); |
||
| 292 | $url = $title->getFullURL(); |
||
| 293 | |||
| 294 | $result .= "BEGIN:VEVENT\r\n"; |
||
| 295 | $result .= "SUMMARY:" . $this->escape( $params['summary'] ) . "\r\n"; |
||
| 296 | $result .= "URL:$url\r\n"; |
||
| 297 | $result .= "UID:$url\r\n"; |
||
| 298 | |||
| 299 | if ( array_key_exists( 'start', $params ) ) { |
||
| 300 | $result .= "DTSTART:" . $params['start'] . "\r\n"; |
||
| 301 | } |
||
| 302 | |||
| 303 | if ( array_key_exists( 'end', $params ) ) { |
||
| 304 | $result .= "DTEND:" . $params['end'] . "\r\n"; |
||
| 305 | } |
||
| 306 | |||
| 307 | if ( array_key_exists( 'location', $params ) ) { |
||
| 308 | $result .= "LOCATION:" . $this->escape( $params['location'] ) . "\r\n"; |
||
| 309 | } |
||
| 310 | |||
| 311 | if ( array_key_exists( 'description', $params ) ) { |
||
| 312 | $result .= "DESCRIPTION:" . $this->escape( $params['description'] ) . "\r\n"; |
||
| 313 | } |
||
| 314 | |||
| 315 | $t = strtotime( str_replace( 'T', ' ', $timestamp ) ); |
||
| 316 | $result .= "DTSTAMP:" . date( "Ymd", $t ) . "T" . date( "His", $t ) . "\r\n"; |
||
| 317 | $result .= "SEQUENCE:" . $title->getLatestRevID() . "\r\n"; |
||
| 318 | $result .= "END:VEVENT\r\n"; |
||
| 319 | |||
| 320 | return $result; |
||
| 321 | } |
||
| 322 | |||
| 333 |