Completed
Push — master ( c8eead...62889a )
by Michael
04:13
created

CheckoutWizard   C

Complexity

Total Complexity 61

Size/Duplication

Total Lines 434
Duplicated Lines 0.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 61
c 1
b 0
f 0
lcom 1
cbo 1
dl 4
loc 434
rs 6.018

15 Methods

Rating   Name   Duplication   Size   Complexity  
C CheckoutWizard() 0 26 7
A prepare_Fieldname() 0 20 3
A process_Fieldname() 0 18 3
A prepare_Fieldtype() 0 9 1
A process_Fieldtype() 0 8 1
B process_lookup() 0 25 4
D prepare_Settings() 0 41 10
B process_Settings() 0 24 3
A prepare_search() 0 17 4
A process_search() 0 18 3
B prepare_defaultvalue() 4 25 5
A process_defaultvalue() 0 11 2
A process_confirm() 0 4 1
A isValidEmail() 0 4 1
F completeCallback() 0 124 13

How to fix   Duplicated Code    Complexity   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CheckoutWizard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CheckoutWizard, and based on these observations, apply Extract Interface, too.

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 '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
    function CheckoutWizard()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
CheckoutWizard 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...
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
10
    {
11
        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...
12
        // start the session and initialize the wizard
13
        if (!isset($_SESSION)) {
14
            session_start();
15
        }
16
        parent::ZervWizard($_SESSION, __CLASS__);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (ZervWizard() instead of CheckoutWizard()). Are you sure this is correct? If so, you might want to change this to $this->ZervWizard().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Coding Style introduced by
PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead
Loading history...
17
18
        $this->addStep('Fieldname', _MA_PEDIGREE_ENTER_FIELD);
19
        if ($this->getValue('field') == 0) { //only for a new field
20
            $this->addStep('Fieldtype', _MA_PEDIGREE_FIELD_TYP_SEL);
21
            if ($this->getValue('fieldtype') == 'selectbox' || $this->getValue('fieldtype') == 'radiobutton') {
22
                $this->addStep('lookup', _MA_PEDIGREE_FIELD_ADD_VALUE);
23
            }
24
        }
25
26
        $this->addStep('Settings', _MA_PEDIGREE_FIELD_PARAM);
27
        if ($this->getValue('hassearch') == 'hassearch') {
28
            $this->addStep('search', _MA_PEDIGREE_SEARCH_PARAMFIELD);
29
        }
30
        if ($this->getValue('fieldtype') != 'picture') {
31
            $this->addStep('defaultvalue', _MA_PEDIGREE_FIELD_DEFAUT);
32
        }
33
        $this->addStep('confirm', _MA_PEDIGREE_FIELDCONFIRM);
34
    }
35
36
    function prepare_Fieldname()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
    {
38
        global $xoopsDB, $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...
39
        if (!$field == 0) // field allready exists (editing mode)
40
        {
41
            $sql    = "SELECT * from " . $xoopsDB->prefix("pedigree_fields") . " WHERE ID=" . $field;
42
            $result = $xoopsDB->query($sql);
43
            while ($row = $xoopsDB->fetchArray($result)) {
44
                $name             = $row['FieldName'];
45
                $fieldexplenation = $row['FieldExplenation'];
46
                $fieldtype        = $row['FieldType'];
47
            }
48
            $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...
49
            $this->setValue('explain', $fieldexplenation);
0 ignored issues
show
Bug introduced by
The variable $fieldexplenation 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...
50
            //set the fieldtype because we wont allow it to be edited
51
            $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...
52
53
        }
54
        $this->setValue('field', $field); //is it a new field or are we editing a field
55
    }
56
57
    /**
58
     * @param $form
59
     *
60
     * @return bool
61
     */
62
    function process_Fieldname(&$form)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
