Completed
Push — master ( 454ebd...de22fe )
by Michael
03:58
created

PermissionHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
D getPermissions() 0 34 10
1
<?php namespace XoopsModules\Smartfaq;
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Credits: Mithrandir
7
 * Licence: GNU
8
 */
9
10
use XoopsModules\Smartfaq;
11
12
/**
13
 * Class PermissionHandler
14
 * @package XoopsModules\Smartfaq
15
 */
16
class PermissionHandler extends \XoopsObjectHandler
17
{
18
    /*
19
    * Returns permissions for a certain type
20
    *
21
    * @param string $type "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
22
    * @param int $id id of the item (forum, topic or possibly post) to get permissions for
23
    *
24
    * @return array
25
    */
26
    /**
27
     * @param  string $type
28
     * @param  null   $id
29
     * @return array
30
     */
31
    public function getPermissions($type = 'category', $id = null)
32
    {
33
        global $xoopsUser;
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...
34
        static $permissions;
35
36
        if (!isset($permissions[$type]) || (null !== $id && !isset($permissions[$type][$id]))) {
37
            $smartModule = Smartfaq\Utility::getModuleInfo();
38
            //Get group permissions handler
39
            $gpermHandler = xoops_getHandler('groupperm');
40
            //Get user's groups
41
            $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : [XOOPS_GROUP_ANONYMOUS];
42
43
            switch ($type) {
44
                case 'category':
45
                    $gperm_name = 'category_read';
46
                    break;
47
48
                case 'item':
49
                    $gperm_name = 'item_read';
50
                    break;
51
52
                case 'moderation':
53
                    $gperm_name = 'category_moderation';
54
                    $groups     = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
55
            }
56
57
            //Get all allowed item ids in this module and for this user's groups
58
            $userpermissions    = $gpermHandler->getItemIds($gperm_name, $groups, $smartModule->getVar('mid'));
0 ignored issues
show
Bug introduced by
The variable $gperm_name 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...
59
            $permissions[$type] = $userpermissions;
60
        }
61
62
        //Return the permission array
63
        return isset($permissions[$type]) ? $permissions[$type] : [];
64
    }
65
}
66