Completed
Push — master ( 089ad5...045fb9 )
by Richard
10:39
created

NotificationsPreload::eventOnModuleUpdateConfigs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %
Metric Value
dl 14
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
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
use Xoops\Core\Kernel\Handlers\XoopsUser;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsUser.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Xoops\Core\Kernel\Handlers\XoopsModule;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModule.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Xoops\Core\PreloadItem;
15
use Xoops\Module\Plugin;
16
use Xoops\Module\Plugin\ConfigCollector;
17
18
19
/**
20
 * Notifications core preloads
21
 *
22
 * @author    trabis <[email protected]>
23
 * @copyright 2012-2015 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 */
26
class NotificationsPreload extends PreloadItem
27
{
28
    /**
29
     * listen for core.include.common.classmaps
30
     * add any module specific class map entries
31
     *
32
     * @param mixed $args not used
33
     *
34
     * @return void
35
     */
36
    public static function eventCoreIncludeCommonClassmaps($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
37
    {
38
        $path = dirname(__DIR__);
39
        XoopsLoad::addMap(array(
40
            'notifications' => $path . '/class/helper.php',
41
        ));
42
    }
43
44
    public static function eventCoreFooterStart($args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

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

Loading history...
45
    {
46
        $xoops = Xoops::getInstance();
47
        $helper = Notifications::getInstance();
48
49
        $notifications = array();
50
        $notifications['show'] = $xoops->isModule() && $xoops->isUser() && $helper->enabled('inline') ? 1 : 0;
51
        if ($notifications['show']) {
52
            $helper->loadLanguage('main');
53
            $categories = $helper->getSubscribableCategories();
54
            $event_count = 0;
55
            if (!empty($categories)) {
56
                $notification_handler = $helper->getHandlerNotification();
57
                foreach ($categories as $category) {
58
                    $section['name'] = $category['name'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$section was never initialized. Although not strictly required by PHP, it is generally a good practice to add $section = 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...
59
                    $section['title'] = $category['title'];
0 ignored issues
show
Bug introduced by
The variable $section does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
60
                    $section['description'] = $category['description'];
61
                    $section['itemid'] = $category['item_id'];
62
                    $section['events'] = array();
63
                    $subscribed_events = $notification_handler->getSubscribedEvents($category['name'], $category['item_id'], $xoops->module->getVar('mid'), $xoops->user->getVar('uid'));
64
                    foreach ($helper->getEvents($category['name'], true, $xoops->module->getVar('dirname')) as $event) {
65 View Code Duplication
                        if (!empty($event['admin_only']) && !$xoops->user->isAdmin($xoops->module->getVar('mid'))) {
66
                            continue;
67
                        }
68
                        if (!empty($event['invisible'])) {
69
                            continue;
70
                        }
71
                        $subscribed = in_array($event['name'], $subscribed_events) ? 1 : 0;
72
                        $section['events'][$event['name']] = array(
73
                            'name'        => $event['name'],
74
                            'title'       => $event['title'],
75
                            'caption'     => $event['caption'],
76
                            'description' => $event['description'],
77
                            'subscribed'  => $subscribed
78
                        );
79
                        ++$event_count;
80
                    }
81
                    $notifications['categories'][$category['name']] = $section;
82
                }
83
                $notifications['target_page'] = $helper->url('update.php');
84
                $notifications['mid'] = $xoops->module->getVar('mid');
85
                $notifications['redirect_script'] = $xoops->getEnv('PHP_SELF');
86
                $xoops->tpl()->assign(array(
87
                    'lang_activenotifications'  => _MD_NOTIFICATIONS_ACTIVENOTIFICATIONS,
88
                    'lang_notificationoptions'  => _MD_NOTIFICATIONS_NOTIFICATIONOPTIONS,
89
                    'lang_updateoptions'        => _MD_NOTIFICATIONS_UPDATEOPTIONS,
90
                    'lang_updatenow'            => _MD_NOTIFICATIONS_UPDATENOW,
91
                    'lang_category'             => _MD_NOTIFICATIONS_CATEGORY,
92
                    'lang_event'                => _MD_NOTIFICATIONS_EVENT,
93
                    'lang_events'               => _MD_NOTIFICATIONS_EVENTS,
94
                    'lang_checkall'             => _MD_NOTIFICATIONS_CHECKALL,
95
                    'lang_notificationmethodis' => _MD_NOTIFICATIONS_NOTIFICATIONMETHODIS,
96
                    'lang_change'               => _MD_NOTIFICATIONS_CHANGE,
97
                    'editprofile_url'           => XOOPS_URL . '/edituser.php?uid=' . $xoops->user->getVar('uid')
98
                ));
99
                switch ($xoops->user->getVar('notify_method')) {
100
                    case NOTIFICATIONS_METHOD_DISABLE:
101
                        $xoops->tpl()->assign('user_method', _MD_NOTIFICATIONS_DISABLE);
102
                        break;
103
                    case NOTIFICATIONS_METHOD_PM:
104
                        $xoops->tpl()->assign('user_method', _MD_NOTIFICATIONS_PM);
105
                        break;
106
                    case NOTIFICATIONS_METHOD_EMAIL:
107
                        $xoops->tpl()->assign('user_method', _MD_NOTIFICATIONS_EMAIL);
108
                        break;
109
                }
110
            } else {
111
                $notifications['show'] = 0;
112
            }
113
            if ($event_count == 0) {
114
                $notifications['show'] = 0;
115
            }
116
        }
117
        $xoops->tpl()->assign('notifications', $notifications);
118
    }
119
120 View Code Duplication
    public static function eventSystemModuleUpdateConfigs(ConfigCollector $collector)
121
    {
122
        $helper = \Xoops::getModuleHelper('notifications');
123
        if ($plugin = Plugin::getPlugin(
0 ignored issues
show
Unused Code introduced by
$plugin 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...
124
            $collector->module()->getVar('dirname'),
125
            'notifications',
126
            true
127
        )) {
128
            $pluginConfigs = $helper->getPluginableConfigs($collector->module());
129
            $collector->add($pluginConfigs);
130
        }
131
    }
132
133
    public static function eventSystemModuleInstallConfigs(XoopsModule $module)
134
    {
135
        if ($plugin = Plugin::getPlugin($module->getVar('dirname'), 'notifications', true)) {
0 ignored issues
show
Unused Code introduced by
$plugin 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...
136
            Notifications::getInstance()->insertModuleRelations($module);
137
        }
138
    }
139
140
    /**
141
     * remove any notifications for module being uninstalled
142
     *
143
     * @param XoopsModule $module module object
144
     *
145
     * @return void
146
     */
147
    public static function eventSystemModuleUninstall(XoopsModule $module)
148
    {
149
        if ($plugin = Plugin::getPlugin($module->getVar('dirname'), 'notifications')) {
0 ignored issues
show
Unused Code introduced by
$plugin 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...
150
            Notifications::getInstance()->deleteModuleRelations($module);
151
        }
152
    }
153
154
    public static function eventSystemPreferencesForm(XoopsModule $module)
155
    {
156
        if ($plugin = Plugin::getPlugin($module->getVar('dirname'), 'notifications')) {
0 ignored issues
show
Unused Code introduced by
$plugin 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...
157
            Notifications::getInstance()->loadLanguage('main');
158
        }
159
    }
160
161
    /**
162
     * core.include.checklogin.success
163
     *
164
     * @return void
165
     */
166
    public static function eventCoreIncludeCheckloginSuccess()
167
    {
168
        // This was in include checklogin.php, moving here for now
169
        // RMV-NOTIFY
170
        // Perform some maintenance of notification records
171
        $xoops = Xoops::getInstance();
172
        if ($xoops->user instanceof XoopsUser) {
173
            Notifications::getInstance()->getHandlerNotification()->doLoginMaintenance($xoops->user->getVar('uid'));
174
        }
175
    }
176
}
177