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

Category::getCategoryPath()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 12
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartfaq;
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
use \XoopsModules\Smartfaq;
10
11
// defined('XOOPS_ROOT_PATH') || die('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...
12
13
/**
14
 * Class Category
15
 * @package XoopsModules\Smartfaq
16
 */
17
class Category extends \XoopsObject
18
{
19
    /**
20
     * @var array
21
     * @access private
22
     */
23
    private $groups_read = null;
24
25
    /**
26
     * @var array
27
     * @access private
28
     */
29
    private $groups_admin = null;
0 ignored issues
show
Unused Code introduced by
The property $groups_admin is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    /**
32
     * constructor
33
     * @param null $id
34
     */
35
    public function __construct($id = null)
36
    {
37
        $this->db = \XoopsDatabaseFactory::getDatabaseConnection();
38
        $this->initVar('categoryid', XOBJ_DTYPE_INT, null, false);
39
        $this->initVar('parentid', XOBJ_DTYPE_INT, null, false);
40
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
41
        $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255);
42
        $this->initVar('total', XOBJ_DTYPE_INT, 1, false);
43
        $this->initVar('weight', XOBJ_DTYPE_INT, 1, false);
44
        $this->initVar('created', XOBJ_DTYPE_INT, null, false);
45
        $this->initVar('last_faq', XOBJ_DTYPE_INT);
46
47
        //not persistent values
48
        $this->initVar('faqcount', XOBJ_DTYPE_INT, 0, false);
49
        $this->initVar('last_faqid', XOBJ_DTYPE_INT);
50
        $this->initVar('last_question_link', XOBJ_DTYPE_TXTBOX);
51
52
        if (null !== $id) {
53
            if (is_array($id)) {
54
                $this->assignVars($id);
55
            } else {
56
                /** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */
57
                $categoryHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category');
58
                $category        = $categoryHandler->get($id);
59
                foreach ($category->vars as $k => $v) {
60
                    $this->assignVar($k, $v['value']);
61
                }
62
                $this->assignOtherProperties();
63
            }
64
        }
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function notLoaded()
71
    {
72
        return (-1 == $this->getVar('categoryid'));
73
    }
74
75
    public function assignOtherProperties()
76
    {
77
        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...
78
        $smartModule = Smartfaq\Utility::getModuleInfo();
79
        $module_id   = $smartModule->getVar('mid');
80
81
        $gpermHandler = xoops_getHandler('groupperm');
82
83
        $this->groups_read = $gpermHandler->getGroupIds('category_read', $this->categoryid(), $module_id);
84
    }
85
86
    /**
87
     * @return bool
88
     */
89 View Code Duplication
    public function checkPermission()
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...
90
    {
91
//        require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
92
93
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
94
        if ($userIsAdmin) {
95
            return true;
96
        }
97
98
        /** @var \XoopsModules\Smartfaq\PermissionHandler $smartPermHandler */
99
        $smartPermHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Permission');
100
101
        $categoriesGranted = $smartPermHandler->getPermissions('category');
102
        if (in_array($this->categoryid(), $categoriesGranted)) {
103
            $ret = true;
104
        }
105
106
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret 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...
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function categoryid()
113
    {
114
        return $this->getVar('categoryid');
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function parentid()
121
    {
122
        return $this->getVar('parentid');
123
    }
124
125
    /**
126
     * @param  string $format
127
     * @return mixed
128
     */
129 View Code Duplication
    public function name($format = 'S')
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...
130
    {
131
        $ret = $this->getVar('name', $format);
132
        if (('s' === $format) || ('S' === $format) || ('show' === $format)) {
133
            $myts = \MyTextSanitizer::getInstance();
134
            $ret  = $myts->displayTarea($ret);
135
        }
136
137
        return $ret;
138
    }
139
140
    /**
141
     * @param  string $format
142
     * @return mixed
143
     */
144
    public function description($format = 'S')
145
    {
146
        return $this->getVar('description', $format);
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152
    public function weight()
153
    {
154
        return $this->getVar('weight');
155
    }
156
157
    /**
158
     * @param  bool $withAllLink
159
     * @param  bool $open
160
     * @return mixed|string
161
     */
162
    public function getCategoryPath($withAllLink = false, $open = false)
163
    {
164
        $filename = 'category.php';
165
        if (false !== $open) {
166
            $filename = 'open_category.php';
167
        }
168
        if ($withAllLink) {
169
            $ret = "<a href='" . XOOPS_URL . '/modules/smartfaq/' . $filename . '?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
170
        } else {
171
            $ret = $this->name();
172
        }
173
        $parentid        = $this->parentid();
174
        /** @var Smartfaq\CategoryHandler $categoryHandler */
175
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
176
        if (0 != $parentid) {
177
            $parentObj = $categoryHandler->get($parentid);
178
            if ($parentObj->notLoaded()) {
179
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method getCategoryPath() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
180
            }
181
            $parentid = $parentObj->parentid();
0 ignored issues
show
Unused Code introduced by
$parentid 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...
182
            $ret      = $parentObj->getCategoryPath(true, $open) . ' > ' . $ret;
183
        }
184
185
        return $ret;
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public function getGroups_read()
192
    {
193
//        if(count($this->groups_read) < 1) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
194
        if (!is_array($this->groups_read)) {
195
            $this->assignOtherProperties();
196
        }
197
198
        return $this->groups_read;
199
    }
200
201
    /**
202
     * @param array $groups_read
203
     */
204
    public function setGroups_read($groups_read = ['0'])
205
    {
206
        $this->groups_read = $groups_read;
207
    }
208
209
    /**
210
     * @param  bool $sendNotifications
211
     * @param  bool $force
212
     * @return bool
213
     */
214
    public function store($sendNotifications = true, $force = true)
215
    {
216
//        $categoryHandler = new sfCategoryHandler($this->db);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
217
        /** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */
218
        $categoryHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category');
219
220
        $ret = $categoryHandler->insert($this, $force);
221
        if ($sendNotifications && $ret && $this->isNew()) {
222
            $this->sendNotifications();
223
        }
224
        $this->unsetNew();
225
226
        return $ret;
227
    }
228
229
    public function sendNotifications()
230
    {
231
        $smartModule = Smartfaq\Utility::getModuleInfo();
232
233
        $myts                = \MyTextSanitizer::getInstance();
234
        $notificationHandler = xoops_getHandler('notification');
0 ignored issues
show
Unused Code introduced by
$notificationHandler 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...
235
236
        $tags                  = [];
237
        $tags['MODULE_NAME']   = $myts->htmlSpecialChars($smartModule->getVar('name'));
238
        $tags['CATEGORY_NAME'] = $this->name();
239
        $tags['CATEGORY_URL']  = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/category.php?categoryid=' . $this->categoryid();
240
241
        $notificationHandler = xoops_getHandler('notification');
242
        $notificationHandler->triggerEvent('global_faq', 0, 'category_created', $tags);
243
    }
244
245
    /**
246
     * @param  array $category
247
     * @param  bool  $open
248
     * @return array
249
     */
250
    public function toArray($category = [], $open = false)
251
    {
252
        $category['categoryid'] = $this->categoryid();
253
        $category['name']       = $this->name();
254
        if (false !== $open) {
255
            $category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/open_category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
256
        } else {
257
            $category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
258
        }
259
        $category['total']       = $this->getVar('faqcount');
260
        $category['description'] = $this->description();
261
262
        if ($this->getVar('last_faqid') > 0) {
263
            $category['last_faqid']         = $this->getVar('last_faqid', 'n');
264
            $category['last_question_link'] = $this->getVar('last_question_link', 'n');
265
        }
266
267
        return $category;
268
    }
269
}
270