Completed
Push — master ( 26776f...d9604e )
by Michael
11:31
created

fields.php ➔ editField()   F

Complexity

Conditions 24
Paths 2112

Size

Total Lines 246
Code Lines 174

Duplication

Lines 39
Ratio 15.85 %

Importance

Changes 0
Metric Value
cc 24
eloc 174
nc 2112
nop 0
dl 39
loc 246
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 10 and the first side effect is on line 3.

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
//$Id: fields.php,v 1.9 2005/12/30 15:51:14 ackbarr Exp $
3
include('../../../include/cp_header.php');
4
include_once('admin_header.php');
5
include_once(XOOPS_ROOT_PATH . '/class/pagenav.php');
6
require_once(XOOPS_ROOT_PATH . '/class/xoopsformloader.php');
7
require_once(XHELP_CLASS_PATH . '/xhelpForm.php');
8
require_once(XHELP_CLASS_PATH . '/xhelpFormRegex.php');
9
10
define('_XHELP_FIELD_MINLEN', 2);
11
define('_XHELP_FIELD_MAXLEN', 16777215);
12
13
global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
14
$module_id = $xoopsModule->getVar('mid');
15
16
$op = 'default';
17
18
if ( isset( $_REQUEST['op'] ) )
19
{
20
    $op = $_REQUEST['op'];
21
}
22
23
switch ( $op )
24
{
25
    case "delfield":
26
        deleteField();
27
        break;
28
    case "editfield":
29
        editField();
30
        break;
31
32
    case "clearAddSession":
33
        clearAddSession();
34
        break;
35
36
    case "clearEditSession":
37
        clearEditSession();
38
        break;
39
40
    case "setFieldRequired":
41
        setFieldRequired();
42
        break;
43
44
    case "manageFields":
45
    default:
46
        manageFields();
47
        break;
48
49
}
50
51
function manageFields()
0 ignored issues
show
Coding Style introduced by
manageFields uses the super-global variable $_GET 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...
Coding Style introduced by
manageFields uses the super-global variable $_POST 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...
52
{
53
    global $imagearray;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
54
55
    $session     =& Session::singleton();
56
    $regex_array =& _getRegexArray();
57
    $hFields     =& xhelpGetHandler('ticketField');
58
59
    $start = $limit = 0;
60
61
    if (isset($_GET['limit'])) {
62
        $limit = intval($_GET['limit']);
63
    }
64
65
    if (isset($_GET['start'])) {
66
        $start = intval($_GET['start']);
67
    }
68
69
    if (!$limit) {
70
        $limit = 15;
71
    }
72
73
    if (!isset($_POST['addField'])) {
74
        $crit = new Criteria('','');
75
        $crit->setLimit($limit);
76
        $crit->setStart($start);
77
        $crit->setSort('weight');
78
        $crit->setOrder('ASC');
79
80
        $count = $hFields->getCount($crit);
81
        $fields =& $hFields->getObjects($crit);
82
83
        //Display List of Current Fields, form for new field
84
        xoops_cp_header();
85
        //echo $oAdminButton->renderButtons('manfields');
86
        $indexAdmin = new ModuleAdmin();
87
        echo $indexAdmin->addNavigation('fields.php');
88
89
        if ($count) {
90
            $nav = new XoopsPageNav($count, $limit, $start, 'start', "op=manageFields&amp;limit=$limit");
91
92
            echo "<table width='100%' cellspacing='1' class='outer'>
93
                <tr><th colspan='7'><label>"._AM_XHELP_TEXT_MANAGE_FIELDS."</label></th></tr>";
94
            echo "<tr class='head'>
95
                <td>"._AM_XHELP_TEXT_ID."</td>
96
                <td>"._AM_XHELP_TEXT_NAME."</td>
97
                <td>"._AM_XHELP_TEXT_DESCRIPTION."</td>
98
                <td>"._AM_XHELP_TEXT_FIELDNAME."</td>
99
                <td>"._AM_XHELP_TEXT_CONTROLTYPE."</td>
100
                <td>"._AM_XHELP_TEXT_REQUIRED."</td>
101
                <td>"._AM_XHELP_TEXT_ACTIONS."</td>
102
            </tr>";
103
104
            $req_link_params = array('op' => 'setFieldRequired',
105
                'setrequired' => 1,
106
                'id' => 0);
107
108
            foreach ($fields as $field) {
109
                $req_link_params['id'] = $field->getVar('id');
110
111
                if ($field->getVar('required')) {
112
                    $req_link_params['setrequired'] = 0;
113
                    $req_img = $imagearray['online'];
114
                    $req_title = _AM_XHELP_MESSAGE_DEACTIVATE;
115
                } else {
116
                    $req_link_params['setrequired'] = 1;
117
                    $req_img = $imagearray['offline'];
118
                    $req_title = _AM_XHELP_MESSAGE_ACTIVATE;
119
                }
120
121
                $edit_url = xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php',
122
                array('op' => 'editfield', 'id' => $field->getVar('id')));
123
                $del_url  = xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php',
124
                array('op' => 'delfield', 'id' => $field->getVar('id')));
125
126
                echo "<tr class='even'><td>".$field->getVar('id')."</td>
127
                    <td>".$field->getVar('name')."</td>
128
                    <td>".$field->getVar('description')."</td>
129
                    <td>".$field->getVar('fieldname')."</td>
130
                    <td>".xhelpGetControlLabel($field->getVar('controltype'))."</td>
131
                    <td><a href='".xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', $req_link_params)."' title='$req_title'>$req_img</a></td>
132
                    <td><a href='$edit_url'>{$imagearray['editimg']}</a>
133
                        <a href='$del_url'>{$imagearray['deleteimg']}</a></td>
134
                    </tr>";
135
136
            }
