Completed
Branch master (44f723)
by Michael
11:35 queued 08:26
created

CheckoutWizard::CheckoutWizard()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 26
Code Lines 16

Duplication

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

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
require_once __DIR__ . '/wizard.php';
3
4
/**
5
 * Class CheckoutWizard
6
 */
7
class CheckoutWizard extends ZervWizard
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...
8
{
9
    /**
10
     * checkoutwizard constructor.
11
     */
12
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SESSION 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...
13
    {
14
        global $field;
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...
15
        // start the session and initialize the wizard
16
        if (!isset($_SESSION)) {
17
            session_start();
18
        }
19
        parent::__construct($_SESSION, __CLASS__);
20
21
        $this->addStep('Fieldname', _MA_PEDIGREE_ENTER_FIELD);
22
        if ($this->getValue('field') == 0) { //only for a new field
23
            $this->addStep('Fieldtype', _MA_PEDIGREE_FIELD_TYP_SEL);
24
            if ($this->getValue('fieldtype') === 'selectbox' || $this->getValue('fieldtype') === 'radiobutton') {
25
                $this->addStep('lookup', _MA_PEDIGREE_FIELD_ADD_VALUE);
26
            }
27
        }
28
29
        $this->addStep('Settings', _MA_PEDIGREE_FIELD_PARAM);
30
        if ($this->getValue('hassearch') === 'hassearch') {
31
            $this->addStep('search', _MA_PEDIGREE_SEARCH_PARAMFIELD);
32
        }
33
        if ($this->getValue('fieldtype') !== 'picture') {
34
            $this->addStep('defaultvalue', _MA_PEDIGREE_FIELD_DEFAUT);
35
        }
36
        $this->addStep('confirm', _MA_PEDIGREE_FIELDCONFIRM);
37
    }
38
39
    public function prepareFieldname()
0 ignored issues
show
Coding Style introduced by
prepareFieldname uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        global $field;
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...
42
        if (!$field == 0) {
43
            // field allready exists (editing mode)
44
45
            $sql    = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE ID=' . $field;
46
            $result = $GLOBALS['xoopsDB']->query($sql);
47
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
48
                $name             = $row['FieldName'];
49
                $fieldexplanation = $row['FieldExplanation'];
50
                $fieldtype        = $row['FieldType'];
51
            }
52
            $this->setValue('name', $name);
0 ignored issues
show
Bug introduced by
The variable $name 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...
53
            $this->setValue('explain', $fieldexplanation);
0 ignored issues
show
Bug introduced by
The variable $fieldexplanation 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...
54
            //set the fieldtype because we wont allow it to be edited
55
            $this->setValue('fieldtype', $fieldtype);
0 ignored issues
show
Bug introduced by
The variable $fieldtype 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...
56
        }
57
        $this->setValue('field', $field); //is it a new field or are we editing a field
58
    }
59
60
    /**
61
     * @param $form
62
     *
63
     * @return bool
64
     */
65
    public function processFieldname(&$form)
66
    {
67
        $name = $this->coalesce($form['name']);
68
        if (strlen($name) > 0) {
69
            $this->setValue('name', $name);
70
        } else {
71
            $this->addError('name', _MA_PEDIGREE_FIELD_NAM);
72
        }
73
74
        $fieldexplanation = $this->coalesce($form['explain']);
75
        if (strlen($fieldexplanation) > 0) {
76
            $this->setValue('explain', $fieldexplanation);
77
        } else {
78
            $this->addError('explain', _MA_PEDIGREE_FIELD_EXPLAN1);
79
        }
80
81
        return !$this->isError();
82
    }
83
84
    public function prepareFieldtype()
