Completed
Push — master ( 62af87...249590 )
by Michael
03:02
created

PermissionHandler   C

Complexity

Total Complexity 62

Size/Duplication

Total Lines 399
Duplicated Lines 7.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 30
loc 399
rs 5.9493
c 0
b 0
f 0
wmc 62
lcom 1
cbo 1

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A loadHandler() 0 10 2
A getValidForumPerms() 0 6 1
A getPermissionTable() 0 7 1
A deleteByForum() 0 7 1
A deleteByCategory() 0 7 1
A setCategoryPermission() 0 7 1
C getPermission() 0 22 7
A getCategories() 0 6 1
A getForums() 0 6 1
C getAllowedItems() 0 28 8
A getGroups() 0 8 2
C createPermData() 8 49 13
A loadPermData() 0 8 2
B validateRight() 11 21 5
B myCheckRight() 0 24 5
C deleteRight() 11 32 7
A applyTemplate() 0 7 1
A getTemplate() 0 7 1
A setTemplate() 0 6 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PermissionHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PermissionHandler, and based on these observations, apply Extract Interface, too.

1
<?php namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
use XoopsModules\Newbb;
14
15
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
17
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
18
define('NEWBB_HANDLER_PERMISSION', 1);
19
20
// Initializing XoopsGroupPermHandler if not loaded yet
21
if (!class_exists('XoopsGroupPermHandler')) {
22
    require_once $GLOBALS['xoops']->path('kernel/groupperm.php');
23
}
24
25
/**
26
 * Class PermissionHandler
27
 */
28
class PermissionHandler extends \XoopsGroupPermHandler
29
{
30
    public $_handler;
0 ignored issues
show
Coding Style introduced by
$_handler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
31
    /** @var \Xmf\Module\Helper\Cache */
32
    protected $cacheHelper;
33
34
    /**
35
     * @param $db
36
     */
37
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
38
    {
39
        $this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
40
41
        $this->db = $db;
42
        parent::__construct($db);
43
    }
44
45
    /**
46
     * @param $name
47
     * @return mixed
48
     */
49
    public function loadHandler($name)
50
    {
51
        if (!isset($this->_handler[$name])) {
52
//            require_once __DIR__ . "/permission.{$name}.php";
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
            $className             = '\\XoopsModules\\Newbb\\Permission' . ucfirst($name) . 'Handler';
54
            $this->_handler[$name] = new $className($this->db);
55
        }
56
57
        return $this->_handler[$name];
58
    }
59
60
    /**
61
     * @param  bool $fullname
62
     * @return mixed
63
     */
64
    public function getValidForumPerms($fullname = false)
65
    {
66
        $handler = $this->loadHandler('forum');
67
68
        return $handler->getValidPerms($fullname);
69
    }
70
71
    /**
72
     * @param  int  $forum
73
     * @param  bool $topic_locked
74
     * @param  bool $isAdmin
75
     * @return mixed
76
     */
77
    public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false)
0 ignored issues
show
Coding Style introduced by
$topic_locked does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
78
    {
79
        $handler = $this->loadHandler('forum');
80
        $perm    = $handler->getPermissionTable($forum, $topic_locked, $isAdmin);
0 ignored issues
show
Coding Style introduced by
$topic_locked does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
81
82
        return $perm;
83
    }
84
85
    /**
86
     * @param $forum_id
87
     * @return mixed
88
     */
89
    public function deleteByForum($forum_id)
0 ignored issues
show
Coding Style introduced by
$forum_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
90
    {
91
        $this->cacheHelper->delete('permission_forum');
92
        $handler = $this->loadHandler('forum');
93
94
        return $handler->deleteByForum($forum_id);
0 ignored issues
show
Coding Style introduced by
$forum_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
95
    }
96
97
    /**
98
     * @param $cat_id
99
     * @return mixed
100
     */
101
    public function deleteByCategory($cat_id)
0 ignored issues
show
Coding Style introduced by
$cat_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
102
    {
103
        $this->cacheHelper->delete('permission_category');
104
        $handler = $this->loadHandler('category');
105
106
        return $handler->deleteByCategory($cat_id);
0 ignored issues
show
Coding Style introduced by
$cat_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
107
    }