137
            echo '</table>';
138
            //Render Page Nav
139
            echo "<div id='pagenav'>". $nav->renderNav(). "</div><br />";
140
        }
141
142
        //Get Custom Field From session (if exists)
143
        $field_info = $session->get('xhelp_addField');
144
        $field_errors = $session->get('xhelp_addFieldErrors');
145
146
        $hDepts =& xhelpGetHandler('department');
147
        $depts =& $hDepts->getObjects();
148
        $deptarr = array();
149
150
        foreach($depts as $obj) {
151
            $deptarr[$obj->getVar('id')] = $obj->getVar('department');
152
        }
153
154
        if (! $field_info === false) {
155
            //extract($field_info , EXTR_PREFIX_ALL , 'fld_');
156
            $fld_controltype = $field_info['controltype'];
157
            $fld_datatype = $field_info['datatype'];
158
            $fld_departments = $field_info['departments'];
159
            $fld_name = $field_info['name'];
160
            $fld_fieldname = $field_info['fieldname'];
161
            $fld_description = $field_info['description'];
162
            $fld_required = $field_info['required'];
163
            $fld_length = $field_info['length'];
164
            $fld_weight = $field_info['weight'];
165
            $fld_defaultvalue = $field_info['defaultvalue'];
166
            $fld_values = $field_info['values'];
167
            $fld_validation = $field_info['validation'];
168
        } else {
169
            $fld_controltype = '';
170
            $fld_datatype = '';
171
            $fld_departments = array_keys($deptarr);
172
            $fld_name = '';
173
            $fld_fieldname = '';
174
            $fld_description = '';
175
            $fld_required = '';
176
            $fld_length = '';
177
            $fld_weight = '';
178
            $fld_defaultvalue = '';
179
            $fld_values = '';
180
            $fld_validation = '';
181
        }
182
183 View Code Duplication
        if (! $field_errors === false) {
184
            xhelpRenderErrors($field_errors, xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'clearAddSession')));
185
        }
186
187
        //Add Field Form
188
        $controls = xhelpGetControlArray();
189
        $control_select = new XoopsFormSelect(_AM_XHELP_TEXT_CONTROLTYPE, 'fld_controltype', $fld_controltype);
190
        foreach($controls as $key=>$control) {
191
            $control_select->addOption($key, $control['label']);
192
        }
193
194
        $datatypes = array(
195
        _XHELP_DATATYPE_TEXT => _XHELP_DATATYPE_TEXT,
196
        _XHELP_DATATYPE_NUMBER_INT => _XHELP_DATATYPE_NUMBER_INT,
197
        _XHELP_DATATYPE_NUMBER_DEC => _XHELP_DATATYPE_NUMBER_DEC);
198
199
        $datatype_select = new XoopsFormSelect(_AM_XHELP_TEXT_DATATYPE, 'fld_datatype', $fld_datatype);
200
        $datatype_select->addOptionArray($datatypes);
201
202
        $dept_select = new XoopsFormSelect(_AM_XHELP_TEXT_DEPARTMENTS, 'fld_departments', $fld_departments, 5, true);
203
        foreach($depts as $obj) {
204
            $dept_select->addOptionArray($deptarr);
205
        }
206
        unset($depts);
207
208
        $form = new xhelpForm(_AM_XHELP_ADD_FIELD, 'add_field', xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'managefields')));
209
        $nameEle = new XoopsFormText(_AM_XHELP_TEXT_NAME, 'fld_name', 30, 64, $fld_name);
210
        $nameEle->setDescription(_AM_XHELP_TEXT_NAME_DESC);
211
        $form->addElement($nameEle);
