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

UserSubmit   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 216
Duplicated Lines 13.89 %

Coupling/Cohesion

Components 2
Dependencies 4
Metric Value
wmc 10
lcom 2
cbo 4
dl 30
loc 216
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getInstance() 0 9 2
A write() 0 6 1
A getUserSubmitHeader() 0 19 1
A getUserSubmitForm() 0 22 1
B getUserSubmitSave() 0 25 1
A getUserSubmitFooter() 0 9 1
A getUserSubmitSwitch() 11 11 1
A render() 19 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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: UserSubmit.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserSubmit.
28
 */
29
class UserSubmit 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 UserSubmit
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 string $module
66
    *  @param mixed $table
67
    *  @param string $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 getUserSubmitHeader    
83
     * @param $moduleDirname
84
     *
85
     * @return string
86
     */
87
    public function getUserSubmitHeader($moduleDirname)
88
    {
89
        $ret = $this->getInclude();
90
        $ret .= $this->xoopscode->getXoopsCodeLoadLanguage('admin');
91
        $ret .= $this->getCommentLine('It recovered the value of argument op in URL$');
92
        $ret .= $this->xoopscode->getXoopsCodeXoopsRequest('op', 'op', 'form');
93
        $ret .= $this->getCommentLine('Template');
94
        $ret .= $this->usercode->getUserTplMain($moduleDirname, 'submit');
95
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
96
        $ret .= $this->xoopscode->getXoopsCodeAddStylesheet();
97
        $ret .= "\$permSubmit = (\$gpermHandler->checkRight('{$moduleDirname}_ac', 4, \$groups, \$GLOBALS['xoopsModule']->getVar('mid'))) ? true : false;\n";
98
        $ret .= $this->getCommentLine('Redirection if not permissions');
99
        $condIf = $this->xoopscode->getXoopsCodeRedirectHeader('index', '', '2', '_NOPERM');
100
        $condIf .= $this->getSimpleString('exit();');
101
102
        $ret .= $this->phpcode->getPhpCodeConditions('$permSubmit', ' == ', 'false', $condIf, false);
103
104
        return $ret;
105
    }
106
107
    /*
108
    *  @public function getAdminPagesList
109
    *  @param string $tableName
110
    *  @param string $language
111
    */
112
    /**
113
     * @param $moduleDirname
114
     * @param $tableName
115
     * @param $language
116
     *
117
     * @return string
118
     */
119
    public function getUserSubmitForm($moduleDirname, $tableName, $tableSoleName, $language)
120
    {
121
        $stuModuleDirname = strtoupper($moduleDirname);
0 ignored issues
show
Unused Code introduced by
$stuModuleDirname 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...
122
        $stuTableSoleName = strtoupper($tableSoleName);
0 ignored issues
show
Unused Code introduced by
$stuTableSoleName 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...
123
        $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...
124
125
        $ret = $this->getCommentLine('Mavigation');
126
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$navigation', "{$language}SUBMIT_PROPOSER");
127
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('navigation', '$navigation');
128
        $ret .= $this->getCommentLine('Title of page');
129
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$title', "{$language}SUBMIT_PROPOSER . '&nbsp;-&nbsp;'");
130
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$title.', "\$GLOBALS['xoopsModule']->name()");
131
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('xoops_pagetitle', '$title');
132
        $ret .= $this->getCommentLine('Description');
133
        $ret .= $this->usercode->getUserAddMeta('description', $language, 'SUBMIT_PROPOSER');
134
        $ret .= $this->getCommentLine('Form Create');
135
        $ret .= $this->xoopscode->getXoopsCodeObjHandlerCreate($tableName);
136
        $ret .= $this->xoopscode->getXoopsCodeGetForm('form', $tableName, 'Obj');
137
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('form', '$form->render()');
138
139
        return $ret;
140
    }
141
142
    /*
143
    *  @public function getUserSubmitSave
144
    *  @param string $moduleDirname
145
    *  @param string $tableName
146
    */
147
    /**
148
     * @param $moduleDirname
149
     * @param $table_id
150
     * @param $tableName
151
     *
152
     * @return string
153
     */
154
    public function getUserSubmitSave($moduleDirname, $fields, $tableName, $language)