108
109
    /**
110
     * @param        $category
111
     * @param  array $groups
112
     * @return mixed
113
     */
114
    public function setCategoryPermission($category, array $groups = [])
115
    {
116
        $this->cacheHelper->delete('permission_category');
117
        $handler = $this->loadHandler('category');
118
119
        return $handler->setCategoryPermission($category, $groups);
120
    }
121
122
    /**
123
     * @param         $type
124
     * @param  string $gperm_name
125
     * @param  int    $id
126
     * @return bool
127
     */
128
    public function getPermission($type, $gperm_name = 'access', $id = 0)
0 ignored issues
show
Coding Style introduced by
function getPermission() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
getPermission uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
129
    {
130
        global $xoopsModule;
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...
131
        $ret = false;
132
        if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) {
133
            $ret = true;
134
        }
135
136
        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
137
        if (!$groups) {
138
            $ret = false;
139
        }
140
        if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) {
0 ignored issues
show
Coding Style introduced by
$allowed_groups does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
141
            $ret = false;
142
        }
143
144
        if (count(array_intersect($allowed_groups, $groups)) > 0) {
0 ignored issues
show
Coding Style introduced by
$allowed_groups does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
145
            $ret = true;
146
        }
147
148
        return $ret;
149
    }
150
151
    /**
152
     * @param  string $perm_name
153
     * @return array
154
     */
155
    public function &getCategories($perm_name = 'access')
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
156
    {
157
        $ret = $this->getAllowedItems('category', "category_{$perm_name}");
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
158
159
        return $ret;
160
    }
161
162
    /**
163
     * @param  string $perm_name
164
     * @return array
165
     */
166
    public function getForums($perm_name = 'access')
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
167
    {
168
        $ret = $this->getAllowedItems('forum', "forum_{$perm_name}");
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
169
170
        return $ret;
171
    }
172
173
    /**
174
     * @param $type
175
     * @param $perm_name
176
     * @return array
177
     */
178
    public function getAllowedItems($type, $perm_name)
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
getAllowedItems uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
179
    {
180
        $ret = [];
181
182
        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
183
        if (count($groups) < 1) {
184
            return $ret;
185
        }
186
187
        if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) {
0 ignored issues
show
Coding Style introduced by
$_cachedPerms does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
188
            return $ret;
189
        }
190
191
        $allowed_items = [];
0 ignored issues
show
Coding Style introduced by
$allowed_items does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
192
        foreach ($_cachedPerms as $id => $allowed_groups) {
0 ignored issues
show
Coding Style introduced by
$_cachedPerms does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
193
            if (0 == $id || empty($allowed_groups)) {
0 ignored issues
show
Coding Style introduced by
$allowed_groups does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
194
                continue;
195
            }
196
197
            if (array_intersect($groups, $allowed_groups)) {
0 ignored issues
show
Coding Style introduced by
$allowed_groups does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
198
                $allowed_items[$id] = 1;
0 ignored issues
show
Coding Style introduced by
$allowed_items does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
199
            }
200
        }
201
        unset($_cachedPerms);
0 ignored issues
show
Coding Style introduced by
$_cachedPerms does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
202
        $ret = array_keys($allowed_items);
0 ignored issues
show
Coding Style introduced by
$allowed_items does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
203
204
        return $ret;
205
    }
206
207
    /**
208
     * @param        $gperm_name
209
     * @param  int   $id
210
     * @return array
211
     */
212
    public function getGroups($gperm_name, $id = 0)
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
213
    {
214
        $_cachedPerms = $this->loadPermData($gperm_name);
0 ignored issues
show
Coding Style introduced by
$_cachedPerms does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
215
        $groups       = empty($_cachedPerms[$id]) ? [] : array_unique($_cachedPerms[$id]);
0 ignored issues
show
Coding Style introduced by
$_cachedPerms does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
216
        unset($_cachedPerms);
0 ignored issues
show
Coding Style introduced by
$_cachedPerms does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
217
218
        return $groups;
219
    }
220
221
    /**
222
     * @param  string $perm_name
223
     * @return array
224
     */
