Passed
Push — master ( 9625ba...bbffc5 )
by Michael
02:24
created

AdminPermissions::getPermissionsSwitch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 2
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: AdminPermissions.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class AdminPermissions.
27
 */
28
class AdminPermissions extends TDMCreateFile
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...
29
{
30
    /**
31
     *  @public function constructor
32
     *
33
     *  @param null
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
    }
39
40
    /**
41
     *  @static function getInstance
42
     *
43
     *  @param null
44
     *
45
     *  @return AdminPermissions
46
     */
47
    public static function getInstance()
48
    {
49
        static $instance = false;
50
        if (!$instance) {
51
            $instance = new self();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     *  @public function write
59
     *
60
     *  @param string $module
61
     *  @param mixed $tables
62
     *  @param string $filename
63
     *
64
     *  @return string
65
     */
66
    public function write($module, $tables, $filename)
67
    {
68
        $this->setModule($module);
69
        $this->setTables($tables);
70
        $this->setFileName($filename);
71
    }
72
73
    /**
74
     * @private function getPermissionsHeader
75
     *
76
     * @param $module
77
     * @param $language
78
     *
79
     * @return string
80
     */
81
    private function getPermissionsHeader($module, $language)
82
    {
83
        $pc = TDMCreatePhpCode::getInstance();
84
        $xc = TDMCreateXoopsCode::getInstance();
85
        $cc = ClassXoopsCode::getInstance();
86
        $axc = AdminXoopsCode::getInstance();
87
        $moduleDirname = $module->getVar('mod_dirname');
88
        $tables = $this->getTableTables($module->getVar('mod_id'));
89
        foreach (array_keys($tables) as $t) {
90
            if (1 == $tables[$t]->getVar('table_permissions')) {
91
                $tableName = $tables[$t]->getVar('table_name');
92
            }
93
        }
94
        $ret = $this->getInclude('header');
95
        $ret .= $pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
96
        $ret .= $xc->getXoopsHandlerLine($moduleDirname, $tableName);
0 ignored issues
show
Bug introduced by
The variable $tableName 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...
97
        $ret .= $pc->getPhpCodeCommentLine('Check admin have access to this page');
98
        $ret .= $axc->getAdminTemplateMain($moduleDirname, 'permissions');
99
        $ret .= $xc->getXcTplAssign('navigation', "\$adminMenu->addNavigation('permissions.php')");
100
        $ret .= $xc->getXcXoopsRequest('op', 'op', 'global');
101
        $ret .= $xc->getXcLoad('XoopsFormLoader');
102
        $optionsSelect = [
103
            'global' => "{$language}PERMISSIONS_GLOBAL", 'approve' => "{$language}PERMISSIONS_APPROVE",
104
            'submit' => "{$language}PERMISSIONS_SUBMIT", 'view' => "{$language}PERMISSIONS_VIEW",
105
        ];
106
        $formSelect = $xc->getXoopsFormSelectExtraOptions('formSelect', '\'\'', 'op', $optionsSelect, 'onchange="document.fselperm.submit()"');
0 ignored issues
show
Documentation introduced by
'onchange="document.fselperm.submit()"' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
        $ret .= $cc->getXoopsSimpleForm('permTableForm', 'formSelect', $formSelect, '\'\'', 'fselperm', 'permissions');
108
109
        return $ret;
110
    }
111
112
    /**
113
     *  @private function getPermissionsSwitch
114
     *  @param $moduleDirname
115
     *  @param $language
116
     *
117
     *  @return string
118
     */
119
    private function getPermissionsSwitch($moduleDirname, $language)
120
    {
121
        $pc = TDMCreatePhpCode::getInstance();
122
        $cases = [
123
            'global'  => [
124
                "\$formTitle = {$language}PERMISSIONS_GLOBAL;",
125
                "\$permName = '{$moduleDirname}_ac';",
126
                "\$permDesc = {$language}PERMISSIONS_GLOBAL_DESC;",
127
                "\$globalPerms = array( '4' => {$language}PERMISSIONS_GLOBAL_4, '8' => {$language}PERMISSIONS_GLOBAL_8, '16' => {$language}PERMISSIONS_GLOBAL_16 );",
128
            ],
129
            'approve' => [
130
                "\$formTitle = {$language}PERMISSIONS_APPROVE;",
131
                "\$permName = '{$moduleDirname}_approve';",
132
                "\$permDesc = {$language}PERMISSIONS_APPROVE_DESC;",
133
            ],
134
            'submit'  => [
135
                "\$formTitle = {$language}PERMISSIONS_SUBMIT;",
136
                "\$permName = '{$moduleDirname}_submit';",
137
                "\$permDesc = {$language}PERMISSIONS_SUBMIT_DESC;",
138
            ],
139
            'view'    => [
140
                "\$formTitle = {$language}PERMISSIONS_VIEW;",
141
                "\$permName = '{$moduleDirname}_view';",
142
                "\$permDesc = {$language}PERMISSIONS_VIEW_DESC;",
143
            ],
144
        ];
145
146
        $contentSwitch = $pc->getPhpCodeCaseSwitch($cases, true, false, "\t");
147
148
        return $pc->getPhpCodeSwitch('op', $contentSwitch);
149
    }
150
151
    /**
152
     *  @private function getPermissionsBody
153
     *
154
     *  @param string $module
155
     *  @param string $language
156
     *
157
     *  @return string
158
     */
159
    private function getPermissionsBody($module, $language)
160
    {
161
        $pc = TDMCreatePhpCode::getInstance();
162
        $xc = TDMCreateXoopsCode::getInstance();
163
        $tables = $this->getTableTables($module->getVar('mod_id'));
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $module (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
164
        $tableName = '';
165
        foreach (array_keys($tables) as $t) {
166
            if (1 == $tables[$t]->getVar('table_permissions')) {
167
                $tableId = $tables[$t]->getVar('table_id');
168
                $tableMid = $tables[$t]->getVar('table_mid');
169
                $tableName = $tables[$t]->getVar('table_name');
170
            }
171
        }
172
        $ucfTableName = ucfirst($tableName);
0 ignored issues
show
Unused Code introduced by
$ucfTableName 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...
173
        $fields = $this->getTableFields($tableMid, $tableId);
0 ignored issues
show
Bug introduced by
The variable $tableMid 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...
Bug introduced by
The variable $tableId 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...
174
        $fieldId = 'id';
175
        $fieldMain = 'title';
176
        foreach (array_keys($fields) as $f) {
177
            $fieldName = $fields[$f]->getVar('field_name');
178
            if (0 == $f) {
179
                $fieldId = $fieldName;
180
            }
181
            if (1 == $fields[$f]->getVar('field_main')) {
182
                $fieldMain = $fieldName;
183
            }
184
        }
185
186
        $ret = $xc->getXcGetVar('moduleId', 'xoopsModule', 'mid');
187
        $ret .= $xc->getXcGroupPermForm('permform', '$formTitle', '$moduleId', '$permName', '$permDesc', "'admin/permissions.php'");
188
        $foreach1 = $xc->getXcAddItem('permform', '$gPermId', '$gPermName', "\t");
189
        $if1 = $pc->getPhpCodeForeach('globalPerms', false, 'gPermId', 'gPermName', $foreach1, "\t");
190
        $if1 .= $xc->getXcTplAssign('form', '$permform->render()', true, "\t");
191
        $else = $xc->getXcObjHandlerCount($tableName, "\t");
192
        $if2 = $xc->getXcObjHandlerAll($tableName, $fieldMain, 0, 0, "\t\t");
193
        $getVar1 = $xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldId, true);
194
        $getVar2 = $xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldMain, true);
195
        $foreach2 = $xc->getXcAddItem('permform', $getVar1, $getVar2, "\t")."\r";
196
        $if2 .= $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $foreach2, "\t\t");
197
        $if2 .= $xc->getXcTplAssign('form', '$permform->render()', true, "\t\t");
198
        $elseInter = $xc->getXcRedirectHeader($tableName, '?op=new', '3', "{$language}NO_PERMISSIONS_SET", true, "\t\t");
199
        $elseInter .= $this->getSimpleString("\t\texit();");
200
        $else .= $pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $if2, $elseInter, "\t");
201
202
        $ret .= $pc->getPhpCodeConditions('$op', ' == ', "'global'", $if1, $else);
203
        $ret .= $pc->getPhpCodeUnset('permform');
204
205
        return $ret;
206
    }
207
208
    /**
209
     *  @public function render
210
     *
211
     *  @param null
212
     *
213
     *  @return bool|string
214
     */
215
    public function render()
216
    {
217
        $module = $this->getModule();
218
        $filename = $this->getFileName();
219
        $moduleDirname = $module->getVar('mod_dirname');
220
        $language = $this->getLanguage($moduleDirname, 'AM');
221
        $content = $this->getHeaderFilesComments($module, $filename);
222
        $content .= $this->getPermissionsHeader($module, $language);
223
        $content .= $this->getPermissionsSwitch($moduleDirname, $language);
224
        $content .= $this->getPermissionsBody($module, $language);
225
        $content .= $this->getInclude('footer');
226
227
        $this->create($moduleDirname, 'admin', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
228
229
        return $this->renderFile();
230
    }
231
}
232