212
213
        $fieldnameEle = new XoopsFormText(_AM_XHELP_TEXT_FIELDNAME, 'fld_fieldname', 30, 64, $fld_fieldname);
214
        $fieldnameEle->setDescription(_AM_XHELP_TEXT_FIELDNAME_DESC);
215
        $form->addElement($fieldnameEle);
216
217
        $descriptionEle = new XoopsFormTextArea(_AM_XHELP_TEXT_DESCRIPTION, 'fld_description', $fld_description, 5, 60);
218
        $descriptionEle->setDescription(_AM_XHELP_TEXT_DESCRIPTION_DESC);
219
        $form->addElement($descriptionEle);
220
221
        $dept_select->setDescription(_AM_XHELP_TEXT_DEPT_DESC);
222
        $control_select->setDescription(_AM_XHELP_TEXT_CONTROLTYPE_DESC);
223
        $datatype_select->setDescription(_AM_XHELP_TEXT_DATATYPE_DESC);
224
225
        $form->addElement($dept_select);
226
        $form->addElement($control_select);
227
        $form->addElement($datatype_select);
228
229
        $required = new XoopsFormRadioYN(_AM_XHELP_TEXT_REQUIRED, 'fld_required', $fld_required);
230
        $required->setDescription(_AM_XHELP_TEXT_REQUIRED_DESC);
231
        $form->addElement($required);
232
233
        $lengthEle = new XoopsFormText(_AM_XHELP_TEXT_LENGTH, 'fld_length', 5, 5, $fld_length);
234
        $lengthEle->setDescription(_AM_XHELP_TEXT_LENGTH_DESC);
235
        $weightEle = new XoopsFormText(_AM_XHELP_TEXT_WEIGHT, 'fld_weight', 5, 5, $fld_weight);
236
        $weightEle->setDescription(_AM_XHELP_TEXT_WEIGHT_DESC);
237
238
        $form->addElement($lengthEle);
239
        $form->addElement($weightEle);
240
241
        $regex_control = new xhelpFormRegex(_AM_XHELP_TEXT_VALIDATION, 'fld_valid', $fld_validation);
242
        $regex_control->addOptionArray($regex_array);
243
        $regex_control->setDescription(_AM_XHELP_TEXT_VALIDATION_DESC);
244
245
        $form->addElement($regex_control);
246
247
        $defaultValueEle = new XoopsFormText(_AM_XHELP_TEXT_DEFAULTVALUE, 'fld_defaultvalue', 30, 100, $fld_defaultvalue);
248
        $defaultValueEle->setDescription(_AM_XHELP_TEXT_DEFAULTVALUE_DESC);
249
        $form->addElement($defaultValueEle);
250
        $values = new XoopsFormTextArea(_AM_XHELP_TEXT_FIELDVALUES, 'fld_values', $fld_values, 5, 60);
251
        $values->setDescription(_AM_XHELP_TEXT_FIELDVALUES_DESC);
252
        $form->addElement($values);
253
254
        $btn_tray = new XoopsFormElementTray('');
255
        $btn_tray->addElement(new XoopsFormButton('', 'addField', _AM_XHELP_BUTTON_SUBMIT, 'submit'));
256
257
        $form->addElement($btn_tray);
258
        echo $form->render();