63
    {
64
        $name = $this->coalesce($form['name']);
65
        if (strlen($name) > 0) {
66
            $this->setValue('name', $name);
67
        } else {
68
            $this->addError('name', _MA_PEDIGREE_FIELD_NAM);
69
        }
70
71
        $fieldexplenation = $this->coalesce($form['explain']);
72
        if (strlen($fieldexplenation) > 0) {
73
            $this->setValue('explain', $fieldexplenation);
74
        } else {
75
            $this->addError('explain', _MA_PEDIGREE_FIELD_EXPLAN1);
76
        }
77
78
        return !$this->isError();
79
    }
80
81
    function prepare_Fieldtype()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82
    {
83
        $this->fieldtype[] = array('value' => "radiobutton", 'description' => "Radiobutton");
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...
84
        $this->fieldtype[] = array('value' => "selectbox", 'description' => _MA_PEDIGREE_DROPDOWNFIELD);
85
        $this->fieldtype[] = array('value' => "textbox", 'description' => _MA_PEDIGREE_TEXTBOXFIELD);
86
        $this->fieldtype[] = array('value' => "textarea", 'description' => _MA_PEDIGREE_TEXTAREAFIELD);
87
        $this->fieldtype[] = array('value' => "dateselect", 'description' => _MA_PEDIGREE_DATEFIELD);
88
        $this->fieldtype[] = array('value' => "urlfield", 'description' => _MA_PEDIGREE_URLFIELD);
89
    }
90
91
    /**
92
     * @param $form
93
     *
94
     * @return bool
95
     */
96
    function process_Fieldtype(&$form)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
    {
98
        $this->prepare_Fieldtype();
99
        $fieldtype = $this->coalesce($form['fieldtype']);
100
        $this->setValue('fieldtype', $fieldtype);
101
102
        return !$this->isError();
103
    }
104
105
    /**
106
     * @param $form
107
     *
108
     * @return bool
109
     */
110
    function process_lookup(&$form)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
111
    {
112
113
        $fc = $this->coalesce($form['fc']);
114
        $this->setValue('fc', $fc);
115
        $lookup   = $this->coalesce($form['lookup' . $fc]);
116
        $lookupid = $this->coalesce($form['id' . $fc]);
117
        if (strlen($lookup) > 0) {
118
            $this->setValue('lookup' . $fc, $lookup);
119
            $this->setValue('id' . $fc, $lookupid);
120
        }
121
        $lastlookup = $this->getValue('lookup' . $fc);
122
        if ($lastlookup == "") {
123
            $this->setValue('fc', $fc - 1);
124
        }
125
126
        for ($i = 0; $i < $fc; ++$i) {
127
            $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...
128
        }
129
        //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...
130
        $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...
131
132
        return !$this->isError();
133
        //
134
    }
135
136
    function prepare_Settings()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
137
    {
138
        global $xoopsDB;
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...
139
        if (!$this->getValue('field') == 0) // field allready exists (editing mode)
140
        {
141
            {
142
                $sql = "SELECT * from " . $xoopsDB->prefix("pedigree_fields") . " WHERE ID='" . $this->getValue('field') . "'";
143
            }
144
            $result = $xoopsDB->query($sql);
145
            while ($row = $xoopsDB->fetchArray($result)) {
146
                $hs = $row['HasSearch'];
147
                if ($hs == "1") {
148
                    $this->setValue('hassearch', "hassearch");
149
                }
150
                $vip = $row['ViewInPedigree'];
151
                if ($vip == "1") {
152
                    $this->setValue('viewinpedigree', "viewinpedigree");
153
                }
154
                $via = $row['ViewInAdvanced'];
155
                if ($via == "1") {
156
                    $this->setValue('viewinadvanced', "viewinadvanced");
157
                }
158
                $vipie = $row['ViewInPie'];
159
                if ($vipie == "1") {
160
                    $this->setValue('viewinpie', "viewinpie");
161
                }
162
                $vil = $row['ViewInList'];
163
                if ($vil == "1") {
164
                    $this->setValue('viewinlist', "viewinlist");
165
                }
166
                $lit = $row['Litter'];
167
                if ($lit == "1") {
168
                    $this->setValue('Litter', "Litter");
169
                }
170
                $Glit = $row['Generallitter'];
171
                if ($Glit == "1") {
172
                    $this->setValue('Generallitter', "Generallitter");
173
                }
174
            }
175
        }
176
    }