225
    public function createPermData($perm_name = 'forum_all')
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
226
    {
227
        global $xoopsModule;
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...
228
        /** @var \XoopsModuleHandler $moduleHandler */
229
        $perms = [];
230
231 View Code Duplication
        if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname')) {
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...
232
            $modid = $xoopsModule->getVar('mid');
233
        } else {
234
            $moduleHandler = xoops_getHandler('module');
235
            $module        = $moduleHandler->getByDirname('newbb');
236
            $modid         = $module->getVar('mid');
237
            unset($module);
238
        }
239
240
        if (in_array($perm_name, ['forum_all', 'category_all'], true)) {
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
241
            /** @var \XoopsMemberHandler $memberHandler */
242
            $memberHandler = xoops_getHandler('member');
243
            $groups        = array_keys($memberHandler->getGroupList());
244
245
            $type          = ('category_all' === $perm_name) ? 'category' : 'forum';
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
246
            $objectHandler = Newbb\Helper::getInstance()->getHandler($type);
247
            $object_ids    = $objectHandler->getIds();
0 ignored issues
show
Coding Style introduced by
$object_ids does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
248
            foreach ($object_ids as $item_id) {
0 ignored issues
show
Coding Style introduced by
$object_ids does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
249
                $perms[$perm_name][$item_id] = $groups;
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
250
            }
251
        } else {
252
            $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Unused Code introduced by
$gpermHandler 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...
253
            $criteria     = new \CriteriaCompo(new \Criteria('gperm_modid', $modid));
254
            if (!empty($perm_name) && 'forum_all' !== $perm_name && 'category_all' !== $perm_name) {
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
255
                $criteria->add(new \Criteria('gperm_name', $perm_name));
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
256
            }
257
            $permissions = $this->getObjects($criteria);
258
259
            foreach ($permissions as $gperm) {
260
                $item_id                                         = $gperm->getVar('gperm_itemid');
0 ignored issues
show
Coding Style introduced by
$item_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
261
                $group_id                                        = (int)$gperm->getVar('gperm_groupid');
0 ignored issues
show
Coding Style introduced by
$group_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
262
                $perms[$gperm->getVar('gperm_name')][$item_id][] = $group_id;
0 ignored issues
show
Coding Style introduced by
$item_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
263
            }
264
        }
265
        if (count($perms) > 0) {
266
            foreach (array_keys($perms) as $perm) {
267
                $this->cacheHelper->write("permission_{$perm}", $perms[$perm]);
268
            }
269
        }
270
        $ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
271
272
        return $ret;
273
    }
274
275
    /**
276
     * @param  string $perm_name
277
     * @return array|mixed|null
278
     */
279
    public function &loadPermData($perm_name = 'forum_access')
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
280
    {
281
        if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) {
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
282
            $perms = $this->createPermData($perm_name);
0 ignored issues
show
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
283
        }
284
285
        return $perms;
286
    }
287
288
    /**
289
     * @param       $perm
290
     * @param       $itemid
291
     * @param       $groupid
292
     * @param  null $mid
293
     * @return bool
294
     */
295
    public function validateRight($perm, $itemid, $groupid, $mid = null)
0 ignored issues
show
Coding Style introduced by
function validateRight() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
validateRight uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
296
    {
297 View Code Duplication
        if (empty($mid)) {
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...
298
            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
299
                $mid = $GLOBALS['xoopsModule']->getVar('mid');
300
            } else {
301
                /** @var \XoopsModuleHandler $moduleHandler */
302
                $moduleHandler = xoops_getHandler('module');
303
                $mod           = $moduleHandler->getByDirname('newbb');
304
                $mid           = $mod->getVar('mid');
305
                unset($mod);
306
            }
307
        }
308
        if ($this->myCheckRight($perm, $itemid, $groupid, $mid)) {
309
            return true;
310
        }
311
        $this->cacheHelper->delete('permission');
312
        $this->addRight($perm, $itemid, $groupid, $mid);
313
314
        return true;
315
    }
