Completed
Branch master (55a138)
by Michael
03:21
created

CategoryHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 179
Duplicated Lines 17.88 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 32
loc 179
rs 10
c 0
b 0
f 0
wmc 21
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B createCat() 16 40 6
A modifyCat() 0 7 1
A deleteCat() 0 7 1
A getCat() 0 14 3
A getAllCat() 0 9 2
A getAllCatById() 0 16 3
A _addCatPermCriteria() 16 16 3
A haveSubmitRight() 0 4 1

How to fix   Duplicated Code   

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:

1
<?php namespace XoopsModules\Extcal;
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    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      extcal
16
 * @since
17
 * @author       XOOPS Development Team,
18
 */
19
20
use XoopsModules\Extcal;
21
22
// 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...
23
24
// // require_once __DIR__ . '/ExtcalPersistableObjectHandler.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
25
//require_once __DIR__ . '/perm.php';
26
//require_once __DIR__ . '/time.php';
27
28
29
/**
30
 * Class CategoryHandler.
31
 */
32
class CategoryHandler extends ExtcalPersistableObjectHandler
33
{
34
    public $_extcalPerm;
35
36
    /**
37
     * @param $db
38
     */
39
    public function __construct(\XoopsDatabase $db)
40
    {
41
        $this->_extcalPerm = Extcal\Perm::getHandler();
42
//        parent::__construct($db, 'extcal_cat', _EXTCAL_CLN_CAT, 'cat_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
43
        parent::__construct($db, 'extcal_cat', Category::class, 'cat_id');
44
    }
45
46
    /**
47
     * @param $data
48
     *
49
     * @return bool
50
     */
51
    public function createCat($data)
0 ignored issues
show
Coding Style introduced by
createCat 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...
52
    {
53
        $cat = $this->create();
54
        $cat->setVars($data);
55
        $this->insert($cat);
56
57
        $catId = $this->getInsertId();
0 ignored issues
show
Unused Code introduced by
$catId 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...
58
59
        // Retriving permission mask
60
        /** @var XoopsGroupPermHandler $groupPermissionHandler */
61
        $groupPermissionHandler = xoops_getHandler('groupperm');
62
        $moduleId               = $GLOBALS['xoopsModule']->getVar('mid');
63
64
        $criteria =  new \CriteriaCompo();
65
        $criteria->add( new \Criteria('gperm_name', 'extcal_perm_mask'));
66
        $criteria->add( new \Criteria('gperm_modid', $moduleId));
67
        $permMask = $groupPermissionHandler->getObjects($criteria);
68
69
        // Retriving group list
70
        $memberHandler = xoops_getHandler('member');
71
        $glist         = $memberHandler->getGroupList();
0 ignored issues
show
Unused Code introduced by
$glist 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...
72
73
        // Applying permission mask
74
        foreach ($permMask as $perm) {
75 View Code Duplication
            if (1 == $perm->getVar('gperm_itemid')) {
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...
76
                $groupPermissionHandler->addRight('extcal_cat_view', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
77
            }
78 View Code Duplication
            if (2 == $perm->getVar('gperm_itemid')) {
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...
79
                $groupPermissionHandler->addRight('extcal_cat_submit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
80
            }
81 View Code Duplication
            if (4 == $perm->getVar('gperm_itemid')) {
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...
82
                $groupPermissionHandler->addRight('extcal_cat_autoapprove', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
83
            }
84 View Code Duplication
            if (8 == $perm->getVar('gperm_itemid')) {
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...
85
                $groupPermissionHandler->addRight('extcal_cat_edit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
86
            }
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * @param $catId
94
     * @param $data
95
     *
96
     * @return bool
97
     */
98
    public function modifyCat($catId, $data)
99
    {
100
        $cat = $this->get($catId);
101
        $cat->setVars($data);
102
103
        return $this->insert($cat);
104
    }
105
106
    /**
107
     * @param $catId
108
     */
109
    public function deleteCat($catId)
110
    {
111
        /* TODO :
112
           - Delete all events in this category
113
          */
114
        $this->deleteById($catId);
115
    }
116
117
    // Return one cat selected by his id
118
119
    /**
120
     * @param      $catId
121
     * @param bool $skipPerm
122
     *
123
     * @return bool
124
     */
125
    public function getCat($catId, $skipPerm = false)
0 ignored issues
show
Coding Style introduced by
getCat 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...
126
    {
127
        $criteriaCompo =  new \CriteriaCompo();
128
        $criteriaCompo->add( new \Criteria('cat_id', $catId));
129
        if (!$skipPerm) {
130
            $this->_addCatPermCriteria($criteriaCompo, $GLOBALS['xoopsUser']);
131
        }
132
        $ret = $this->getObjects($criteriaCompo);
133
        if (isset($ret[0])) {
134
            return $ret[0];
135
        } else {
136
            return false;
137
        }
138
    }
139
140
    /**
141
     * @param        $user
142
     * @param string $perm
143
     *
144
     * @return array
145
     */
146
    public function getAllCat($user, $perm = 'extcal_cat_view')
147
    {
148
        $criteriaCompo =  new \CriteriaCompo();
149
        if ('all' !== $perm) {
150
            $this->_addCatPermCriteria($criteriaCompo, $user, $perm);
151
        }
152
153
        return $this->getObjects($criteriaCompo);
154
    }
155
156
    /**
157
     * @param        $user
158
     * @param string $perm
159
     *
160
     * @return array
161
     */
162
    public function getAllCatById($user, $perm = 'all')
163
    {
164
        $criteriaCompo =  new \CriteriaCompo();
165
        if ('all' !== $perm) {
166
            $this->_addCatPermCriteria($criteriaCompo, $user, $perm);
167
        }
168
169
        $t = $this->objectToArray($this->getObjects($criteriaCompo));
170
        $r = [];
171
        //        while (list($k, $v) = each($t)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
172
        foreach ($t as $k => $v) {
173
            $r[$v['cat_id']] = $v;
174
        }
175
176
        return $r;
177
    }
178
179
    /**
180
     * @param   \CriteriaElement     $criteria
181
     * @param        $user
182
     * @param string $perm
183
     */
184 View Code Duplication
    public function _addCatPermCriteria(\CriteriaElement $criteria, $user, $perm = 'extcal_cat_view')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
185
    {
186
        $authorizedAccessCats = $this->_extcalPerm->getAuthorizedCat($user, 'extcal_cat_view');
187
        $count                = count($authorizedAccessCats);
188
        if ($count > 0) {
189
            $in = '(' . $authorizedAccessCats[0];
190
            array_shift($authorizedAccessCats);
191
            foreach ($authorizedAccessCats as $authorizedAccessCat) {
192
                $in .= ',' . $authorizedAccessCat;
193
            }
194
            $in .= ')';
195
            $criteria->add( new \Criteria('cat_id', $in, 'IN'));
196
        } else {
197
            $criteria->add( new \Criteria('cat_id', '(0)', 'IN'));
198
        }
199
    }
200
201
    /**
202
     * @param XoopsUser|string $xoopsUser
203
     *
204
     * @return bool
205
     */
206
    public function haveSubmitRight(&$xoopsUser)
207
    {
208
        return count($this->_extcalPerm->getAuthorizedCat($xoopsUser, 'extcal_cat_submit')) > 0;
209
    }
210
}
211