| Conditions | 48 | 
| Paths | 2 | 
| Total Lines | 266 | 
| 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  | 
            ||
| 193 | function rss_buildItems(&$rss, &$data, $opt) { | 
            ||
| 194 | global $conf;  | 
            ||
| 195 | global $lang;  | 
            ||
| 196 | /* @var DokuWiki_Auth_Plugin $auth */  | 
            ||
| 197 | global $auth;  | 
            ||
| 198 | |||
| 199 | $eventData = array(  | 
            ||
| 200 | 'rss' => &$rss,  | 
            ||
| 201 | 'data' => &$data,  | 
            ||
| 202 | 'opt' => &$opt,  | 
            ||
| 203 | );  | 
            ||
| 204 |     $event     = new Doku_Event('FEED_DATA_PROCESS', $eventData); | 
            ||
| 205 |     if($event->advise_before(false)) { | 
            ||
| 206 |         foreach($data as $ditem) { | 
            ||
| 207 |             if(!is_array($ditem)) { | 
            ||
| 208 | // not an array? then only a list of IDs was given  | 
            ||
| 209 |                 $ditem = array('id' => $ditem); | 
            ||
| 210 | }  | 
            ||
| 211 | |||
| 212 | $item = new FeedItem();  | 
            ||
| 213 | $id = $ditem['id'];  | 
            ||
| 214 |             if(!$ditem['media']) { | 
            ||
| 215 | $meta = p_get_metadata($id);  | 
            ||
| 216 |             } else { | 
            ||
| 217 | $meta = array();  | 
            ||
| 218 | }  | 
            ||
| 219 | |||
| 220 | // add date  | 
            ||
| 221 |             if($ditem['date']) { | 
            ||
| 222 | $date = $ditem['date'];  | 
            ||
| 223 |             } elseif ($ditem['media']) { | 
            ||
| 224 | $date = @filemtime(mediaFN($id));  | 
            ||
| 225 |             } elseif (file_exists(wikiFN($id))) { | 
            ||
| 226 | $date = @filemtime(wikiFN($id));  | 
            ||
| 227 |             } elseif($meta['date']['modified']) { | 
            ||
| 228 | $date = $meta['date']['modified'];  | 
            ||
| 229 |             } else { | 
            ||
| 230 | $date = 0;  | 
            ||
| 231 | }  | 
            ||
| 232 |             if($date) $item->date = date('r', $date); | 
            ||
| 233 | |||
| 234 | // add title  | 
            ||
| 235 |             if($conf['useheading'] && $meta['title']) { | 
            ||
| 236 | $item->title = $meta['title'];  | 
            ||
| 237 |             } else { | 
            ||
| 238 | $item->title = $ditem['id'];  | 
            ||
| 239 | }  | 
            ||
| 240 |             if($conf['rss_show_summary'] && !empty($ditem['sum'])) { | 
            ||
| 241 | $item->title .= ' - '.strip_tags($ditem['sum']);  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | // add item link  | 
            ||
| 245 |             switch($opt['link_to']) { | 
            ||
| 246 | case 'page':  | 
            ||
| 247 |                     if($ditem['media']) { | 
            ||
| 248 | $item->link = media_managerURL(  | 
            ||
| 249 | array(  | 
            ||
| 250 | 'image' => $id,  | 
            ||
| 251 | 'ns' => getNS($id),  | 
            ||
| 252 | 'rev' => $date  | 
            ||
| 253 | ), '&', true  | 
            ||
| 254 | );  | 
            ||
| 255 |                     } else { | 
            ||
| 256 | $item->link = wl($id, 'rev='.$date, true, '&');  | 
            ||
| 257 | }  | 
            ||
| 258 | break;  | 
            ||
| 259 | case 'rev':  | 
            ||
| 260 |                     if($ditem['media']) { | 
            ||
| 261 | $item->link = media_managerURL(  | 
            ||
| 262 | array(  | 
            ||
| 263 | 'image' => $id,  | 
            ||
| 264 | 'ns' => getNS($id),  | 
            ||
| 265 | 'rev' => $date,  | 
            ||
| 266 | 'tab_details' => 'history'  | 
            ||
| 267 | ), '&', true  | 
            ||
| 268 | );  | 
            ||
| 269 |                     } else { | 
            ||
| 270 | $item->link = wl($id, 'do=revisions&rev='.$date, true, '&');  | 
            ||
| 271 | }  | 
            ||
| 272 | break;  | 
            ||
| 273 | case 'current':  | 
            ||
| 274 |                     if($ditem['media']) { | 
            ||
| 275 | $item->link = media_managerURL(  | 
            ||
| 276 | array(  | 
            ||
| 277 | 'image' => $id,  | 
            ||
| 278 | 'ns' => getNS($id)  | 
            ||
| 279 | ), '&', true  | 
            ||
| 280 | );  | 
            ||
| 281 |                     } else { | 
            ||
| 282 | $item->link = wl($id, '', true, '&');  | 
            ||
| 283 | }  | 
            ||
| 284 | break;  | 
            ||
| 285 | case 'diff':  | 
            ||
| 286 | default:  | 
            ||
| 287 |                     if($ditem['media']) { | 
            ||
| 288 | $item->link = media_managerURL(  | 
            ||
| 289 | array(  | 
            ||
| 290 | 'image' => $id,  | 
            ||
| 291 | 'ns' => getNS($id),  | 
            ||
| 292 | 'rev' => $date,  | 
            ||
| 293 | 'tab_details' => 'history',  | 
            ||
| 294 | 'mediado' => 'diff'  | 
            ||
| 295 | ), '&', true  | 
            ||
| 296 | );  | 
            ||
| 297 |                     } else { | 
            ||
| 298 | $item->link = wl($id, 'rev='.$date.'&do=diff', true, '&');  | 
            ||
| 299 | }  | 
            ||
| 300 | }  | 
            ||
| 301 | |||
| 302 | // add item content  | 
            ||
| 303 |             switch($opt['item_content']) { | 
            ||
| 304 | case 'diff':  | 
            ||
| 305 | case 'htmldiff':  | 
            ||
| 306 |                     if($ditem['media']) { | 
            ||
| 307 | $medialog = new MediaChangeLog($id);  | 
            ||
| 308 | $revs = $medialog->getRevisions(0, 1);  | 
            ||
| 309 | $rev = $revs[0];  | 
            ||
| 310 | $src_r = '';  | 
            ||
| 311 | $src_l = '';  | 
            ||
| 312 | |||
| 313 |                         if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) { | 
            ||
| 314 | $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));  | 
            ||
| 315 | $src_r = ml($id, $more, true, '&', true);  | 
            ||
| 316 | }  | 
            ||
| 317 |                         if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) { | 
            ||
| 318 | $more = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1];  | 
            ||
| 319 | $src_l = ml($id, $more, true, '&', true);  | 
            ||
| 320 | }  | 
            ||
| 321 | $content = '';  | 
            ||
| 322 |                         if($src_r) { | 
            ||
| 323 | $content = '<table>';  | 
            ||
| 324 | $content .= '<tr><th width="50%">'.$rev.'</th>';  | 
            ||
| 325 | $content .= '<th width="50%">'.$lang['current'].'</th></tr>';  | 
            ||
| 326 | $content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>';  | 
            ||
| 327 | $content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>';  | 
            ||
| 328 | $content .= '</table>';  | 
            ||
| 329 | }  | 
            ||
| 330 | |||
| 331 |                     } else { | 
            ||
| 332 | require_once(DOKU_INC.'inc/DifferenceEngine.php');  | 
            ||
| 333 | $pagelog = new PageChangeLog($id);  | 
            ||
| 334 | $revs = $pagelog->getRevisions(0, 1);  | 
            ||
| 335 | $rev = $revs[0];  | 
            ||
| 336 | |||
| 337 |                         if($rev) { | 
            ||
| 338 |                             $df = new Diff(explode("\n", rawWiki($id, $rev)), | 
            ||
| 339 |                                            explode("\n", rawWiki($id, ''))); | 
            ||
| 340 |                         } else { | 
            ||
| 341 |                             $df = new Diff(array(''), | 
            ||
| 342 |                                            explode("\n", rawWiki($id, ''))); | 
            ||
| 343 | }  | 
            ||
| 344 | |||
| 345 |                         if($opt['item_content'] == 'htmldiff') { | 
            ||
| 346 | // note: no need to escape diff output, TableDiffFormatter provides 'safe' html  | 
            ||
| 347 | $tdf = new TableDiffFormatter();  | 
            ||
| 348 | $content = '<table>';  | 
            ||
| 349 | $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';  | 
            ||
| 350 | $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';  | 
            ||
| 351 | $content .= $tdf->format($df);  | 
            ||
| 352 | $content .= '</table>';  | 
            ||
| 353 |                         } else { | 
            ||
| 354 | // note: diff output must be escaped, UnifiedDiffFormatter provides plain text  | 
            ||
| 355 | $udf = new UnifiedDiffFormatter();  | 
            ||
| 356 | $content = "<pre>\n".hsc($udf->format($df))."\n</pre>";  | 
            ||
| 357 | }  | 
            ||
| 358 | }  | 
            ||
