Completed
Branch master (f9052b)
by Michael
02:32
created

ExtgalleryPublicCatHandler::_haveValidParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * ExtGallery Class Manager
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   {@link http://xoops.org/ XOOPS Project}
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Zoullou (http://www.zoullou.net)
15
 * @package     ExtGallery
16
 */
17
18
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
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...
19
20
include_once __DIR__ . '/catHandler.php';
21
22
/**
23
 * Class ExtgalleryPublicCat
24
 */
25
class ExtgalleryPublicCat extends ExtgalleryCat
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...
26
{
27
    /**
28
     * ExtgalleryPublicCat constructor.
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
}
35
36
/**
37
 * Class ExtgalleryPublicCatHandler
38
 */
39
class ExtgalleryPublicCatHandler extends ExtgalleryCatHandler
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...
40
{
41
    /**
42
     * ExtgalleryPublicCatHandler constructor.
43
     * @param XoopsDatabase $db
44
     */
45
    public function __construct(XoopsDatabase $db)
46
    {
47
        parent::__construct($db, 'public');
48
    }
49
50
    /**
51
     * @param $data
52
     *
53
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
54
     */
55
    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...
56
    {
57
        $cat = $this->create();
58
        $cat->setVars($data);
59
60
        if (!$this->_haveValidParent($cat)) {
61
            return false;
62
        }
63
64
        $this->insert($cat, true);
65
        $this->rebuild();
66
67
        $criteria = new CriteriaCompo();
68
        $criteria->setSort('cat_id');
69
        $criteria->setOrder('DESC');
70
        $criteria->setLimit(1);
71
72
        $cat =& $this->getObjects($criteria);
73
        $cat = $cat[0];
74
75
        $moduleId = $GLOBALS['xoopsModule']->getVar('mid');
0 ignored issues
show
Unused Code introduced by
$moduleId 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...
76
77
        // Retriving permission mask
78
        /** @var XoopsGroupPermHandler $gpermHandler */
79
        $gpermHandler = xoops_getHandler('groupperm');
80
        $moduleId     = $GLOBALS['xoopsModule']->getVar('mid');
81
        $groups       = $GLOBALS['xoopsUser']->getGroups();
82
83
        $criteria = new CriteriaCompo();
84
        $criteria->add(new Criteria('gperm_name', 'extgallery_public_mask'));
85
        $criteria->add(new Criteria('gperm_modid', $moduleId));
86
        $permMask = $gpermHandler->getObjects($criteria);
87
88
        // Retriving group list
89
        $memberHandler = xoops_getHandler('member');
90
        $glist         = $memberHandler->getGroupList();
91
92
        // Applying permission mask
93
        $permArray       = include XOOPS_ROOT_PATH . '/modules/extgallery/include/perm.php';
94
        $modulePermArray = $permArray['modulePerm'];
95
        $pluginPermArray = $permArray['pluginPerm'];
96
97
        foreach ($permMask as $perm) {
98 View Code Duplication
            foreach ($modulePermArray as $permMask) {
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...
99
                if ($perm->getVar('gperm_itemid') == $permMask['maskId']) {
100
                    $gpermHandler->addRight($permMask['name'], $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
101
                }
102
            }
103
104 View Code Duplication
            foreach ($pluginPermArray as $permMask) {
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...
105
                if ($perm->getVar('gperm_itemid') == $permMask['maskId']) {
106
                    $gpermHandler->addRight($permMask['name'], $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
107
                }
108
            }
109
        }
110
    }
111
112
    /**
113
     * @param XoopsObject $cat
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cat not be null|XoopsObject?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
114
     *
115
     * @return bool
116
     */
117
    public function _haveValidParent(XoopsObject $cat = null)
118
    {
119
        // Check if haven't photo in parent category (parent category isn't an album)
120
        $parentCat = $this->get($cat->getVar('cat_pid'));
121
122
        return !$this->_isAlbum($parentCat);
123
    }
124
125
    /**
126
     * @return ExtgalleryPublicPermHandler
127
     */
128
    public function _getPermHandler()
129
    {
130
        return ExtgalleryPublicPermHandler::getInstance();
131
    }
132
}
133