notification.inc.php ➔ smartpartner_notify_iteminfo()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 12
nop 2
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * Module: SmartPartner
6
 * Author: The SmartFactory <www.smartfactory.ca>
7
 * Licence: GNU
8
 * @param $category
9
 * @param $item_id
10
 * @return mixed
11
 */
12
13
function smartpartner_notify_iteminfo($category, $item_id)
14
{
15
    // This must contain the name of the folder in which reside SmartPartner
16
    if (!defined('SMARTPARTNER_DIRNAME')) {
17
        define('SMARTPARTNER_DIRNAME', 'smartpartner');
18
    }
19
20
    global $xoopsModule, $xoopsModuleConfig, $xoopsConfig;
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...
21
22
    if (empty($xoopsModule) || $xoopsModule->getVar('dirname') != SMARTPARTNER_DIRNAME) {
23
        $moduleHandler = xoops_getHandler('module');
24
        $module        = &$moduleHandler->getByDirname(SMARTPARTNER_DIRNAME);
25
        $configHandler = xoops_getHandler('config');
26
        $config        = &$configHandler->getConfigsByCat(0, $module->getVar('mid'));
27
    } else {
28
        $module = &$xoopsModule;
29
        $config = &$xoopsModuleConfig;
30
    }
31
32
    if ($category === 'global') {
33
        $item['name'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
34
        $item['url']  = '';
35
36
        return $item;
37
    }
38
39
    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...
40
41
    if ($category === 'item') {
42
        // Assume we have a valid partner id
43
        $sql          = 'SELECT question FROM ' . $xoopsDB->prefix('smartpartner_partner') . ' WHERE id = ' . $item_id;
44
        $result       = $xoopsDB->query($sql); // TODO: error check
45
        $result_array = $xoopsDB->fetchArray($result);
46
        $item['name'] = $result_array['title'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47
        $item['url']  = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/partner.php?id=' . $item_id;
48
49
        return $item;
50
    }
51
}
52