references_random_news.php ➔ b_references_random_news_show()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 25

Duplication

Lines 3
Ratio 12 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 3
loc 25
rs 8.8977
c 0
b 0
f 0
1
<?php
2
/**
3
 * ****************************************************************************
4
 * references - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.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 of Instant Zero (http://www.instant-zero.com)
15
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package         references
17
 * @author          Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
18
 *
19
 * ****************************************************************************
20
 */
21
/**
22
 * Affiche x articles au hasard
23
 *
24
 * @param array $options indice 0 = nombre d'articles à afficher
25
 * @return array Le contenu du bloc
26
 */
27
function b_references_random_news_show($options)
28
{
29
    global $xoopsConfig;
30
    require XOOPS_ROOT_PATH . '/modules/references/include/common.php';
31
    $block = array();
32
    $start = 0;
33
    $limit = (int)$options[0];
34
    if ($limit > 0) {
35
        $items = array();
0 ignored issues
show
Unused Code introduced by
$items 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...
36
        $h_references_articles->setCachingOptions(array('cacheDir' => REFERENCES_CACHE_PATH, 'caching' => false, 'lifeTime' => null, 'automaticSerialization' => true, 'fileNameProtection' => false));
0 ignored issues
show
Bug introduced by
The variable $h_references_articles does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
37
        $categories = array();
38 View Code Duplication
        if (is_array($options) && count($options) > 1) {
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...
39
            $categories = array_slice($options, 1);
40
        }
41
        $items = $h_references_articles->getRecentArticles($start, $limit, 'RAND(), NOW()', 'DESC', true, $categories);
42
        $h_references_articles->setCachingOptions(array('cacheDir' => REFERENCES_CACHE_PATH, 'caching' => true, 'lifeTime' => null, 'automaticSerialization' => true, 'fileNameProtection' => false));
43
        if (count($items) > 0) {
44
            foreach ($items as $item) {
45
                $block['block_random_news'][] = $item->toArray();
46
            }
47
        }
48
    }
49
50
    return $block;
51
}
52
53
/**
54
 * Paramètres du bloc
55
 *
56
 * @param array $options indice 0 = nombre d'articles à afficher
57
 * @return string
58
 */
59 View Code Duplication
function b_references_random_news_edit($options)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
60
{
61
    global $xoopsConfig;
62
    include XOOPS_ROOT_PATH . '/modules/references/include/common.php';
63
    $handlers   = references_handler::getInstance();
64
    $categories = $handlers->h_references_categories->getListArray();
65
    $form       = '';
66
    $form .= "<table border='0'>";
67
    $form .= '<tr><td>' . _MB_REFERENCES_ITEMS_COUNT . "</td><td><input type='text' name='options[]' id='options' value='" . $options[0] . "' /></td></tr>\n";
68
    $form .= '<tr><td>' . _MB_REFERENCES_CATEGORIES . "</td><td><select name='options[]' multiple='multiple'>";
69
    $size = count($options);
70
    foreach ($categories as $Idcategory => $categoryName) {
71
        $sel = '';
72
        for ($i = 1; $i < $size; ++$i) {
73
            if ($options[$i] == $Idcategory) {
74
                $sel = " selected='selected'";
75
            }
76
        }
77
        $form .= "<option value='$Idcategory' $sel>$categoryName</option>";
78
    }
79
    $form .= "</select></td></tr>\n";
80
    $form .= '</table>';
81
82
    return $form;
83
}
84