85
    {
86
        $this->fieldtype[] = array('value' => 'radiobutton', 'description' => _MA_PEDIGREE_RADIOBUTTONFIELD);
0 ignored issues
show
Bug introduced by
The property fieldtype 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...
87
        $this->fieldtype[] = array('value' => 'selectbox', 'description' => _MA_PEDIGREE_DROPDOWNFIELD);
88
        $this->fieldtype[] = array('value' => 'textbox', 'description' => _MA_PEDIGREE_TEXTBOXFIELD);
89
        $this->fieldtype[] = array('value' => 'textarea', 'description' => _MA_PEDIGREE_TEXTAREAFIELD);
90
        $this->fieldtype[] = array('value' => 'DataSelect', 'description' => _MA_PEDIGREE_DATEFIELD);
91
        $this->fieldtype[] = array('value' => 'urlfield', 'description' => _MA_PEDIGREE_URLFIELD);
92
    }
93
94
    /**
95
     * @param $form
96
     *
97
     * @return bool
98
     */
99
    public function processFieldtype($form)
100
    {
101
        $this->prepareFieldtype();
102
        $fieldtype = $this->coalesce($form['fieldtype']);
103
        $this->setValue('fieldtype', $fieldtype);
104
105
        return !$this->isError();
106
    }
107
108
    /**
109
     * @param $form
110
     *
111
     * @return bool
112
     */
113
    public function processLookup($form)
114
    {
115
        $fc = $this->coalesce($form['fc']);
116
        $this->setValue('fc', $fc);
117
        $lookup   = $this->coalesce($form['lookup' . $fc]);
118
        $lookupid = $this->coalesce($form['id' . $fc]);
119
        if (strlen($lookup) > 0) {
120
            $this->setValue('lookup' . $fc, $lookup);
121
            $this->setValue('id' . $fc, $lookupid);
122
        }
123
        $lastlookup = $this->getValue('lookup' . $fc);
124
        if ($lastlookup == '') {
125
            $this->setValue('fc', $fc - 1);
126
        }
127
128
        for ($i = 0; $i < $fc; ++$i) {
129
            $radioarray[] = array('id' => $this->getValue('id' . ($i + 1)), 'value' => $this->getValue('lookup' . ($i + 1)));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$radioarray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $radioarray = 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...
130
        }
131
        //print_r($radioarray); die();
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132
        $this->setValue('radioarray', $radioarray);
0 ignored issues
show
Bug introduced by
The variable $radioarray 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...
133
134
        return !$this->isError();
135
        //
136
    }
137
138
    public function prepareSettings()
0 ignored issues
show
Coding Style introduced by
prepareSettings uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
139
    {
140
        if (!$this->getValue('field') == 0) {
141
            // field allready exists (editing mode)
142
143
            {
144
                $sql = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " WHERE ID='" . $this->getValue('field') . "'";
145
            }
146
            $result = $GLOBALS['xoopsDB']->query($sql);
147
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
148
                $hs = $row['HasSearch'];
149
                if ($hs == '1') {
150
                    $this->setValue('hassearch', 'hassearch');
151
                }
152
                $vip = $row['ViewInPedigree'];
153
                if ($vip == '1') {
154
                    $this->setValue('viewinpedigree', 'viewinpedigree');
155
                }
156
                $via = $row['ViewInAdvanced'];
157
                if ($via == '1') {
158
                    $this->setValue('viewinadvanced', 'viewinadvanced');
159
                }
160
                $vipie = $row['ViewInPie'];
161
                if ($vipie == '1') {
162
                    $this->setValue('viewinpie', 'viewinpie');
163
                }
164
                $vil = $row['ViewInList'];
165
                if ($vil == '1') {
166
                    $this->setValue('viewinlist', 'viewinlist');
167
                }
168
                $lit = $row['Litter'];
169
                if ($lit == '1') {
170
                    $this->setValue('Litter', 'Litter');
171
                }
172
                $Glit = $row['Generallitter'];
173
                if ($Glit == '1') {
174
                    $this->setValue('Generallitter', 'Generallitter');
175
                }
176
            }
177
        }
178
    }
