Completed
Pull Request — master (#51)
by Gino
06:03
created

UserBroken::getUserBrokenSwitch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %
Metric Value
dl 11
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
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 29 and the first side effect is on line 24.

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
/*
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       The XOOPS Project http://sourceforge.net/projects/xoops/
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: UserBroken.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserBroken.
28
 */
29
class UserBroken 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...
30
{
31
    /*
32
     *  @public function constructor
33
     *  @param null
34
     */
35
    /**
36
     *
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
0 ignored issues
show
Bug introduced by
The property xoopscode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Bug introduced by
The property phpcode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        $this->usercode = UserXoopsCode::getInstance();
0 ignored issues
show
Bug introduced by
The property usercode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
    }
45
46
    /*
47
     *  @static function &getInstance
48
     *  @param null
49
     */
50
    /**
51
     * @return UserBroken
52
     */
53
    public static function &getInstance()
54
    {
55
        static $instance = false;
56
        if (!$instance) {
57
            $instance = new self();
58
        }
59
60
        return $instance;
61
    }
62
63
    /*
64
     *  @public function write
65
     *  @param $module
66
     *  @param mixed $table
67
     *  @param $filename
68
     */
69
    /**
70
     * @param $module
71
     * @param $table
72
     * @param $filename
73
     */
74
    public function write($module, $table, $filename)
75
    {
76
        $this->setModule($module);
77
        $this->setTable($table);
78
        $this->setFileName($filename);
79
    }
80
81
    /*
82
     *  @public function getUserBrokenHeader
83
     *  @param null
84
     */
85
    /**
86
     * @param $moduleDirname
87
     *
88
     * @return
89
     */
90
    public function getUserBrokenHeader($moduleDirname, $fields)
91
    {
92
        $fieldId = $this->xoopscode->getXoopsCodeSaveFieldId($fields);
93
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
94
        $ret = $this->getInclude();
95
        $ret .= $this->xoopscode->getXoopsCodeXoopsRequest('op', 'op', 'list');
96
        $ret .= $this->xoopscode->getXoopsCodeXoopsRequest("{$ccFieldId}", "{$fieldId}", '', 'Int');
97
        $ret .= $this->getCommentLine('Template');
98
        $ret .= $this->usercode->getUserTplMain($moduleDirname, 'broken');
99
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
100
        $ret .= $this->xoopscode->getXoopsCodeAddStylesheet();
101
        $ret .= $this->getCommentLine('Redirection if not permissions');
102
        $condIf = $this->xoopscode->getXoopsCodeRedirectHeader('index', '', '2', '_NOPERM');
103
        $condIf .= $this->getSimpleString('exit();');
104
        $ret .= $this->phpcode->getPhpCodeConditions('$permSubmit', ' == ', 'false', $condIf, false);
105
106
        return $ret;
107
    }
108
109
    /*
110
     *  @public function getAdminPagesList
111
      *  @param $tableName
112
      *  @param $language     
113
      *
114
      *  @return
115
      */
116
    public function getUserBrokenForm($tableName, $language)
117
    {
118
        $ret = $this->getCommentLine('Mavigation');
119
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$navigation', "{$language}SUBMIT_PROPOSER");
120
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('navigation', '$navigation');
121
        $ret .= $this->getCommentLine('Title of page');
122
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$title', "{$language}SUBMIT_PROPOSER . '&nbsp;-&nbsp;'");
123
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$title.', "\$GLOBALS['xoopsModule']->name()");
124
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('xoops_pagetitle', '$title');
125
        $ret .= $this->getCommentLine('Description');
126
        $ret .= $this->usercode->getUserAddMeta('description', $language, 'SUBMIT_PROPOSER');
127
        $ret .= $this->getCommentLine('Form Create');
128
        $ret .= $this->xoopscode->getXoopsCodeObjHandlerCreate($tableName);
129
        $ret .= $this->xoopscode->getXoopsCodeGetForm('form', $tableName, 'Obj');
130
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('form', '$form->render()');
131
132
        return $ret;
133
    }
134
135
    /*
136
     *  @public function getUserBrokenSave
137
     *  @param $moduleDirname
138
     *  @param $tableName
139
     */
140
    /**
141
     * @param $moduleDirname
142
     * @param $table_id
143
     * @param $tableName
144
     *
145
     * @return
146
     */
147
    public function getUserBrokenSave($moduleDirname, $fields, $tableName, $language)
148
    {
149
        $fieldId = $this->xoopscode->getXoopsCodeSaveFieldId($fields);
0 ignored issues
show
Unused Code introduced by
$fieldId 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...
150
        $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...
151
        $ret = $this->phpcode->getPhpCodeCommentLine('Security Check');
152
        $xoopsSecurityCheck = $this->xoopscode->getXoopsCodeSecurityCheck();
153
        $securityError = $this->xoopscode->getXoopsCodeSecurityErrors();
154
        $implode = $this->phpcode->getPhpCodeImplode(',', $securityError);
155
        $redirectError = $this->xoopscode->getXoopsCodeRedirectHeader($tableName, '', '3', $implode);
156
        $ret .= $this->phpcode->getPhpCodeConditions($xoopsSecurityCheck, '', '', $redirectError, false, "\t");
157
        $ret .= $this->xoopscode->getXoopsCodeObjHandlerCreate($tableName);
158
159
        $ret .= $this->getSimpleString('$error = false;');
160
        $ret .= $this->getSimpleString("\$errorMessage = '';");
161
        $ret .= $this->getCommentLine('Test first the validation');
162
        $ret .= $this->xoopscode->getXoopsCodeLoad('captcha');
163
        $ret .= $this->xoopscode->getXoopsCodeXoopsCaptcha();
164
165
        $ret .= $this->phpcode->getPhpCodeConditions('!$xoopsCaptcha->verify()', '', '', "\$errorMessage .= \$xoopsCaptcha->getMessage().'<br>';\n\$error = true;\n", false, "\t");
166
167
        $ret .= $this->xoopscode->getXoopsCodeSaveElements($moduleDirname, $tableName, $fields);
168
169
        $condElse = $this->getCommentLine('Insert Data');
170
        $insert = $this->xoopscode->getXoopsCodeInsert($tableName, $tableName, 'Obj', true);
171
        $redirctHeader = $this->xoopscode->getXoopsCodeRedirectHeader('index', '', '2', "{$language}FORM_OK");
172
        $condElse .= $this->phpcode->getPhpCodeConditions($insert, '', '', $redirctHeader, false, "\t\t");
173
        $assigne = $this->xoopscode->getXoopsCodeTplAssign('error_message', '$errorMessage');
174
        $ret .= $this->phpcode->getPhpCodeConditions('$error', ' == ', 'true', $assigne, $condElse, "\t");
175
176
        $ret .= $this->getCommentLine('Get Form Error');
177
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
178
        $ret .= $this->xoopscode->getXoopsCodeGetForm('form', $tableName, 'Obj');
179
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('form', '$form->display()');
180
181
        return $ret;
182
    }
183
184
    /*
185
      *  @private function getUserSubmitSwitch
186
      *  @param $moduleDirname
187
     *  @param $tableName
188
     *  @param $tableSoleName
189
     *  @param $language
190
      *
191
      * @return
192
      */
193 View Code Duplication
    private function getUserBrokenSwitch($moduleDirname, $tableName, $language)
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...
194
    {
195
        $table = $this->getTable();
196
        $tableId = $table->getVar('table_id');
197
        $tableMid = $table->getVar('table_mid');
198
        $fields = $this->getTableFields($tableMid, $tableId);
199
        $cases = array('form' => array($this->getUserBrokenForm($tableName, $language)),
200
                    'save' => array($this->getUserBrokenSave($moduleDirname, $fields, $tableName, $language)), );
201
202
        return $this->xoopscode->getXoopsCodeSwitch('op', $cases, true);
203
    }
204
205
    /*
206
     *  @public function render
207
     *  @param null
208
     */
209
    /**
210
     * @return bool|string
211
     */
212
    public function render()
213
    {
214
        $module = $this->getModule();
215
        $table = $this->getTable();
216
        $filename = $this->getFileName();
217
        $moduleDirname = $module->getVar('mod_dirname');
218
        $tableId = $table->getVar('table_id');
219
        $tableMid = $table->getVar('table_mid');
220
        $tableName = $table->getVar('table_name');
221
        $fields = $this->getTableFields($tableMid, $tableId);
222
        $language = $this->getLanguage($moduleDirname, 'MA');
223
        $content = $this->getHeaderFilesComments($module, $filename);
224
        $content .= $this->getUserBrokenHeader($moduleDirname, $fields);
225
        $content .= $this->getUserBrokenSwitch($moduleDirname, $tableName, $language);
226
        $content .= $this->getInclude('footer');
227
228
        $this->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
0 ignored issues
show
Documentation introduced by
_AM_TDMCREATE_FILE_CREATED 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...
Documentation introduced by
_AM_TDMCREATE_FILE_NOTCREATED 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...
229
230
        return $this->renderFile();
231
    }
232
}
233