Passed
Push — master ( 48749a...470881 )
by Michael
03:31
created

adslight_notify_iteminfo()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 52
rs 8.4906
cc 7
nc 12
nop 2

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 declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project (https://xoops.org)
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team
16
 * @author       Pascal Le Boustouller: original author ([email protected])
17
 * @author       Luc Bizet (www.frxoops.org)
18
 * @author       jlm69 (www.jlmzone.com)
19
 * @author       mamba (www.xoops.org)
20
 * @param mixed $category
21
 * @param mixed $item_id
22
 */
23
24
/**
25
 * @param $category
26
 * @param $item_id
27
 *
28
 * @return array|void
29
 */
30
function adslight_notify_iteminfo(
31
    $category,
32
    $item_id
33
) {
34
    global $xoopsDB;
35
    $moduleDirName = \basename(\dirname(__DIR__));
36
    /** @var \XoopsModuleHandler $moduleHandler */
37
    $moduleHandler = xoops_getHandler('module');
38
    $module        = $moduleHandler->getByDirname($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
39
    if ('global' === $category) {
40
        $item['name'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.
Loading history...
41
        $item['url']  = '';
42
43
        return $item;
44
    }
45
46
    $item_id = (int)$item_id;
47
    if ('category' === $category) {
48
        // Assume we have a valid topid id
49
        $sql = 'SELECT SQL_CACHE title  FROM ' . $xoopsDB->prefix('adslight_categories') . " WHERE cid ={$item_id} LIMIT 1";
50
51
        $result = $xoopsDB->query($sql);
52
        if (!$xoopsDB->isResultSet($result)) {
53
            \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
54
        }
55
        if ($xoopsDB->isResultSet($result)) {
56
            $result_array = $xoopsDB->fetchArray($result);
57
            $item['name'] = $result_array['title'];
58
            $item['url']  = XOOPS_URL . '/modules/adslight/index.php?pa=adsview&amp;cid=' . $item_id;
59
60
            return $item;
61
        }
62
        /** @var \XoopsModuleHandler $moduleHandler */
63
        $moduleHandler = xoops_getHandler('module');
64
        /** @var \XoopsModule $myModule */
65
        $myModule = $moduleHandler->getByDirname('adslight');
66
        $myModule->setErrors('Could not query the database.');
67
    }
68
69
    if ('listing' === $category) {
70
        // Assume we have a valid post id
71
        $sql          = 'SELECT title FROM ' . $xoopsDB->prefix('adslight_listing') . " WHERE lid={$item_id} LIMIT 1";
72
        $result = $xoopsDB->query($sql);
73
        if (!$xoopsDB->isResultSet($result)) {
74
            \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
75
        }
76
        $result_array = $xoopsDB->fetchArray($result);
77
        $item['name'] = $result_array['title'];
78
        //      $item['catname'] = $result_array['cat.title'];
79
        $item['url'] = XOOPS_URL . '/modules/adslight/viewads.php?lid= ' . $item_id;
80
81
        return $item;
82
    }
83
}
84