Passed
Push — master ( dcbd4a...4bb7b0 )
by Michael
02:37
created

xoopspartners_random()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 21
c 1
b 1
f 0
nc 16
nop 4
dl 0
loc 30
rs 6.7272
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 :
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 xoopspartners module (tested with version 1.1)
30
function b_marquee_xoopspartners($limit, $dateFormat, $itemsSize)
0 ignored issues
show
Unused Code introduced by
The parameter $dateFormat is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
function b_marquee_xoopspartners($limit, /** @scrutinizer ignore-unused */ $dateFormat, $itemsSize)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
{
32
    $block    = [];
33
    $myts     = \MyTextSanitizer::getInstance();
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
    $arrayIds = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $arrayIds is dead and can be removed.
Loading history...
35
    $arrayIds = xoopspartners_random($limit);
36
    global $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...
37
38
    foreach ($arrayIds as $id) {
39
        $result = $xoopsDB->query('SELECT id, url, image, title FROM ' . $xoopsDB->prefix('partners') . " WHERE id=$id");
40
        list($id, $url, $image, $title) = $xoopsDB->fetchRow($result);
41
        $origtitle = $title;
42
        $title     = $myts->htmlSpecialChars($title);
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
43
        if ($itemsSize > 0) {
44
            $title = $myts->htmlSpecialChars(substr($origtitle, 0, 19));
45
        } else {
46
            $title = $myts->htmlSpecialChars($origtitle);
47
        }
48
49
        $block[] = [
50
            'date'     => '',
51
            'category' => '',
52
            'author'   => '',
53
            'title'    => $title,
54
            'link'     => "<a href='" . XOOPS_URL . '/modules/xoopspartners/vpartner.php?id=' . $id . "'>" . $title . '</a>'
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
        ];
56
    }
57
58
    return $block;
59
}
60
61
/**
62
 * @param        $numberPartners
63
 * @param bool   $random
64
 * @param string $orden
65
 * @param string $desc
66
 *
67
 * @return array
68
 */
69
function xoopspartners_random($numberPartners, $random = true, $orden = '', $desc = '')
70
{
71
    global $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...
72
    $PartnersId  = [];
73
    $ArrayReturn = [];
74
    $numrows     = 0;
75
    if ($random) {
76
        $result  = $xoopsDB->query('SELECT id FROM ' . $xoopsDB->prefix('partners') . ' WHERE status = 1');
77
        $numrows = $xoopsDB->getRowsNum($result);
78
    } else {
79
        $result = $xoopsDB->query('SELECT id FROM ' . $xoopsDB->prefix('partners') . ' WHERE status = 1 ORDER BY ' . $orden . ' ' . $desc, $numberPartners);
80
    }
81
    while (false !== ($ret = $xoopsDB->fetchArray($result))) {
82
        $PartnersId[] = $ret['id'];
83
    }
84
    if (($numrows <= $numberPartners) || (!$random)) {
85
        return $PartnersId;
86
        //        exit();
87
    }
88
    $numberTotal  = 0;
89
    $totalPartner = count($PartnersId) - 1;
90
    while ($numberPartners > $numberTotal) {
91
        $RandomPart = mt_rand(0, $totalPartner);
92
        if (!in_array($PartnersId[$RandomPart], $ArrayReturn)) {
93
            $ArrayReturn[] = $PartnersId[$RandomPart];
94
            ++$numberTotal;
95
        }
96
    }
97
98
    return $ArrayReturn;
99
}
100