259
260
include_once "admin_footer.php";
261
262
    } else {
263
        //Validate Field Information
264
        $has_errors = false;
265
        $hField =& xhelpGetHandler('ticketField');
266
267
        $values =& _parseValues($_POST['fld_values']);
268
269 View Code Duplication
        if (!$control = xhelpGetControl($_POST['fld_controltype'])) {
270
            $has_errors = true;
271
            $errors['fld_controltype'][] = _AM_XHELP_VALID_ERR_CONTROLTYPE;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$errors was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errors = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
272
        }
273
274
        $fld_needslength = $control['needs_length'];
275
        $fld_needsvalues = $control['needs_values'];
276
277
        //name field filled?
278 View Code Duplication
        if (trim($_POST['fld_name']) == '') {
279
            $has_errors = true;
280
            $errors['fld_name'][] = _AM_XHELP_VALID_ERR_NAME;
0 ignored issues
show
Bug introduced by
The variable $errors 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...
281
        }
282
283
        $fld_fieldname = sanitizeFieldName($_POST['fld_fieldname']);
284
285
        //fieldname filled
286 View Code Duplication
        if (trim($fld_fieldname) == '') {
287
            $has_errors = true;
288
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME;
289
        }
290
291
        //fieldname unique?
292
        $crit = new CriteriaCompo(new Criteria('fieldname', $fld_fieldname));
293
        if ($hField->getCount($crit)) {
294
            $has_errors = true;
295
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME_UNIQUE;
296
        }
297
298
        //Length filled
299
        if (intval($_POST['fld_length']) == 0 && $fld_needslength == true) {
300
            $has_errors = true;
301
            $errors['fld_length'][] = sprintf(_AM_XHELP_VALID_ERR_LENGTH, 2, 16777215);
302
        }
303
304
        //Departments Chosen?
305
306
        //default value in value set?
307 View Code Duplication
        if (count($values)) {
308
            if (!in_array($_POST['fld_defaultvalue'], $values, true) && !array_key_exists($_POST['fld_defaultvalue'], $values) ) {
309
                $has_errors = true;
310
                $errors['fld_defaultvalue'][] = _AM_XHELP_VALID_ERR_DEFAULTVALUE;
311
            }
312
313
            //length larger than longest value?
314
            $length = intval($_POST['fld_length']);
315
            foreach($values as $key=>$value) {
316
                if (strlen($key) > $length) {
317
                    $has_errors = true;
318
                    $errors['fld_values'][] = sprintf(_AM_XHELP_VALID_ERR_VALUE_LENGTH, htmlentities($key), $length);
319
                }
320
            }
321
322
            //Values are all of the correct datatype?
323
        } elseif ($fld_needsvalues) {
324
            $has_errors = true;
325
            $errors['fld_values'][] = _AM_XHELP_VALID_ERR_VALUE;
326
        }
327
328
        if ($has_errors) {
329
            $afield = array();
330
331
            $afield['name'] = $_POST['fld_name'];
332
            $afield['description'] = $_POST['fld_description'];
333
            $afield['fieldname'] = $fld_fieldname;
334
            $afield['departments'] = $_POST['fld_departments'];
335
            $afield['controltype'] = $_POST['fld_controltype'];
336
            $afield['datatype'] = $_POST['fld_datatype'];
337
            $afield['required'] = $_POST['fld_required'];
338
            $afield['weight'] = $_POST['fld_weight'];
339
            $afield['defaultvalue'] = $_POST['fld_defaultvalue'];
340
            $afield['values'] = $_POST['fld_values'];
341
            $afield['length'] = $_POST['fld_length'];
342
            $afield['validation'] = ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']);
343
            $session->set('xhelp_addField', $afield);
344
            $session->set('xhelp_addFieldErrors', $errors);
345
            header('Location: '. xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'));
346
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function manageFields() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
347
        }
348
349
        //Save field
350
        $hField =& xhelpGetHandler('ticketField');
351
        $field =& $hField->create();
352
        $field->setVar('name', $_POST['fld_name']);
353
        $field->setVar('description', $_POST['fld_description']);
354
        $field->setVar('fieldname', $fld_fieldname);
355
        $field->setVar('controltype', $_POST['fld_controltype']);
356
        $field->setVar('datatype', $_POST['fld_datatype']);
357
        $field->setVar('fieldlength', $_POST['fld_length']);
358
        $field->setVar('required', $_POST['fld_required']);
359
        $field->setVar('weight', $_POST['fld_weight']);
360
        $field->setVar('defaultvalue', $_POST['fld_defaultvalue']);
361
        $field->setVar('validation', ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']));
362
        $field->addValues($values);
363
        $field->addDepartments($_POST['fld_departments']);
364
365
        if ($hField->insert($field)) {
366
            _clearAddSessionVars();
367
            redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'), 3, _AM_XHELP_MSG_FIELD_ADD_OK);
368
369
        } else {
370
            $errors = $field->getHtmlErrors();
371
            redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'), 3, _AM_XHELP_MSG_FIELD_ADD_ERR . $errors);
372
        }
373
    }
374
}
375
376
function _formatValues($values_arr)
377
{
378
    $ret = '';
379
    foreach($values_arr as $key=>$value) {
380
        $ret .= "$key=$value\r\n";
381
    }
382
383
    return $ret;
384
}
385
386
function &_parseValues($raw_values)
387
{
388
    $_inValue = false;
0 ignored issues
show
Unused Code introduced by
$_inValue 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...
389
    $values = array();
390
391
    if (strlen($raw_values) == 0) {
392
        return $values;
393
    }
394
395
    //Split values into name/value pairs
396
    $lines = explode("\r\n", $raw_values);
397
398
    //Parse each line into name=value
399
    foreach($lines as $line) {
400
        if (strlen(trim($line)) == 0) {
401
            continue(1);
402
        }
403
        $name = $value = '';
404
        $_inValue = false;
405
        $chrs = strlen($line);
406
        for ($i = 0; $i <= $chrs; $i++) {
407
            $chr = substr($line, $i, 1);
408
            if ($chr == '=' && $_inValue == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
409
                $_inValue=true;
410
            } else {
411
                if ($_inValue) {
412
                    $name .= $chr;
413
                } else {
414
                    $value .= $chr;
415
                }
416
            }
417
        }
418
        //Add value to array
419
        if ($value=='') {
420
            $values[$name]=$name;
421
        } else {
422
            $values[$value] = $name;
423
        }
424
425
        //Reset name / value vars
426
        $name = $value = '';
0 ignored issues
show
Unused Code introduced by
$value 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...
Unused Code introduced by
$name 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...
427
    }
428
429
    return $values;
430
}
431
432
function deleteField()
0 ignored issues
show
Coding Style introduced by
deleteField uses the super-global variable $_REQUEST 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...
Coding Style introduced by
deleteField uses the super-global variable $_POST 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...
433
{
434
    global $_eventsrv ;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
435 View Code Duplication
    if (!isset( $_REQUEST['id'])) {
436
        redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=> 'manageDepartments'), false), 3, _AM_XHELP_MESSAGE_NO_FIELD);