179
180
    /**
181
     * @param $form
182
     *
183
     * @return bool
184
     */
185
    public function processSettings($form)
186
    {
187
        $hassearch = $this->coalesce($form['hassearch']);
188
        $this->setValue('hassearch', $hassearch);
189
        $viewinpedigree = $this->coalesce($form['viewinpedigree']);
190
        $this->setValue('viewinpedigree', $viewinpedigree);
191
        $viewinadvanced = $this->coalesce($form['viewinadvanced']);
192
        $this->setValue('viewinadvanced', $viewinadvanced);
193
        $viewinpie = $this->coalesce($form['viewinpie']);
194
        $this->setValue('viewinpie', $viewinpie);
195
        $viewinlist = $this->coalesce($form['viewinlist']);
196
        $this->setValue('viewinlist', $viewinlist);
197
        $Litter = $this->coalesce($form['Litter']);
198
        $this->setValue('Litter', $Litter);
199
        $Generallitter = $this->coalesce($form['Generallitter']);
200
        $this->setValue('Generallitter', $Generallitter);
201
202
        //if both litter and general litter are set; unset one of them
203
        if ($this->getValue('Litter') === 'Litter' && $this->getValue('Generallitter') === 'Generallitter') {
204
            $this->setValue('Generallitter', 0);
205
        }
206
207
        return !$this->isError();
208
    }
209
210
    public function prepareSearch()
0 ignored issues
show
Coding Style introduced by
prepareSearch uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
211
    {
212
        if (!$this->getValue('field') == 0) {
213
            // field allready exists (editing mode)
214
215
            $sql    = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE ID=' . $this->getValue('field');
216
            $result = $GLOBALS['xoopsDB']->query($sql);
217
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
218
                if ($this->getValue('hassearch') === 'hassearch') {
219
                    $searchname = $row['SearchName'];
220
                    $this->setValue('searchname', $searchname);
221
                    $searchexplain = $row['SearchExplanation'];
222
                    $this->setValue('searchexplain', $searchexplain);
223
                }
224
            }
225
        }
226
    }
227
228
    /**
229
     * @param $form
230
     *
231
     * @return bool
232
     */
233
    public function processSearch($form)
234
    {
235
        $searchname = $this->coalesce($form['searchname']);
236
        if (strlen($searchname) > 0) {
237
            $this->setValue('searchname', $searchname);
238
        } else {
239
            $this->addError('searchname', 'Please enter the searchname');
240
        }
241
242
        $fieldexplanation = $this->coalesce($form['searchexplain']);
243
        if (strlen($fieldexplanation) > 0) {
244
            $this->setValue('searchexplain', $fieldexplanation);
245
        } else {
246
            $this->addError('searchexplain', 'Please enter the search explanation for this field');
247
        }
248
249
        return !$this->isError();
250
    }
251
252
    public function prepareDefaultvalue()
0 ignored issues
show
Coding Style introduced by
prepareDefaultvalue uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
253
    {
254
        if (!$this->getValue('field') == 0) {
255
            // field allready exists (editing mode)
256
257
            $sql    = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE ID=' . $this->getValue('field');
258
            $result = $GLOBALS['xoopsDB']->query($sql);
259
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
260
                $def = $row['DefaultValue'];
261
                $this->setValue('defaultvalue', $def);
262
                if ($row['LookupTable'] == '1') { //we have a lookup table; load values
263
                    $sql    = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $this->getValue('field')) . " order by 'order'";
264
                    $fc     = 0;
265
                    $result = $GLOBALS['xoopsDB']->query($sql);
266
                    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
267
                        $radioarray[] = array('id' => $row['Id'], 'value' => $row['value']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$radioarray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $radioarray = 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...
268
                        ++$fc;
269
                    }
270
                    $this->setValue('radioarray', $radioarray);
0 ignored issues
show
Bug introduced by
The variable $radioarray 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...
271
                    $this->setValue('fc', $fc);
272
                }
273
            }