177
178
    /**
179
     * @param $form
180
     *
181
     * @return bool
182
     */
183
    function process_Settings(&$form)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
184
    {
185
        $hassearch = $this->coalesce($form['hassearch']);
186
        $this->setValue('hassearch', $hassearch);
187
        $viewinpedigree = $this->coalesce($form['viewinpedigree']);
188
        $this->setValue('viewinpedigree', $viewinpedigree);
189
        $viewinadvanced = $this->coalesce($form['viewinadvanced']);
190
        $this->setValue('viewinadvanced', $viewinadvanced);
191
        $viewinpie = $this->coalesce($form['viewinpie']);
192
        $this->setValue('viewinpie', $viewinpie);
193
        $viewinlist = $this->coalesce($form['viewinlist']);
194
        $this->setValue('viewinlist', $viewinlist);
195
        $Litter = $this->coalesce($form['Litter']);
196
        $this->setValue('Litter', $Litter);
197
        $Generallitter = $this->coalesce($form['Generallitter']);
198
        $this->setValue('Generallitter', $Generallitter);
199
200
        //if both litter and general litter are set; unset one of them
201
        if ($this->getValue('Litter') == "Litter" && $this->getValue('Generallitter') == "Generallitter") {
202
            $this->setValue('Generallitter', 0);
203
        }
204
205
        return !$this->isError();
206
    }
207
208
    function prepare_search()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
209
    {
210
        global $xoopsDB;
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...
211
        if (!$this->getValue('field') == 0) // field allready exists (editing mode)
212
        {
213
            $sql    = "SELECT * from " . $xoopsDB->prefix("pedigree_fields") . " WHERE ID=" . $this->getValue('field');
214
            $result = $xoopsDB->query($sql);
215
            while ($row = $xoopsDB->fetchArray($result)) {
216
                if ($this->getValue('hassearch') == "hassearch") {
217
                    $searchname = $row['SearchName'];
218
                    $this->setValue('searchname', $searchname);
219
                    $searchexplain = $row['SearchExplenation'];
220
                    $this->setValue('searchexplain', $searchexplain);
221
                }
222
            }
223
        }
224
    }
225
226
    /**
227
     * @param $form
228
     *
229
     * @return bool
230
     */
231
    function process_search(&$form)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
232
    {
233
        $searchname = $this->coalesce($form['searchname']);
234
        if (strlen($searchname) > 0) {
235
            $this->setValue('searchname', $searchname);
236
        } else {
237
            $this->addError('searchname', 'Please enter the searchname');
238
        }
239
240
        $fieldexplenation = $this->coalesce($form['searchexplain']);
241
        if (strlen($fieldexplenation) > 0) {
242
            $this->setValue('searchexplain', $fieldexplenation);
243
        } else {
244
            $this->addError('searchexplain', 'Please enter the search explanation for this field');
245
        }
246
247
        return !$this->isError();
248
    }
249
250
    function prepare_defaultvalue()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
251
    {
252
        global $xoopsDB;
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...
253
        if (!$this->getValue('field') == 0) // field allready exists (editing mode)
254
        {
255
            $sql    = "SELECT * from " . $xoopsDB->prefix("pedigree_fields") . " WHERE ID=" . $this->getValue('field');
256
            $result = $xoopsDB->query($sql);
257
            while ($row = $xoopsDB->fetchArray($result)) {
258
                $def = $row['DefaultValue'];
259
                $this->setValue('defaultvalue', $def);
260
                if ($row['LookupTable'] == "1") { //we have a lookup table; load values
261
                    $sql    = "SELECT * from " . $xoopsDB->prefix("pedigree_lookup" . $this->getValue('field')) . " order by 'order'";
262
                    $fc     = 0;
263
                    $result = $xoopsDB->query($sql);
264 View Code Duplication
                    while ($row = $xoopsDB->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
265
                        $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...
266
                        ++$fc;
267
                    }
268
                    $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...
269
                    $this->setValue('fc', $fc);
270
                }
271
            }
272
273
        }
274
    }