316
317
    /**
318
     * Check permission (directly)
319
     *
320
     * @param string $gperm_name   Name of permission
321
     * @param int    $gperm_itemid ID of an item
322
     * @param        int           /array $gperm_groupid A group ID or an array of group IDs
323
     * @param int    $gperm_modid  ID of a module
324
     *
325
     * @return bool TRUE if permission is enabled
326
     */
327
    public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
0 ignored issues
show
Coding Style introduced by
function myCheckRight() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
328
    {
329
        $ret      = false;
330
        $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid));
0 ignored issues
show
Coding Style introduced by
$gperm_modid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
331
        $criteria->add(new \Criteria('gperm_name', $gperm_name));
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
332
        $gperm_itemid = (int)$gperm_itemid;
0 ignored issues
show
Coding Style introduced by
$gperm_itemid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
333
        if ($gperm_itemid > 0) {
0 ignored issues
show
Coding Style introduced by
$gperm_itemid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
334
            $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid));
0 ignored issues
show
Coding Style introduced by
$gperm_itemid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
335
        }
336
        if (is_array($gperm_groupid)) {
0 ignored issues
show
Coding Style introduced by
$gperm_groupid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
337
            $criteria2 = new \CriteriaCompo();
338
            foreach ($gperm_groupid as $gid) {
0 ignored issues
show
Coding Style introduced by
$gperm_groupid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
339
                $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR');
340
            }
341
            $criteria->add($criteria2);
342
        } else {
343
            $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid));
0 ignored issues
show
Coding Style introduced by
$gperm_groupid does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
344
        }
345
        if ($this->getCount($criteria) > 0) {
346
            $ret = true;
347
        }
348
349
        return $ret;
350
    }
351
352
    /**
353
     * @param       $perm
354
     * @param       $itemid
355
     * @param       $groupid
356
     * @param  null $mid
357
     * @return bool
358
     */
359
    public function deleteRight($perm, $itemid, $groupid, $mid = null)
0 ignored issues
show
Coding Style introduced by
function deleteRight() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
deleteRight uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
360
    {
361
        $this->cacheHelper->delete('permission');
362 View Code Duplication
        if (null === $mid) {
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...
363
            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
364
                $mid = $GLOBALS['xoopsModule']->getVar('mid');
365
            } else {
366
                /** @var \XoopsModuleHandler $moduleHandler */
367
                $moduleHandler = xoops_getHandler('module');
368
                $mod           = $moduleHandler->getByDirname('newbb');
369
                $mid           = $mod->getVar('mid');
370
                unset($mod);
371
            }
372
        }
373
        if (is_callable([&$this->XoopsGroupPermHandler, 'deleteRight'])) {
374
            return parent::deleteRight($perm, $itemid, $groupid, $mid);
375
        } else {
376
            $criteria = new \CriteriaCompo(new \Criteria('gperm_name', $perm));
377
            $criteria->add(new \Criteria('gperm_groupid', $groupid));
378
            $criteria->add(new \Criteria('gperm_itemid', $itemid));
379
            $criteria->add(new \Criteria('gperm_modid', $mid));
380
            $permsObject = $this->getObjects($criteria);
381
            if (!empty($permsObject)) {
382
                foreach ($permsObject as $permObject) {
383
                    $this->delete($permObject);
384
                }
385
            }
386
            unset($criteria, $permsObject);
387
        }
388
389
        return true;
390
    }
391
392
    /**
393
     * @param        $forum
394
     * @param  int   $mid
395
     * @return mixed
396
     */
397
    public function applyTemplate($forum, $mid = 0)
398
    {
399
        $this->cacheHelper->delete('permission_forum');
400
        $handler = $this->loadHandler('forum');
401
402
        return $handler->applyTemplate($forum, $mid);
403
    }
404
405
    /**
406
     * @return mixed
407
     */
408
    public function getTemplate()
409
    {
410
        $handler  = $this->loadHandler('forum');
411
        $template = $handler->getTemplate();
412
413
        return $template;
414
    }
415
416
    /**
417
     * @param $perms
418
     * @return mixed
419
     */
420
    public function setTemplate($perms)
421
    {
422
        $handler = $this->loadHandler('forum');
423
424
        return $handler->setTemplate($perms);
425
    }
426
}
427