Passed
Push — master ( 827974...c305a5 )
by Michael
04:03
created

class/CategoriesHandler.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * Module: Adslight
19
 *
20
 * @category        Module
21
 * @author          XOOPS Development Team <https://xoops.org>
22
 * @copyright       {@link https://xoops.org/ XOOPS Project}
23
 * @license         GPL 2.0 or later
24
 */
25
26
27
28
/**
29
 * Class CategoriesHandler
30
 */
31
class CategoriesHandler extends \XoopsPersistableObjectHandler
32
{
33
    /**
34
     * @var XoopsModules\Adslight\Helper
0 ignored issues
show
The type XoopsModules\Adslight\XoopsModules\Adslight\Helper was not found. Did you mean XoopsModules\Adslight\Helper? If so, make sure to prefix the type with \.
Loading history...
35
     */
36
    public $helper;
37
38
    /**
39
     * Constructor
40
     * @param null|\XoopsDatabase                $db
41
     * @param null|Helper $helper
42
     */
43
44
    public function __construct(\XoopsDatabase $db = null, $helper = null)
45
    {
46
        /** @var Helper $this- >helper */
47
        $this->helper = $helper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $helper can also be of type XoopsModules\Adslight\Helper. However, the property $helper is declared as type XoopsModules\Adslight\XoopsModules\Adslight\Helper. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
        parent::__construct($db, 'adslight_categories', Categories::class, 'cid', 'title');
49
    }
50
51
    /**
52
     * @param bool $isNew
53
     *
54
     * @return \XoopsObject
55
     */
56
    public function create($isNew = true): \XoopsObject
57
    {
58
        $obj         = parent::create($isNew);
59
        $obj->helper = $this->helper;
60
61
        return $obj;
62
    }
63
64
65
    //====================================
66
67
    /**
68
     * @param int $pid
69
     *
70
     * @return int
71
     */
72
    public function getCategoriesCount($pid = 0): int
73
    {
74
        if (-1 == $pid) {
75
            return $this->getCount();
76
        }
77
        $helper   = Helper::getInstance();
78
        $criteria = new \CriteriaCompo();
79
        if (isset($pid) && (-1 != $pid)) {
80
            $criteria->add(new \Criteria('pid', $pid));
81
            if (!$helper->isUserAdmin()) {
82
                $categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
83
                if (\count($categoriesGranted) > 0) {
84
                    $criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
85
                } else {
86
                    return 0;
87
                }
88
                //                if (\is_object($GLOBALS['xoopsUser'])) {
89
                //                    $criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
90
                //                }
91
            }
92
        }
93
94
        return $this->getCount($criteria);
95
    }
96
97
    /**
98
     * Get all subcats and put them in an array indexed by parent id
99
     *
100
     * @param array $categories
101
     *
102
     * @return array
103
     */
104
    public function getSubCats($categories): array
105
    {
106
        $helper   = Helper::getInstance();
107
        $criteria = new \CriteriaCompo(new \Criteria('pid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN'));
108
        $ret      = [];
109
        if (!$helper->isUserAdmin()) {
110
            $categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
111
            if (\count($categoriesGranted) > 0) {
112
                $criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
113
            } else {
114
                return $ret;
115
            }
116
117
            if (\is_object($GLOBALS['xoopsUser'])) {
118
                $criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
119
            }
120
        }
121
        $criteria->setSort('weight');
122
        $criteria->order = 'ASC'; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method
123
        $subcats         = $this->getObjects($criteria, true);
124
        /** @var Categories $subcat */
125
        foreach ($subcats as $subcat) {
126
            $ret[$subcat->getVar('pid')][$subcat->getVar('cid')] = $subcat;
127
        }
128
129
        return $ret;
130
    }
131
}
132