Completed
Push — master ( 66731e...5478d6 )
by Michael
02:20
created

catads.php ➔ b_marquee_catads()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 56
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 44
nc 4
nop 3
dl 0
loc 56
rs 7.7489
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * ****************************************************************************
4
 * marquee - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard (http://www.herve-thouzard.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright         Hervé Thouzard (http://www.herve-thouzard.com)
15
 * @license           http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package           marquee
17
 * @author            Hervé Thouzard (http://www.herve-thouzard.com)
18
 *
19
 * Version : $Id:
20
 * ****************************************************************************
21
 *
22
 * @param $limit
23
 * @param $dateFormat
24
 * @param $itemsSize
25
 *
26
 * @return array
27
 */
28
29
// Script to list recent ads from the catads module (tested with catads v 1.4)
30
function b_marquee_catads($limit, $dateFormat, $itemsSize)
31
{
32
    global $xoopsModule, $xoopsModuleConfig, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
$myts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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();
0 ignored issues
show
Unused Code introduced by
$catTitle is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
            } else {
71
                $catTitle = $catBuffer[$oneads->getVar('cat_id')];
0 ignored issues
show
Unused Code introduced by
$catTitle is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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