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

smartpartner.php ➔ b_marquee_smartpartner()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 55
Code Lines 35

Duplication

Lines 5
Ratio 9.09 %

Importance

Changes 0
Metric Value
cc 12
eloc 35
nc 24
nop 3
dl 5
loc 55
rs 6.8009
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 partners from the smartpartner module (tested with smartparnter 1.2)
30
function b_marquee_smartpartner($limit, $dateFormat, $itemsSize)
31
{
32
    $block = $newObjects = array();
33
    if (!defined('SMARTPARTNER_DIRNAME')) {
34
        define('SMARTPARTNER_DIRNAME', 'smartpartner');
35
    }
36
    include_once(XOOPS_ROOT_PATH . '/modules/' . SMARTPARTNER_DIRNAME . '/include/common.php');
37
38
    // Creating the partner handler object
39
    $smartpartnerPartnerHandler  = smartpartner_gethandler('partner');
40
    $smartpartnerCategoryHandler = smartpartner_gethandler('category');
41
42
    // Randomize
43
    $partnersObj = $smartpartnerPartnerHandler->getPartners(0, 0, _SPARTNER_STATUS_ACTIVE);
44
    if (count($partnersObj) > 1) {
45
        $keyArray  = array_keys($partnersObj);
46
        $keyRand = array_rand($keyArray, count($keyArray));
47
        for ($i = 0; ($i < count($partnersObj)) && ($i < $limit); ++$i) {
48
            $newObjects[$i] = $partnersObj[$keyRand[$i]];
49
        }
50
        $partnersObj = $newObjects;
51
    }
52
    $catId = array();
53
    foreach ($partnersObj as $partnerObj) {
54
        if (!in_array($partnerObj->categoryid(), $catId)) {
55
            $catId[] = $partnerObj->categoryid();
56
        }
57
    }
58
59
    if ($partnersObj) {
60
        for ($j = 0, $jMax = count($catId); $j < $jMax; ++$j) {
61
            $categoryObj = $smartpartnerCategoryHandler->get($catId[$j]);
0 ignored issues
show
Unused Code introduced by
$categoryObj 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...
62
            for ($i = 0, $iMax = count($partnersObj); $i < $iMax; ++$i) {
63
                if ($partnersObj[$i]->categoryid() == $catId[$j]) {
64
                    $smartConfig =& smartpartner_getModuleConfig();
65 View Code Duplication
                    if ($itemsSize > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
                        $title = xoops_substr($partnersObj[$i]->title(), 0, $itemsSize + 3);
67
                    } else {
68
                        $title = $partnersObj[$i]->title();
69
                    }
70
71
                    $block[] = array(
72
                        'date'     => '',
73
                        'category' => '',
74
                        'author'   => '',
75
                        'title'    => $title,
76
                        'link'     => "<a href='" . XOOPS_URL . '/modules/smartpartner/partner.php?id=' . $partnersObj[$i]->id() . "'>" . $title . '</a>'
77
                    );
78
                }
79
            }
80
        }
81
    }
82
83
    return $block;
84
}
85