| 359 | break;  | 
            ||
| 360 | case 'html':  | 
            ||
| 361 |                     if($ditem['media']) { | 
            ||
| 362 |                         if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) { | 
            ||
| 363 | $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));  | 
            ||
| 364 | $src = ml($id, $more, true, '&', true);  | 
            ||
| 365 | $content = '<img src="'.$src.'" alt="'.$id.'" />';  | 
            ||
| 366 |                         } else { | 
            ||
| 367 | $content = '';  | 
            ||
| 368 | }  | 
            ||
| 369 |                     } else { | 
            ||
| 370 |                         if (@filemtime(wikiFN($id)) === $date) { | 
            ||
| 371 | $content = p_wiki_xhtml($id, '', false);  | 
            ||
| 372 |                         } else { | 
            ||
| 373 | $content = p_wiki_xhtml($id, $date, false);  | 
            ||
| 374 | }  | 
            ||
| 375 | // no TOC in feeds  | 
            ||
| 376 |                         $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content); | 
            ||
| 377 | |||
| 378 | // add alignment for images  | 
            ||
| 379 |                         $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content); | 
            ||
| 380 |                         $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content); | 
            ||
| 381 | |||
| 382 | // make URLs work when canonical is not set, regexp instead of rerendering!  | 
            ||