274
        }
275
    }
276
277
    /**
278
     * @param $form
279
     *
280
     * @return bool
281
     */
282
    public function processDefaultValue($form)
283
    {
284
        $defaultvalue = $this->coalesce($form['defaultvalue']);
285
        if (strlen($defaultvalue) >= 0) {
286
            $this->setValue('defaultvalue', $defaultvalue);
287
        } else {
288
            $this->addError('defaultvalue', 'Please enter a defaultvalue');
289
        }
290
291
        return !$this->isError();
292
    }
293
294
    /**
295
     * @param $form
296
     *
297
     * @return bool
298
     */
299
    public function processConfirm($form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
300
    {
301
        return !$this->isError();
302
    }
303
304
    public function completeCallback()
0 ignored issues
show
Coding Style introduced by
completeCallback uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
305
    {
306
307
        //can this field be searched
308
        $search = $this->getValue('hassearch');
309
        if ($search === 'hassearch') {
310
            $search        = '1';
311
            $searchname    = $this->getValue('searchname');
312
            $searchexplain = $this->getValue('searchexplain');
313
        } else {
314
            $search        = '0';
315
            $searchname    = '';
316
            $searchexplain = '';
317
        }
318
        //show in pedigree
319
        $viewinpedigree = $this->getValue('viewinpedigree');
320
        if ($viewinpedigree === 'viewinpedigree') {
321
            $viewinpedigree = '1';
322
        } else {
323
            $viewinpedigree = '0';
324
        }
325
        //show in advanced
326
        $viewinadvanced = $this->getValue('viewinadvanced');
327
        if ($viewinadvanced === 'viewinadvanced') {
328
            $viewinadvanced = '1';
329
        } else {
330
            $viewinadvanced = '0';
331
        }
332
        //show in pie
333
        $viewinpie = $this->getValue('viewinpie');
334
        if ($viewinpie === 'viewinpie') {
335
            $viewinpie = '1';
336
        } else {
337
            $viewinpie = '0';
338
        }
339
        //view in list
340
        $viewinlist = $this->getValue('viewinlist');
341
        if ($viewinlist === 'viewinlist') {
342
            $viewinlist = '1';
343
        } else {
344
            $viewinlist = '0';
345
        }
346
        //add a litter
347
        $Litter = $this->getValue('Litter');
348
        if ($Litter === 'Litter') {
349
            $Litter = '1';
350
        } else {
351
            $Litter = '0';
352
        }
353
        //general litter
354
        $Generallitter = $this->getValue('Generallitter');
355
        if ($Generallitter === 'Generallitter') {
356
            $Generallitter = '1';
357
        } else {
358
            $Generallitter = '0';
359
        }
360
361
        if (!$this->getValue('field') == 0) {
362
            // field allready exists (editing mode)
363
364
            $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " SET fieldName = '" . htmlspecialchars($this->getValue('name')) . "', fieldType = '" . $this->getValue('fieldType') . "', defaultValue = '" . $this->getValue('defaultValue') . "', fieldExplanation = '" . $this->getValue('explain') . "', hasSearch = '" . $search . "', Litter = '" . $litter . "', generallitter = '" . $generallitter . "', searchName = '" . $searchname . "', searchExplanation = '" . $searchexplain . "', viewInPedigree = '" . $viewinpedigree . "', viewInAdvanced = '" . $viewinadvanced . "', viewInPie = '" . $viewinpie . "', viewInList = '" . $viewinlist . "' WHERE ID ='" . $this->getValue('field') . "'";
0 ignored issues
show
Bug introduced by
The variable $litter does not exist. Did you mean $Litter?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $generallitter does not exist. Did you mean $Litter?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
365
            $GLOBALS['xoopsDB']->queryF($sql);
366
            //possible change defaultvalue for userfield
367
            $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
368
            $GLOBALS['xoopsDB']->queryF($sql);
369
            $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 1024 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
370
            $GLOBALS['xoopsDB']->queryF($sql);
371
            $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_trash') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
372
            $GLOBALS['xoopsDB']->queryF($sql);
373
        } else { //this is a new field
374
            $sql    = 'SELECT MAX(ID) AS lid from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' LIMIT 1';
375
            $result = $GLOBALS['xoopsDB']->query($sql);
376
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
377
                $nextfieldnum = $row['lid'] + 1;
378
            }
379
            //add userfield to various tables as a new field.
380
            //allways add at the end of the table
381
            $tables = array('pedigree_tree', 'pedigree_temp', 'pedigree_trash');
382
            foreach ($tables as $table) {
383
                $SQL = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($table) . ' ADD `user' . $nextfieldnum . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
0 ignored issues
show
Bug introduced by
The variable $nextfieldnum 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...
384
                $GLOBALS['xoopsDB']->queryF($SQL);
385
            }
386
            //is a lookup table present
387
            $lookup = $this->getValue('lookup1');
388
            if ($lookup == '') {
389
                $lookup = '0';
390
            } else {
391
                $lookup = '1';
392
                //create table for lookupfield
393
                $createtable = 'CREATE TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . ' (`ID` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM';
394
                $GLOBALS['xoopsDB']->queryF($createtable);
395
                //fill table
396
                $count = $this->getValue('fc');
397
                for ($x = 1; $x < $count + 1; ++$x) {
398
                    $y   = $x - 1;
399
                    $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . " ( `ID` , `value`, `order`) VALUES ('" . $y . "', '" . $this->getValue('lookup' . $x) . "','" . $y . "')";
400
                    $GLOBALS['xoopsDB']->queryF($sql);
401
                }
402
            }
403
404
            //Insert new record into pedigree_config
405
            //            $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " VALUES ('" . $nextfieldnum . "', '1', '" . htmlspecialchars($this->getValue('name')) . "', '" . $this->getValue('fieldtype') . "', '" . $lookup . "', '" . $this->getValue('defaultvalue') . "', '" . $this->getValue('explain') . "', '" . $search . "', '" . $Litter . "', '" . $Generallitter . "', '" . $searchname . "', '" . $searchexplain . "', '" . $viewinpedigree . "', '" . $viewinadvanced . "', '" . $viewinpie . "', '" . $viewinlist . "','','" . $nextfieldnum . "')";
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
406
            $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " VALUES ('" . $nextfieldnum . "', '1', '" . $GLOBALS['xoopsDB']->escape(htmlSpecialChars($this->getValue('name'))) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('fieldtype')) . "', '" . $GLOBALS['xoopsDB']->escape($lookup) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('defaultvalue')) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('explain')) . "', '" . $GLOBALS['xoopsDB']->escape($search) . "', '" . $GLOBALS['xoopsDB']->escape($Litter) . "', '" . $GLOBALS['xoopsDB']->escape($Generallitter) . "', '" . $GLOBALS['xoopsDB']->escape($searchname) . "', '" . $GLOBALS['xoopsDB']->escape($searchexplain) . "', '" . $GLOBALS['xoopsDB']->escape($viewinpedigree) . "', '" . $GLOBALS['xoopsDB']->escape($viewinadvanced) . "', '" . $GLOBALS['xoopsDB']->escape($viewinpie) . "', '" . $GLOBALS['xoopsDB']->escape($viewinlist) . "','','" . $GLOBALS['xoopsDB']->escape($nextfieldnum) . "')";
407
            $GLOBALS['xoopsDB']->queryF($sql);
408
        }
409
    }
410
411
    /**
412
     * Miscellaneous utility functions
413
     *
414
     * @param $email
415
     *
416
     * @return int
417
     */
418
419
    public function isValidEmail($email)
420
    {
421
        return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i', $email);
422
    }
423
}
424