275
276
    /**
277
     * @param $form
278
     *
279
     * @return bool
280
     */
281
    function process_defaultvalue(&$form)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
282
    {
283
        $defaultvalue = $this->coalesce($form['defaultvalue']);
284
        if (strlen($defaultvalue) >= 0) {
285
            $this->setValue('defaultvalue', $defaultvalue);
286
        } else {
287
            $this->addError('defaultvalue', 'Please enter a defaultvalue');
288
        }
289
290
        return !$this->isError();
291
    }
292
293
    /**
294
     * @param $form
295
     *
296
     * @return bool
297
     */
298
    function process_confirm(&$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...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
299
    {
300
        return !$this->isError();
301
    }
302
303
    function completeCallback()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
304
    {
305
        global $xoopsDB;
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...
306
        //can this field be searched
307
        $search = $this->getValue('hassearch');
308
        if ($search == "hassearch") {
309
            $search        = "1";
310
            $searchname    = $this->getValue('searchname');
311
            $searchexplain = $this->getValue('searchexplain');
312
        } else {
313
            $search        = "0";
314
            $searchname    = "";
315
            $searchexplain = "";
316
        }
317
        //show in pedigree
318
        $viewinpedigree = $this->getValue('viewinpedigree');
319
        if ($viewinpedigree == "viewinpedigree") {
320
            $viewinpedigree = "1";
321
        } else {
322
            $viewinpedigree = "0";
323
        }
324
        //show in advanced
325
        $viewinadvanced = $this->getValue('viewinadvanced');
326
        if ($viewinadvanced == "viewinadvanced") {
327
            $viewinadvanced = "1";
328
        } else {
329
            $viewinadvanced = "0";
330
        }
331
        //show in pie
332
        $viewinpie = $this->getValue('viewinpie');
333
        if ($viewinpie == "viewinpie") {
334
            $viewinpie = "1";
335
        } else {
336
            $viewinpie = "0";
337
        }
338
        //view in list
339
        $viewinlist = $this->getValue('viewinlist');
340
        if ($viewinlist == "viewinlist") {
341
            $viewinlist = "1";
342
        } else {
343
            $viewinlist = "0";
344
        }
345
        //add a litter
346
        $Litter = $this->getValue('Litter');
347
        if ($Litter == "Litter") {
348
            $Litter = "1";
349
        } else {
350
            $Litter = "0";
351
        }
352
        //general litter
353
        $Generallitter = $this->getValue('Generallitter');
354
        if ($Generallitter == "Generallitter") {
355
            $Generallitter = "1";
356
        } else {
357
            $Generallitter = "0";
358
        }
359
360
        if (!$this->getValue('field') == 0) // field allready exists (editing mode)
361
        {
362
            $sql = "UPDATE " . $xoopsDB->prefix("pedigree_fields") . " SET FieldName = '" . htmlSpecialChars($this->getValue('name')) . "', FieldType = '" . $this->getValue('fieldtype')
363
                . "', DefaultValue = '" . $this->getValue('defaultvalue') . "', FieldExplenation = '" . $this->getValue('explain') . "', HasSearch = '" . $search . "', Litter = '" . $Litter
364
                . "', Generallitter = '" . $Generallitter . "', SearchName = '" . $searchname . "', SearchExplenation = '" . $searchexplain . "', ViewInPedigree = '" . $viewinpedigree
365
                . "', ViewInAdvanced = '" . $viewinadvanced . "', ViewInPie = '" . $viewinpie . "', ViewInList = '" . $viewinlist . "' WHERE ID ='" . $this->getValue('field') . "'";
366
            $xoopsDB->queryF($sql);
367
            //possible change defaultvalue for userfield
368
            $sql
369
                = "ALTER TABLE " . $xoopsDB->prefix("pedigree_tree") . " CHANGE `user" . $this->getValue('field') . "` `user" . $this->getValue(
370
                    'field'
371
                ) . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
372
            $xoopsDB->queryF($sql);
373
            $sql
374
                = "ALTER TABLE " . $xoopsDB->prefix("pedigree_temp") . " CHANGE `user" . $this->getValue('field') . "` `user" . $this->getValue(
375
                    'field'
376
                ) . "` VARCHAR( 1024 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
377
            $xoopsDB->queryF($sql);
378
            $sql = "ALTER TABLE " . $xoopsDB->prefix("pedigree_trash") . " CHANGE `user" . $this->getValue('field') . "` `user" . $this->getValue(
379
                    'field'
380
                ) . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
381
            $xoopsDB->queryF($sql);
382
        } else { //this is a new field
383
            $sql    = "SELECT MAX(ID) AS lid from " . $xoopsDB->prefix("pedigree_fields") . " LIMIT 1";
384
            $result = $xoopsDB->query($sql);
385
            while ($row = $xoopsDB->fetchArray($result)) {
386
                $nextfieldnum = $row['lid'] + 1;
387
            }
388
            //add userfield to various tables as a new field.
389
            //allways add at the end of the table
390
            $tables = array('pedigree_tree', 'pedigree_temp', 'pedigree_trash');
391
            foreach ($tables as $table) {
392
                $SQL = "ALTER TABLE " . $xoopsDB->prefix($table) . " ADD `user" . $nextfieldnum . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue(
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...
393
                        'defaultvalue'
394
                    ) . "'";
395
                $xoopsDB->queryF($SQL);
396
            }
397
            //is a lookup table present
398
            $lookup = $this->getValue('lookup1');
399
            if ($lookup == "") {
400
                $lookup = "0";
401
            } else {
402
                $lookup = "1";
403
                //create table for lookupfield
404
                $createtable = "CREATE TABLE " . $xoopsDB->prefix("pedigree_lookup" . $nextfieldnum) . " (`ID` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM";
405
                $xoopsDB->queryF($createtable);
406
                //fill table
407
                $count = $this->getValue('fc');
408
                for ($x = 1; $x < $count + 1; ++$x) {
409
                    $y = $x - 1;
410
                    $sql = "INSERT INTO " . $xoopsDB->prefix("pedigree_lookup" . $nextfieldnum) . " ( `ID` , `value`, `order`) VALUES ('" . $y . "', '" . $this->getValue('lookup' . $x) . "','" . $y
411
                        . "')";
412
                    $xoopsDB->queryF($sql);
413
                }
414
415
            }
416
417
            //Insert new record into pedigree_config
418
            $sql = "INSERT INTO " . $xoopsDB->prefix("pedigree_fields") . " VALUES ('" . $nextfieldnum . "', '1', '" . htmlSpecialChars(
419
                    $this->getValue('name')
420
                ) . "', '" . $this->getValue('fieldtype') . "', '" . $lookup . "', '" . $this->getValue('defaultvalue') . "', '" . $this->getValue(
421
                    'explain'
422
                ) . "', '" . $search . "', '" . $Litter . "', '" . $Generallitter . "', '" . $searchname . "', '" . $searchexplain . "', '" . $viewinpedigree . "', '" . $viewinadvanced . "', '"
423
                . $viewinpie . "', '" . $viewinlist . "','','" . $nextfieldnum . "')";
424
            $xoopsDB->queryF($sql);
425
        }
426
    }
427
428
    /**
429
     * Miscellaneous utility functions
430
     *
431
     * @param $email
432
     *
433
     * @return int
434
     */
435
436
    function isValidEmail($email)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
437
    {
438
        return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i', $email);
439
    }
440
}
441