437
    }
438
439
    $id = intval($_REQUEST['id']);
440
441
    if (!isset($_POST['ok'])) {
442
        xoops_cp_header();
443
        //echo $oAdminButton->renderButtons('manfields');
444
        $indexAdmin = new ModuleAdmin();
445
        echo $indexAdmin->addNavigation('fields.php');
446
447
        xoops_confirm(array('op' => 'delfield', 'id' => $id, 'ok' => 1), XHELP_ADMIN_URL .'/fields.php', sprintf(_AM_XHELP_MSG_FIELD_DEL_CFRM, $id));
448
        xoops_cp_footer();
449
    } else {
450
        $hFields =& xhelpGetHandler('ticketField');
451
        $field =& $hFields->get($id);
452
        if($hFields->delete($field, true)){
453
            $_eventsrv->trigger('delete_field', array(&$field));
454
            header("Location: " . xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'manageFields'), false));
455
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function deleteField() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
456
457
        } else {
458
            redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op' => 'manageFields'), false), 3, $message);
459
        }
460
    }
461
}
462
463
function editField()
0 ignored issues
show
Coding Style introduced by
editField uses the super-global variable $_REQUEST 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...
Coding Style introduced by
editField uses the super-global variable $_POST 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...
464
{
465
    $eventsrv =& xhelpNewEventService();
466
    $session  =& Session::singleton();
467
    $regex_array =& _getRegexArray();
468
469 View Code Duplication
    if (!isset( $_REQUEST['id'])) {
470
        redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=> 'manageDepartments'), false), 3, _AM_XHELP_MESSAGE_NO_FIELD);
471
    }
472
473
    $fld_id = intval($_REQUEST['id']);
474
    $hField =& xhelpGetHandler('ticketField');
475 View Code Duplication
    if (! $field =& $hField->get($fld_id)) {
476
        redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=> 'manageDepartments'), false), 3, _AM_XHELP_MESSAGE_NO_FIELD);
477
    }