| 383 |                         if(!$conf['canonical']) { | 
            ||
| 384 | $base = preg_quote(DOKU_REL, '/');  | 
            ||
| 385 |                             $content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL, $content); | 
            ||
| 386 | }  | 
            ||
| 387 | }  | 
            ||
| 388 | |||
| 389 | break;  | 
            ||
| 390 | case 'abstract':  | 
            ||
| 391 | default:  | 
            ||
| 392 |                     if($ditem['media']) { | 
            ||
| 393 |                         if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) { | 
            ||
| 394 | $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));  | 
            ||
| 395 | $src = ml($id, $more, true, '&', true);  | 
            ||
| 396 | $content = '<img src="'.$src.'" alt="'.$id.'" />';  | 
            ||
| 397 |                         } else { | 
            ||
| 398 | $content = '';  | 
            ||
| 399 | }  | 
            ||
| 400 |                     } else { | 
            ||
| 401 | $content = $meta['description']['abstract'];  | 
            ||
| 402 | }  | 
            ||
| 403 | }  | 
            ||
| 404 | $item->description = $content; //FIXME a plugin hook here could be senseful  | 
            ||
| 405 | |||
| 406 | // add user  | 
            ||
| 407 | # FIXME should the user be pulled from metadata as well?  | 
            ||
| 408 | $user = @$ditem['user']; // the @ spares time repeating lookup  | 
            ||
| 409 |             if(blank($user)) { | 
            ||
| 410 | $item->author = 'Anonymous';  | 
            ||
| 411 | $item->authorEmail = '[email protected]';  | 
            ||
| 412 |             } else { | 
            ||
| 413 | $item->author = $user;  | 
            ||
| 414 | $item->authorEmail = $user . '@undisclosed.example.com';  | 
            ||
| 415 | |||
| 416 | // get real user name if configured  | 
            ||
| 417 |                 if($conf['useacl'] && $auth) { | 
            ||
| 418 | $userInfo = $auth->getUserData($user);  | 
            ||
| 419 |                     if($userInfo) { | 
            ||
| 420 |                         switch($conf['showuseras']) { | 
            ||
| 421 | case 'username':  | 
            ||
| 422 | case 'username_link':  | 
            ||
| 423 | $item->author = $userInfo['name'];  | 
            ||
| 424 | break;  | 
            ||
| 425 | default:  | 
            ||
| 426 | $item->author = $user;  | 
            ||
| 427 | break;  | 
            ||
| 428 | }  | 
            ||
| 429 |                     } else { | 
            ||
| 430 | $item->author = $user;  | 
            ||
| 431 | }  | 
            ||
| 432 | }  | 
            ||
| 433 | }  | 
            ||
| 434 | |||
| 435 | // add category  | 
            ||
| 436 |             if(isset($meta['subject'])) { | 
            ||
| 437 | $item->category = $meta['subject'];  | 
            ||
| 438 |             } else { | 
            ||
| 439 | $cat = getNS($id);  | 
            ||
| 440 | if($cat) $item->category = $cat;  | 
            ||
| 441 | }  | 
            ||
| 442 | |||
| 443 | // finally add the item to the feed object, after handing it to registered plugins  | 
            ||
| 444 | $evdata = array(  | 
            ||
| 445 | 'item' => &$item,  | 
            ||
| 446 | 'opt' => &$opt,  | 
            ||
| 447 | 'ditem' => &$ditem,  | 
            ||
| 448 | 'rss' => &$rss  | 
            ||
| 449 | );  | 
            ||
| 450 |             $evt    = new Doku_Event('FEED_ITEM_ADD', $evdata); | 
            ||
| 451 |             if($evt->advise_before()) { | 
            ||
| 452 | $rss->addItem($item);  | 
            ||
| 453 | }  | 
            ||
| 454 | $evt->advise_after(); // for completeness  | 
            ||
| 455 | }  | 
            ||
| 456 | }  | 
            ||
| 457 | $event->advise_after();  | 
            ||
| 458 | }  | 
            ||
| 459 | |||
| 515 | 
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: