| Conditions | 7 |
| Paths | 4 |
| Total Lines | 56 |
| Code Lines | 44 |
| 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 |
||
| 30 | function b_marquee_catads($limit, $dateFormat, $itemsSize) |
||
| 31 | { |
||
| 32 | global $xoopsModule, $xoopsModuleConfig, $xoopsDB; |
||
|
|
|||
| 33 | include_once XOOPS_ROOT_PATH . '/modules/catads/class/cat.php'; |
||
| 34 | $block = array(); |
||
| 35 | if (empty($xoopsModule) || $xoopsModule->getVar('dirname') !== 'catads') { |
||
| 36 | $moduleHandler = xoops_getHandler('module'); |
||
| 37 | $module = $moduleHandler->getByDirname('catads'); |
||
| 38 | $configHandler = xoops_getHandler('config'); |
||
| 39 | $config =& $configHandler->getConfigsByCat(0, $module->getVar('mid')); |
||
| 40 | } else { |
||
| 41 | $module =& $xoopsModule; |
||
| 42 | $config =& $xoopsModuleConfig; |
||
| 43 | } |
||
| 44 | //echo '<br />ok'; |
||
| 45 | $ads_hnd = xoops_getModuleHandler('ads', 'catads'); |
||
| 46 | $criteria = new CriteriaCompo(new Criteria('waiting', '0')); |
||
| 47 | $criteria->add(new Criteria('published', time(), '<')); |
||
| 48 | $criteria->add(new Criteria('expired', time(), '>')); |
||
| 49 | $criteria->setSort('published'); |
||
| 50 | $criteria->setOrder('DESC'); |
||
| 51 | $criteria->setLimit($limit); |
||
| 52 | $nbads = $ads_hnd->getCount($criteria); |
||
| 53 | |||
| 54 | $itemArray = array(); |
||
| 55 | $catBuffer = array(); |
||
| 56 | |||
| 57 | if ($nbads > 0) { |
||
| 58 | $ads = $ads_hnd->getObjects($criteria); |
||
| 59 | $myts = MyTextSanitizer::getInstance(); |
||
| 60 | foreach ($ads as $oneads) { |
||
| 61 | if ($itemsSize > 0) { |
||
| 62 | $title = xoops_substr($oneads->getVar('ads_title'), 0, $itemsSize); |
||
| 63 | } else { |
||
| 64 | $title = $oneads->getVar('ads_title'); |
||
| 65 | } |
||
| 66 | if (!isset($catBuffer[$oneads->getVar('cat_id')])) { |
||
| 67 | $tmpcat = new AdsCategory($oneads->getVar('cat_id')); |
||
| 68 | $catBuffer[$oneads->getVar('cat_id')] = $tmpcat->title(); |
||
| 69 | $catTitle = $tmpcat->title(); |
||
| 70 | } else { |
||
| 71 | $catTitle = $catBuffer[$oneads->getVar('cat_id')]; |
||
| 72 | } |
||
| 73 | $block[] = array( |
||
| 74 | 'date' => formatTimestamp($oneads->getVar('published'), $dateFormat), |
||
| 75 | 'category' => '', |
||
| 76 | 'author' => XoopsUser::getUnameFromId($oneads->getVar('uid')), |
||
| 77 | 'title' => $title, |
||
| 78 | 'link' => "<a href='" . XOOPS_URL . '/modules/catads/adsitem.php?ads_id=' . $oneads->getVar('ads_id') . "'>" . $title . '</a>' |
||
| 79 | ); |
||
| 80 | unset($itemArray); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | return $block; |
||
| 85 | } |
||
| 86 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state