478
479
    if (!isset($_POST ['editField'])) {
480
        //Get Custom Field From session (if exists)
481
        $field_info = $session->get('xhelp_editField_'.$fld_id);
482
        $field_errors = $session->get('xhelp_editFieldErrors_'.$fld_id);
483
484
        if ( ! $field_info === false) {
485
            $fld_controltype = $field_info['controltype'];
486
            $fld_datatype = $field_info['datatype'];
487
            $fld_departments = $field_info['departments'];
488
            $fld_name = $field_info['name'];
489
            $fld_fieldname = $field_info['fieldname'];
490
            $fld_description = $field_info['description'];
491
            $fld_required = $field_info['required'];
492
            $fld_length = $field_info['length'];
493
            $fld_weight = $field_info['weight'];
494
            $fld_defaultvalue = $field_info['defaultvalue'];
495
            $fld_values = $field_info['values'];
496
            $fld_validation = $field_info['validation'];
497
        } else {
498
            $hFDept =& xhelpGetHandler('ticketFieldDepartment');
499
            $depts =& $hFDept->departmentsByField($field->getVar('id'), true);
500
501
            $fld_controltype = $field->getVar('controltype');
502
            $fld_datatype = $field->getVar('datatype');
503
            $fld_departments = array_keys($depts);
504
            $fld_name = $field->getVar('name');
505
            $fld_fieldname = $field->getVar('fieldname');
506
            $fld_description = $field->getVar('description');
507
            $fld_required = $field->getVar('required');
508
            $fld_length = $field->getVar('fieldlength');
509
            $fld_weight = $field->getVar('weight');
510
            $fld_defaultvalue = $field->getVar('defaultvalue');
511
            $fld_values = _formatValues($field->getVar('fieldvalues'));
512
            $fld_validation = $field->getVar('validation');
513
        }
514
515
        //Display Field modification
516
        xoops_cp_header();
517
        //echo $oAdminButton->renderButtons('manfields');
518
        $indexAdmin = new ModuleAdmin();
519
        echo $indexAdmin->addNavigation('fields.php');
520
521
        //Edit Field Form
522
523
        $controls = xhelpGetControlArray();
524
        $control_select = new XoopsFormSelect(_AM_XHELP_TEXT_CONTROLTYPE, 'fld_controltype', $fld_controltype);
525
        $control_select->setDescription(_AM_XHELP_TEXT_CONTROLTYPE_DESC);
526
        foreach($controls as $key=>$control) {
527
            $control_select->addOption($key, $control['label']);
528
        }
529
530
        $datatypes = array(
531
        _XHELP_DATATYPE_TEXT => _XHELP_DATATYPE_TEXT,
532
        _XHELP_DATATYPE_NUMBER_INT => _XHELP_DATATYPE_NUMBER_INT,
533
        _XHELP_DATATYPE_NUMBER_DEC => _XHELP_DATATYPE_NUMBER_DEC);
534
535
        $datatype_select = new XoopsFormSelect(_AM_XHELP_TEXT_DATATYPE, 'fld_datatype', $fld_datatype);
536
        $datatype_select->setDescription(_AM_XHELP_TEXT_DATATYPE_DESC);
537
        $datatype_select->addOptionArray($datatypes);
538
539
        $hDepts =& xhelpGetHandler('department');
540
        $depts =& $hDepts->getObjects();
541
        $dept_select = new XoopsFormSelect(_AM_XHELP_TEXT_DEPARTMENTS, 'fld_departments', $fld_departments, 5, true);
542
        $dept_select->setDescription(_AM_XHELP_TEXT_DEPT_DESC);
543
        foreach($depts as $obj) {
544
            $dept_select->addOption($obj->getVar('id'), $obj->getVar('department'));
545
        }
546
        unset($depts);
547
548 View Code Duplication
        if (! $field_errors === false) {
549
            xhelpRenderErrors($field_errors, xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'clearEditSession', 'id'=>$fld_id)));
550
        }
551
552
        $form = new xhelpForm(_AM_XHELP_EDIT_FIELD, 'edit_field', xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fld_id)));
553
554
        $nameEle = new XoopsFormText(_AM_XHELP_TEXT_NAME, 'fld_name', 30, 64, $fld_name);
555
        $nameEle->setDescription(_AM_XHELP_TEXT_NAME_DESC);
556
        $form->addElement($nameEle);
557
558
        $fieldnameEle = new XoopsFormText(_AM_XHELP_TEXT_FIELDNAME, 'fld_fieldname', 30, 64, $fld_fieldname);
559
        $fieldnameEle->setDescription(_AM_XHELP_TEXT_FIELDNAME_DESC);
560
        $form->addElement($fieldnameEle);
561
562
        $descriptionEle = new XoopsFormTextArea(_AM_XHELP_TEXT_DESCRIPTION, 'fld_description', $fld_description, 5, 60);
563
        $descriptionEle->setDescription(_AM_XHELP_TEXT_DESCRIPTION_DESC);
564
        $form->addElement($descriptionEle);
565
566
        $form->addElement($dept_select);
567
        $form->addElement($control_select);
568
        $form->addElement($datatype_select);
569
570
        $required = new XoopsFormRadioYN(_AM_XHELP_TEXT_REQUIRED, 'fld_required', $fld_required);
571
        $required->setDescription(_AM_XHELP_TEXT_REQUIRED_DESC);
572
        $form->addElement($required);
573
574
        $lengthEle = new XoopsFormText(_AM_XHELP_TEXT_LENGTH, 'fld_length', 5, 5, $fld_length);
575
        $lengthEle->setDescription(_AM_XHELP_TEXT_LENGTH_DESC);
576
        $form->addElement($lengthEle);
577
578
        $widthEle = new XoopsFormText(_AM_XHELP_TEXT_WEIGHT, 'fld_weight', 5, 5, $fld_weight);
579
        $widthEle->setDescription(_AM_XHELP_TEXT_WEIGHT_DESC);
580
        $form->addElement($widthEle);
581
582
        $regex_control = new xhelpFormRegex(_AM_XHELP_TEXT_VALIDATION, 'fld_valid', $fld_validation);
583
        $regex_control->setDescription(_AM_XHELP_TEXT_VALIDATION_DESC);
584
        $regex_control->addOptionArray($regex_array);
585
586
        $form->addElement($regex_control);
