AlumniCategoryHandler   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubCatArray() 0 15 4
B getCategoriesCount() 0 20 6
C getCategoriesForSearch() 0 36 7
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
/**
13
 * Alumni module for Xoops
14
 *
15
 * @copyright       XOOPS Project https://xoops.org/
16
 * @license         GPL 2.0 or later
17
 * @package         alumni
18
 * @since           2.6.x
19
 * @author          John Mordo (jlm69)
20
 */
21
22
use Xoops\Core\Database\Connection;
23
use Xoops\Core\Kernel\XoopsObject;
24
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
25
use Xoops\Core\Kernel\Criteria;
26
use Xoops\Core\Kernel\CriteriaCompo;
27
28
/**
29
 * Class AlumniCategory
30
 */
31
class AlumniCategory extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    public $alumni = null;
34
35
    /**
36
     * @var array
37
     */
38
    public $_categoryPath = false;
0 ignored issues
show
Coding Style introduced by
$_categoryPath 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...
39
40
    //Constructor
41
    public function __construct()
42
    {
43
        $this->alumni = Alumni::getInstance();
44
        $this->initVar('cid', XOBJ_DTYPE_INT, null, false, 11);
45
        $this->initVar('pid', XOBJ_DTYPE_INT, null, false, 5);
46
        $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 255);
47
        $this->initVar('scaddress', XOBJ_DTYPE_TXTBOX, null, false, 255);
48
        $this->initVar('scaddress2', XOBJ_DTYPE_TXTBOX, null, false, 255);
49
        $this->initVar('sccity', XOBJ_DTYPE_TXTBOX, null, false, 255);
50
        $this->initVar('scstate', XOBJ_DTYPE_TXTBOX, null, false, 255);
51
        $this->initVar('sczip', XOBJ_DTYPE_TXTBOX, null, false, 255);
52
        $this->initVar('scphone', XOBJ_DTYPE_TXTBOX, null, false, 255);
53
        $this->initVar('scfax', XOBJ_DTYPE_TXTBOX, null, false, 255);
54
        $this->initVar('scmotto', XOBJ_DTYPE_TXTBOX, null, false, 255);
55
        $this->initVar('scurl', XOBJ_DTYPE_TXTBOX, null, false, 255);
56
        $this->initVar('img', XOBJ_DTYPE_TXTBOX, null, false, 255);
57
        $this->initVar('scphoto', XOBJ_DTYPE_TXTBOX, null, false, 255);
58
        $this->initVar('ordre', XOBJ_DTYPE_INT, null, false, 5);
59
    }
60
61
    /**
62
     * @param null|int   $id
63
     * @param string $path
64
     * @return string
65
     */
66
    public function getPathFromId($id = null, $path = '')
67
    {
68
        $id   = null !== $id ? (int)$id : $this->cid;
0 ignored issues
show
Unused Code introduced by
$id 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...
69
        $myts = MyTextSanitizer::getInstance();
70
        $name = $myts->htmlSpecialChars($this->title);
71
        $path = "/{$name}{$path}";
72
        if (0 != $this->pid) {
73
            $path = $this->getPathFromId($this->pid, $path);
74
        }
75
        return $path;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getGroups_view()
0 ignored issues
show
Coding Style introduced by
function getGroups_view() 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...
82
    {
83
        return $this->alumni->getPermissionHandler()->getGrantedGroupsById('alumni_view', $this->getVar('cid'));
84
    }
85
}
86
87
/**
88
 * Class AlumniCategoryHandler
89
 */
90
class AlumniCategoryHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
91
{
92
    public $alumni = null;
93
94
    /**
95
     * AlumniCategoryHandler constructor.
96
     * @param \Xoops\Core\Database\Connection|null $db
97
     */
98
    public function __construct(Connection $db = null)
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...
99
    {
100
        $this->alumni = Alumni::getInstance();
101
        parent::__construct($db, 'alumni_category', 'alumnicategory', 'cid', 'title');
102
    }
103
104
    /**
105
     * @param $by_cat
106
     * @param $level
107
     * @param $cat_array
108
     * @param $cat_result
109
     */
110
    public function getSubCatArray($by_cat, $level, $cat_array, $cat_result)
0 ignored issues
show
Coding Style introduced by
$by_cat 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...
111
    {
112
        global $theresult;
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...
113
        $spaces = '';
114
        for ($j = 0; $j < $level; ++$j) {
115
            $spaces .= '--';
116
        }
117
        $theresult[$by_cat['cid']] = $spaces . $by_cat['title'];
0 ignored issues
show
Coding Style introduced by
$by_cat 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...
118
        if (isset($cat_array[$by_cat['cid']])) {
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
119
            $level = ++$level;
120
            foreach ($cat_array[$by_cat['cid']] as $cat) {
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
121
                $this->getSubCatArray($cat, $level, $cat_array, $cat_result);
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
122
            }
123
        }
124
    }
125
126
    /**
127
     * @param int $pid
128
     * @return int
129
     */
130
    public function getCategoriesCount($pid = 0)
131
    {
132
        $xoops = Xoops::getInstance();
133
        if ($pid == -1) {
134
            return $this->getCount();
135
        }
136
        $criteria = new CriteriaCompo();
137
        if (null !== $pid && ($pid != -1)) {
138
            $criteria->add(new criteria('pid', $pid));
139
            if (!$xoops->userIsAdmin) {
140
                $categoriesGranted = $this->alumni->getPermissionHandler()->getGrantedItems('alumni_view');
141
                if (count($categoriesGranted) > 0) {
142
                    $criteria->add(new Criteria('cid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
143
                } else {
144
                    return 0;
145
                }
146
            }
147
        }
148
        return $this->getCount($criteria);
149
    }
150
151
    /**
152
     * @return array
153
     */
154
    public function &getCategoriesForSearch()
155
    {
156
        global $theresult, $xoops, $alumni;
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...
157
        $xoops    = Xoops::getInstance();
158
        $alumni   = Alumni::getInstance();
159
        $moduleId = $alumni->getModule()->mid();
160
        $ret      = [];
161
        $criteria = new CriteriaCompo();
162
        $criteria->setSort('cid');
163
        $criteria->setOrder('ASC');
164
        if (!$xoops->isAdmin()) {
165
            //          $gperm_handler        = $xoops->gethandler('groupperm');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
166
            $groups               = is_object($xoops->isUser()) ? $$xoops->isUser()->getGroups() : '3';
167
            $allowedCategoriesIds = $alumni->getGrouppermHandler()->getItemIds('alumni_view', $groups, $moduleId);
168
            $criteria->add(new Criteria('cid', '(' . implode(',', $allowedCategoriesIds) . ')', 'IN'));
169
        }
170
        $categories = $this->getAll($criteria, ['cid', 'pid', 'title'], false, false);
171
        if (0 == count($categories)) {
172
            return $ret;
173
        }
174
        $cat_array = [];
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
175
        foreach ($categories as $cat) {
176
            $cat_array[$cat['pid']][$cat['cid']] = $cat;
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
177
        }
178
179
        if (!isset($cat_array[0])) {
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
180
            return $ret;
181
        }
182
        $cat_result = [];
0 ignored issues
show
Coding Style introduced by
$cat_result 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...
183
        foreach ($cat_array[0] as $thecat) {
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
184
            $level = 0;
185
            $this->getSubCatArray($thecat, $level, $cat_array, $cat_result);
0 ignored issues
show
Coding Style introduced by
$cat_array 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...
186
        }
187
188
        return $theresult;
189
    }
190
}
191