155
    {
156
        $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...
157
        $ret = $this->phpcode->getPhpCodeCommentLine('Security Check');
158
        $xoopsSecurityCheck = $this->xoopscode->getXoopsCodeSecurityCheck();
159
        $securityError = $this->xoopscode->getXoopsCodeSecurityErrors();
160
        $implode = $this->phpcode->getPhpCodeImplode(',', $securityError);
161
        $redirectError = $this->xoopscode->getXoopsCodeRedirectHeader($tableName, '', '3', $implode);
162
        $ret .= $this->phpcode->getPhpCodeConditions($xoopsSecurityCheck, '', '', $redirectError, false, "\t");
163
        $ret .= $this->xoopscode->getXoopsCodeObjHandlerCreate($tableName);
164
165
        $ret .= $this->xoopscode->getXoopsCodeSaveElements($moduleDirname, $tableName, $fields);
166
167
        $ret .= $this->getCommentLine('Insert Data');
168
        $insert = $this->xoopscode->getXoopsCodeInsert($tableName, $tableName, 'Obj', true);
169
        $confirmOk = $this->xoopscode->getXoopsCodeRedirectHeader('index', '', '2', "{$language}FORM_OK");
170
        $ret .= $this->phpcode->getPhpCodeConditions($insert, '', '', $confirmOk, false, "\t");
171
172
        $ret .= $this->getCommentLine('Get Form Error');
173
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
174
        $ret .= $this->xoopscode->getXoopsCodeGetForm('form', $tableName, 'Obj');
175
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('form', '$form->display()');
176
177
        return $ret;
178
    }
179
180
    /*
181
    *  @public function getUserSubmitFooter
182
    *  @param null
183
    */
184
    /**
185
     * @return string
186
     */
187
    public function getUserSubmitFooter($moduleDirname, $language)
188
    {
189
        $stuModuleDirname = strtoupper($moduleDirname);
0 ignored issues
show
Unused Code introduced by
$stuModuleDirname 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...
190
        $ret = $this->getCommentLine('Breadcrumbs');
191
        $ret .= $this->usercode->getUserBreadcrumbs('SUBMIT', $language);
192
        $ret .= $this->getInclude('footer');
193
194
        return $ret;
195
    }
196
197
    /*
198
     *  @private function getUserSubmitSwitch
199
     *  @param $moduleDirname
200
     *  @param $tableName
201
     *  @param $tableSoleName
202
     *  @param $language
203
     *
204
     * @return string
205
     */
206 View Code Duplication
    private function getUserSubmitSwitch($moduleDirname, $tableName, $tableSoleName, $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...
207
    {
208
        $table = $this->getTable();
209
        $tableId = $table->getVar('table_id');
210
        $tableMid = $table->getVar('table_mid');
211
        $fields = $this->getTableFields($tableMid, $tableId);
212
        $cases = array('form' => array($this->getUserSubmitForm($moduleDirname, $tableName, $tableSoleName, $language)),
213
                    'save' => array($this->getUserSubmitSave($moduleDirname, $fields, $tableName, $language)), );
214
215
        return $this->xoopscode->getXoopsCodeSwitch('op', $cases, true);
216
    }
217
218
    /*
219
    *  @public function render
220
    *  @param null
221
    */
222
    /**
223
     * @return bool|string
224
     */
225 View Code Duplication
    public function render()
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...
226
    {
227
        $module = $this->getModule();
228
        $table = $this->getTable();
229
        $filename = $this->getFileName();
230
        $moduleDirname = $module->getVar('mod_dirname');
231
        $tableName = $table->getVar('table_name');
232
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Unused Code introduced by
$tableCategory 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...
233
        $tableSoleName = $table->getVar('table_solename');
234
        $language = $this->getLanguage($moduleDirname, 'MA');
235
        $content = $this->getHeaderFilesComments($module, $filename);
236
        $content .= $this->getUserSubmitHeader($moduleDirname);
237
        $content .= $this->getUserSubmitSwitch($moduleDirname, $tableName, $tableSoleName, $language);
238
        $content .= $this->getUserSubmitFooter($moduleDirname, $language);
239
240
        $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...
241
242
        return $this->renderFile();
243
    }
244
}
245