587
588
        $defaultValueEle = new XoopsFormText(_AM_XHELP_TEXT_DEFAULTVALUE, 'fld_defaultvalue', 30, 100, $fld_defaultvalue);
589
        $defaultValueEle->setDescription(_AM_XHELP_TEXT_DEFAULTVALUE_DESC);
590
        $form->addElement($defaultValueEle);
591
        $values = new XoopsFormTextArea(_AM_XHELP_TEXT_FIELDVALUES, 'fld_values', $fld_values, 5, 60);
592
        $values->setDescription(_AM_XHELP_TEXT_FIELDVALUES_DESC);
593
        $form->addElement($values);
594
595
        $btn_tray = new XoopsFormElementTray('');
596
        $btn_tray->addElement(new XoopsFormButton('', 'editField', _AM_XHELP_BUTTON_SUBMIT, 'submit'));
597
        $btn_tray->addElement(new XoopsFormButton('','cancel', _AM_XHELP_BUTTON_CANCEL));
598
        $btn_tray->addElement(new XoopsFormHidden('id', $fld_id));
599
600
        $form->addElement($btn_tray);
601
        echo $form->render();
602
603
        include_once "admin_footer.php";
604
    } else {
605
        //Validate Field Information
606
        $has_errors = false;
607
        $errors = array();
608
        $values =& _parseValues($_POST['fld_values']);
609
610 View Code Duplication
        if (!$control = xhelpGetControl($_POST['fld_controltype'])) {
611
            $has_errors = true;
612
            $errors['fld_controltype'][] = _AM_XHELP_VALID_ERR_CONTROLTYPE;
613
        }
614
615
        $fld_needslength = $control['needs_length'];
616
        $fld_needsvalues = $control['needs_values'];
617
618
        //name field filled?
619 View Code Duplication
        if (trim($_POST['fld_name']) == '') {
620
            $has_errors = true;
621
            $errors['fld_name'][] = _AM_XHELP_VALID_ERR_NAME;
622
        }
623
624
        //fieldname filled
625 View Code Duplication
        if (trim($_POST['fld_fieldname']) == '') {
626
            $has_errors = true;
627
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME;
628
        }
629
630
        //fieldname unique?
631
        $crit = new CriteriaCompo(new Criteria('id', $fld_id, '!='));
632
        $crit->add(new Criteria('fieldname', $_POST['fld_fieldname']));
633
        if ($hField->getCount($crit)) {
634
            $has_errors = true;
635
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME_UNIQUE;
636
        }
637
638
        //Length filled
639
        if (intval($_POST['fld_length']) == 0 && $fld_needslength == true) {
640
            $has_errors = true;
641
            $errors['fld_length'][] = sprintf(_AM_XHELP_VALID_ERR_LENGTH, _XHELP_FIELD_MINLEN, _XHELP_FIELD_MAXLEN);
642
        }
643
644
        //default value in value set?
645 View Code Duplication
        if (count($values)) {
646
            if (!in_array($_POST['fld_defaultvalue'], $values, true) && !array_key_exists($_POST['fld_defaultvalue'], $values) ) {
647
                $has_errors = true;
648
                $errors['fld_defaultvalue'][] = _AM_XHELP_VALID_ERR_DEFAULTVALUE;
649
            }
650
651
            //length larger than longest value?
652
            $length = intval($_POST['fld_length']);
653
            foreach($values as $key=>$value) {
654
                if (strlen($key) > $length) {
655
                    $has_errors = true;
656
                    $errors['fld_values'][] = sprintf(_AM_XHELP_VALID_ERR_VALUE_LENGTH, htmlentities($key), $length);
657
                }
658
            }
659
        } elseif ($fld_needsvalues) {
660
            $has_errors = true;
661
            $errors['fld_values'][] = _AM_XHELP_VALID_ERR_VALUE;
662
        }
663
664
        if ($has_errors) {
665
            $afield = array();
666
            $afield['name'] = $_POST['fld_name'];
667
            $afield['description'] = $_POST['fld_description'];
668
            $afield['fieldname'] = $_POST['fld_fieldname'];
669
            $afield['departments'] = $_POST['fld_departments'];
670
            $afield['controltype'] = $_POST['fld_controltype'];
671
            $afield['datatype'] = $_POST['fld_datatype'];
672
            $afield['required'] = $_POST['fld_required'];
673
            $afield['weight'] = $_POST['fld_weight'];
674
            $afield['defaultvalue'] = $_POST['fld_defaultvalue'];
675
            $afield['values'] = $_POST['fld_values'];
676
            $afield['length'] = $_POST['fld_length'];
677
            $afield['validation'] = ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']);
678
            $session->set('xhelp_editField_'.$fld_id, $afield);
679
            $session->set('xhelp_editFieldErrors_'.$fld_id, $errors);
680
            //Redirect to edit page (display errors);
681
            header('Location: '. xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fld_id), false));
682
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function editField() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
683
684
        }
685
        //Store Modified Field info
686
687
        $field->setVar('name', $_POST['fld_name']);
688
        $field->setVar('description', $_POST['fld_description']);
689
        $field->setVar('fieldname', $_POST['fld_fieldname']);
690
        $field->setVar('controltype', $_POST['fld_controltype']);
691
        $field->setVar('datatype', $_POST['fld_datatype']);
692
        $field->setVar('fieldlength', $_POST['fld_length']);
693
        $field->setVar('required', $_POST['fld_required']);
694
        $field->setVar('weight', $_POST['fld_weight']);
695
        $field->setVar('defaultvalue', $_POST['fld_defaultvalue']);
696
        $field->setVar('validation', ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']));
697
        $field->setValues($values);
698
        $field->addDepartments($_POST['fld_departments']);
699
700
        if ($hField->insert($field)) {
701
            _clearEditSessionVars($fld_id);
702
            redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'), 3, _AM_XHELP_MSG_FIELD_UPD_OK);
703
        } else {
704
            redirect_header(xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fld_id), false), 3, _AM_XHELP_MSG_FIELD_UPD_ERR);
705
        }
706
    }
707
708
}
709
710
function &_getRegexArray()
711
{
712
    $regex_array = array(
713
        '' => _AM_XHELP_TEXT_REGEX_CUSTOM,
714
        '^\d{3}-\d{3}-\d{4}$' => _AM_XHELP_TEXT_REGEX_USPHONE,
715
        '^\d{5}(-\d{4})?' => _AM_XHELP_TEXT_REGEX_USZIP,
716
        '^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}$' => _AM_XHELP_TEXT_REGEX_EMAIL);
717
718
    return $regex_array;
719
}
720
721
function setFieldRequired()
0 ignored issues
show
Coding Style introduced by
setFieldRequired uses the super-global variable $_GET 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...
722
{
723
724
    $setRequired = intval($_GET['setrequired']);
725
    $id = intval($_GET['id']);
726
727
    $setRequired = ($setRequired <> 0 ?  1 : 0);
728
729
    $hField =& xhelpGetHandler('ticketField');
730
731
    if ($field =& $hField->get($id)) {
732
        $field->setVar('required', $setRequired);
733
        $ret = $hField->insert($field, true);
734
        if ($ret) {
735
            header('Location: ' . xhelpMakeURI(XHELP_ADMIN_URL .'/fields.php'));
736
        } else {
737
            redirect_header(xhelpMakeURI(XHELP_ADMIN_URL .'/fields.php'), 3, _AM_XHELP_MSG_FIELD_UPD_ERR);
738
        }
739
    } else {
740
        redirect_header(xhelpMakeURI(XHELP_ADMIN_URL .'/fields.php'), 3, _AM_XHELP_MESSAGE_NO_FIELD);
741
    }
742
743
}
744
745
function clearAddSession()
0 ignored issues
show
Best Practice introduced by
The function clearAddSession() has been defined more than once; this definition is ignored, only the first definition in admin/department.php (L952-956) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
746
{
747
    _clearAddSessionVars();
748
    header('Location: ' . xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php'));
749
}
750
751
function clearEditSession()
0 ignored issues
show
Coding Style introduced by
clearEditSession uses the super-global variable $_REQUEST 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...
Best Practice introduced by
The function clearEditSession() has been defined more than once; this definition is ignored, only the first definition in admin/department.php (L965-970) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
752
{
753
    $fieldid = $_REQUEST['id'];
754
    _clearEditSessionVars($fieldid);
755
    header('Location: ' . xhelpMakeURI(XHELP_ADMIN_URL.'/fields.php', array('op'=>'editfield', 'id'=>$fieldid), false));
756
}
757
758
function _clearAddSessionVars()
0 ignored issues
show
Best Practice introduced by
The function _clearAddSessionVars() has been defined more than once; this definition is ignored, only the first definition in admin/department.php (L958-963) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
759
{
760
    $session = Session::singleton();
761
    $session->del('xhelp_addField');
762
    $session->del('xhelp_addFieldErrors');
763
}
764
765 View Code Duplication
function _clearEditSessionVars($id)
0 ignored issues
show
Duplication introduced by
This function 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...
Best Practice introduced by
The function _clearEditSessionVars() has been defined more than once; this definition is ignored, only the first definition in admin/department.php (L972-978) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
766
{
767
    $id = intval($id);
768
    $session = Session::singleton();
769
    $session->del("xhelp_editField_$id");
770
    $session->del("xhelp_editFieldErrors_$id");
771
}
772