Completed
Push — master ( af939e...465698 )
by Michael
13s
created

add_dog.php ➔ check()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 33
nc 32
nop 0
dl 0
loc 53
rs 7.5251
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 46 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// -------------------------------------------------------------------------
3
4
require_once dirname(dirname(__DIR__)) . '/mainfile.php';
5
xoops_load('XoopsRequest');
6
$moduleDirName = basename(__DIR__);
7
xoops_loadLanguage('main', $moduleDirName);
8
// Include any common code for this module.
9
require_once XOOPS_ROOT_PATH . "/modules/{$moduleDirName}/include/common.php";
10
//require_once(XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/class/field.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
11
if (!class_exists('PedigreeField')) {
12
    require_once XOOPS_ROOT_PATH . "/modules/{$moduleDirName}/class/field.php";
13
}
14
15
$xoopsOption['template_main'] = 'pedigree_adddog.tpl';
16
17
include XOOPS_ROOT_PATH . '/header.php';
18
$xoopsTpl->assign('page_title', 'Pedigree database - Update details');
19
20
//check for access
21
$xoopsModule = XoopsModule::getByDirname('pedigree');
22
if (empty($xoopsUser)) {
23
    redirect_header('index.php', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
24
}
25
26
//create function variable from url
27
$f = XoopsRequest::getCmd('f', 'add', 'GET');
28
switch ($f) {
29
    case 'checkName':
30
        checkName();
31
        break;
32
    case 'sire':
33
        sire();
34
        break;
35
    case 'dam':
36
        dam();
37
        break;
38
    case 'check':
39
        check();
40
        break;
41
    default:
42
        addDog();
43
        break;
44
}
45
46
function addDog() {
47
48
    global $xoopsTpl, $xoopsUser, $xoopsDB, $pedigree;
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...
49
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
50
    //get module configuration
51
    $moduleHandler = xoops_getHandler('module');
52
    $module        = $moduleHandler->getByDirname('pedigree');
53
    $configHandler = xoops_getHandler('config');
54
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
55
*/
56
    //check for access
57
    if (empty($xoopsUser)) {
58
        redirect_header('javascript:history.go(-1)', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
59
    }
60
    if ($xoopsUser->getVar('uid') == 0) {
61
        redirect_header('javascript:history.go(-1)', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
62
    }
63
    //create form
64
    include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
65
    $form = new XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_DOG, array('[animalType]' => $pedigree->getConfig('animalType'))), 'dogname', 'add_dog.php?f=checkName', 'POST');
66
    $form->addElement(new XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
67
    //create random value
68
    $random = (mt_rand() % 10000);
69
    $form->addElement(new XoopsFormHidden('random', $random));
70
    //find userid
71
    $form->addElement(new XoopsFormHidden('user', $xoopsUser->getVar('uid')));
72
73
    //name
74
    $form->addElement(new XoopsFormText('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', 'NAAM', $size = 50, $maxsize = 255, $value = ''));
75
    $string = strtr(_MA_PEDIGREE_FLD_NAME_EX, array('[animalType]' => $pedigree->getConfig('animalType')));
76
    $form->addElement(new XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, $string));
77
78
    //submit button
79
    $form->addElement(new XoopsFormButton('', 'button_id', strtr(_MA_PEDIGREE_ADD_DATA, array('[animalType]' => $pedigree->getConfig('animalType'))), 'submit'));
80
81
    //add data (form) to smarty template
82
    $xoopsTpl->assign('form', $form->render());
83
}
84
85
function checkName()
0 ignored issues
show
Coding Style introduced by
checkName 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...
Coding Style introduced by
checkName uses the super-global variable $_GET which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
checkName uses the super-global variable $_POST which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
86
{
87
    //configure global variables
88
    global $xoopsTpl, $xoopsDB, $xoopsUser, $pedigree;
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...
89
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
90
    //get module configuration
91
    $moduleHandler = xoops_getHandler('module');
92
    $module        = $moduleHandler->getByDirname('pedigree');
93
    $configHandler = xoops_getHandler('config');
94
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
95
*/
96
    $name = XoopsRequest::getString('NAAM', '', 'POST');
97
    //query
98
    //$queryString = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE NAAM LIKE'%" . $name . "%' ORDER BY NAAM";
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
99
    $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE NAAM LIKE'%" . $GLOBALS['xoopsDB']->escape($name) . "%' ORDER BY NAAM";
100
    $result      = $GLOBALS['xoopsDB']->query($queryString);
101
    $numresults  = $GLOBALS['xoopsDB']->getRowsNum($result);
102
    if ($numresults >= 1 && !isset($_GET['r'])) {
103
        //create form
104
        include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
105
        $form = new XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_DOG, array('[animalType]' => $pedigree->getConfig('animalType'))), 'dogname', 'add_dog.php?f=checkName&r=1', 'POST');
106
        //other elements
107
        $form->addElement(new XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
108
        $form->addElement(new XoopsFormHidden('NAAM', $_POST['NAAM']));
109
        $form->addElement(new XoopsFormHidden('user', $xoopsUser->getVar('uid')));
110
        while ($row = $GLOBALS['xoopsDB']->fetchBoth($result)) {
111
            //name
112
            $form->addElement(new XoopsFormLabel('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', "<a href=\"dog.php?Id=" . $row['Id'] . "\">" . stripslashes($row['NAAM']) . '</a>'));
113
        }
114
        $form->addElement(new XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, strtr(_MA_PEDIGREE_ADD_KNOWN, array('[animalTypes]' => $pedigree->getConfig('animalTypes')))));
115
        //submit button
116
        $form->addElement(new XoopsFormButton('', 'button_id', strtr(_MA_PEDIGREE_ADD_KNOWNOK, array('[animalType]' => $pedigree->getConfig('animalType'))), 'submit'));
117
        //add data (form) to smarty template
118
        $xoopsTpl->assign('form', $form->render());
119
    } else {
120
        //create form
121
        include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
122
        $form = new XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_DOG, array('[animalType]' => $pedigree->getConfig('animalType'))), 'dogname', 'add_dog.php?f=sire', 'POST');
123
        //added to handle upload
124
        $form->setExtra("enctype='multipart/form-data'");
125
        $form->addElement(new XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
126
        //create random value
127
        $random = (mt_rand() % 10000);
128
        $form->addElement(new XoopsFormHidden('random', $random));
129
        $form->addElement(new XoopsFormHidden('NAAM', htmlspecialchars($_POST['NAAM'], ENT_QUOTES)));
130
        //find userid from previous form
131
        $form->addElement(new XoopsFormHidden('user', $_POST['user']));
132
133
        //name
134
        $form->addElement(new XoopsFormLabel('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', stripslashes($_POST['NAAM'])));
135
        //gender
136
        $gender_radio = new XoopsFormRadio('<b>' . _MA_PEDIGREE_FLD_GEND . '</b>', 'roft', $value = '0');
137
        $gender_radio->addOptionArray(array('0' => strtr(_MA_PEDIGREE_FLD_MALE, array('[male]' => $pedigree->getConfig('male'))), '1' => strtr(_MA_PEDIGREE_FLD_FEMA, array('[female]' => $pedigree->getConfig('female')))));
138
        $form->addElement($gender_radio);
139
        if ('1' == $pedigree->getConfig('ownerbreeder')) {
140
            //breeder
141
            $breeder_select = new XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_BREE . '</b>', $name = 'id_breeder', $value = '0', $size = 1, $multiple = false);
142
            $queryfok       = 'SELECT Id, lastname, firstname from ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY lastname';
143
            $resfok         = $GLOBALS['xoopsDB']->query($queryfok);
144
            $breeder_select->addOption('0', $name = _MA_PEDIGREE_UNKNOWN);
145
            while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
146
                $breeder_select->addOption($rowfok['Id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
147
            }
148
            $form->addElement($breeder_select);
149
            $form->addElement(new XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, strtr(_MA_PEDIGREE_FLD_BREE_EX, array('[animalType]' => $pedigree->getConfig('animalType')))));
150
151
            //owner
152
            $owner_select = new XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_OWNE . '</b>', $name = 'id_owner', $value = '0', $size = 1, $multiple = false);
153
            $queryfok     = 'SELECT Id, lastname, firstname from ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY lastname';
154
            $resfok       = $GLOBALS['xoopsDB']->query($queryfok);
155
            $owner_select->addOption('0', $name = _MA_PEDIGREE_UNKNOWN);
156
            while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
157
                $owner_select->addOption($rowfok['Id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
158
            }
159
            $form->addElement($owner_select);
160
            $form->addElement(new XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, strtr(_MA_PEDIGREE_FLD_OWNE_EX, array('[animalType]' => $pedigree->getConfig('animalType')))));
161
        }
162
        //picture
163
        $max_imgsize = 1024000;
164
        $img_box     = new XoopsFormFile('Image', 'photo', $max_imgsize);
165
        $img_box->setExtra("size ='50'");
166
        $form->addElement($img_box);
167
168
        //create animal object
169
        $animal = new PedigreeAnimal();
170
        //test to find out how many user fields there are..
171
        $fields = $animal->getNumOfFields();
172
173 View Code Duplication
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
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...
174
            $userField   = new PedigreeField($fields[$i], $animal->getConfig());
175
            $fieldType   = $userField->getSetting('fieldtype');
176
            $fieldObject = new $fieldType($userField, $animal);
177
            if ($userField->isActive() && !$userField->isLocked()) {
178
                $newEntry = $fieldObject->newField();
179
                $form->addElement($newEntry);
180
            }
181
            unset($newEntry);
182
        }
183
184
        //submit button
185
        $form->addElement(new XoopsFormButton('', 'button_id', strtr(_MA_PEDIGREE_ADD_SIRE, array('[father]' => $pedigree->getConfig('father'))), 'submit'));
186
187
        //add data (form) to smarty template
188
        $xoopsTpl->assign('form', $form->render());
189
    }
190
}
191
192
function sire() {
0 ignored issues
show
Coding Style introduced by
sire uses the super-global variable $_FILES which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
sire uses the super-global variable $_GET which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
sire uses the super-global variable $_POST which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
sire 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...
193
    global $xoopsTpl, $xoopsUser, $xoopsDB, $pedigree;
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...
194
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
195
    //get module configuration
196
    $moduleHandler = xoops_getHandler('module');
197
    $module        = $moduleHandler->getByDirname('pedigree');
198
    $configHandler = xoops_getHandler('config');
199
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
200
    $empty         = array(); // an empty array
201
*/
202
    //check for access
203 View Code Duplication
    if (!$xoopsUser instanceof XoopsUser) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
204
        redirect_header('javascript:history.go(-1)', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
205
    }
206
//    $user       = isset($_POST['user']) ? $_POST['user'] : null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
207
    $user       = XoopsRequest::getString('user', null, 'POST');
208
    $random     = (empty($random)) ? XoopsRequest::getString('random', null) : $random;
0 ignored issues
show
Bug introduced by
The variable $random seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
209
    $st         = (empty($st)) ? XoopsRequest::getInt('st', 0) : $st;
0 ignored issues
show
Bug introduced by
The variable $st seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
210
    $name       = XoopsRequest::getString('NAAM', '', 'POST');
211
    $roft       = XoopsRequest::getString('roft', '');
212
    $id_owner   = XoopsRequest::getInt('id_owner', 0);
213
    $id_breeder = XoopsRequest::getInt('id_breeder', 0);
214
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
215
    if (empty($random)) {
216
        $random = isset($_POST['random']) ? $_POST['random'] : null;
217
    }
218
    if (isset($_GET['random'])) {
219
        $random = $_GET['random'];
220
    }
221
222
    if (empty($st)) {
223
        $st = 0;
224
    }
225
    if (isset($_GET['st'])) {
226
        $st = $_GET['st'];
227
    }
228
229
    $name = isset($_POST['NAAM']) ? $_POST['NAAM'] : null;
230
    $roft = isset($_POST['roft']) ? $_POST['roft'] : null;
231
232
    $id_owner   = isset($_POST['id_owner']) ? $_POST['id_owner'] : null;
233
    $id_breeder = isset($_POST['id_breeder']) ? $_POST['id_breeder'] : null;
234
*/
235
    $picturefield = isset($_FILES['photo']) ? $_FILES['photo']['name'] : null; // $_FILES['photo']['name'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% 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...
236
    $foto = (empty($picturefield) || $picturefield == '') ? '' : PedigreeUtilities::uploadPicture(0);
237
238
    $numpicturefield = 1;
239
240
    //make the redirect
241
    if (!isset($_GET['r'])) {
242
        if ('' == $name) {
243
            redirect_header('add_dog.php', 1, _MA_PEDIGREE_ADD_NAMEPLZ);
244
        }
245
        //create animal object
246
        $animal = new PedigreeAnimal();
247
        //test to find out how many user fields there are..
248
        $fields = $animal->getNumOfFields();
249
        sort($fields); //sort by Id not by order
250
        $usersql = '';
251
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
252
            $userField   = new PedigreeField($fields[$i], $animal->getConfig());
253
            $fieldType   = $userField->getSetting('fieldtype');
254
            $fieldObject = new $fieldType($userField, $animal);
0 ignored issues
show
Unused Code introduced by
$fieldObject is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
255
            if ($userField->isActive()) {
256
                //check if _FILES variable exists for user picturefield
257
                $currentfield = 'user' . $fields[$i];
258
                $picturefield = $_FILES[$currentfield]['name'];
259
                if ($fieldType === 'Picture' && (!empty($picturefield) || $picturefield != '')) {
260
                    $userpicture = PedigreeUtilities::uploadPicture($numpicturefield);
261
                    $usersql .= ",'" . $userpicture . "'";
262
                    ++$numpicturefield;
263
                } elseif ($userField->isLocked()) {
264
                    //userfield is locked, substitute default value
265
                    $usersql .= ",'" . $userField->defaultvalue . "'";
0 ignored issues
show
Bug introduced by
The property defaultvalue does not seem to exist in PedigreeField.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
266
                } else {
267
                    //echo $fieldType.":".$i.":".$fields[$i]."<br />";
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
268
                    $usersql .= ",'" . PedigreeUtilities::unHtmlEntities($_POST['user' . $fields[$i]]) . "'";
269
                }
270
            } else {
271
                $usersql .= ",''";
272
            }
273
            //echo $fields[$i]."<br/>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
274
        }
275
276
        //insert into pedigree_temp
277
        //        $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " VALUES ('" . $random . "','" . PedigreeUtilities::unHtmlEntities($name) . "','" . $id_owner . "','" . $id_breeder . "','" . $user . "','" . $roft . "','','','" . $foto . "', ''" . $usersql . ')';
278
        $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " VALUES ('" . $GLOBALS['xoopsDB']->escape($random) . "','" . $GLOBALS['xoopsDB']->escape(unHtmlEntities($name)) . "','" . $GLOBALS['xoopsDB']->escape($id_owner) . "','" . $GLOBALS['xoopsDB']->escape($id_breeder) . "','" . $GLOBALS['xoopsDB']->escape($user) . "','" . $GLOBALS['xoopsDB']->escape($roft) . "','','','" . $GLOBALS['xoopsDB']->escape($foto) . "', ''" . $usersql . ')';
279
        //echo $query; die();
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
280
        $GLOBALS['xoopsDB']->query($query);
281
        redirect_header('add_dog.php?f=sire&random=' . $random . '&st=' . $st . '&r=1&l=a', 1, strtr(_MA_PEDIGREE_ADD_SIREPLZ, array('[father]' => $pedigree->getConfig('father'))));
282
    }
283
    //find letter on which to start else set to 'a'
284
    $l = XoopsRequest::getCmd('l', 'a', 'GET');
285
    //assign sire to template
286
    $xoopsTpl->assign('sire', '1');
287
    //create list of males dog to select from
288
    $perp = $pedigree->getConfig('perpage');
289
    //count total number of dogs
290
    $numdog = 'SELECT COUNT(Id) FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='0' AND NAAM LIKE '" . $l . "%'";
291
    $numres = $GLOBALS['xoopsDB']->query($numdog);
292
    //total number of dogs the query will find
293
    list($numresults) = $GLOBALS['xoopsDB']->fetchRow($numres);
294
    //total number of pages
295
    $numpages = floor($numresults / $perp) + 1;
296
    if (($numpages * $perp) == ($numresults + $perp)) {
297
        --$numpages ;
298
    }
299
    //find current page
300
    $cpage = floor($st / $perp) + 1;
301
    //create alphabet
302
    $pages = '';
303 View Code Duplication
    for ($i = 65; $i <= 90; ++$i) {
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...
304
        if ($l == chr($i)) {
305
            $pages .= "<b><a href=\"add_dog.php?f=sire&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a></b>&nbsp;';
306
        } else {
307
            $pages .= "<a href=\"add_dog.php?f=sire&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a>&nbsp;';
308
        }
309
    }
310
    $pages .= '-&nbsp;';
311
    $pages .= "<a href=\"add_dog.php?f=sire&r=1&random=" . $random . "&l=Ã…\">Ã…</a>&nbsp;";
312
    $pages .= "<a href=\"add_dog.php?f=sire&r=1&random=" . $random . "&l=Ö\">Ö</a>&nbsp;";
313
    //create linebreak
314
    $pages .= '<br />';
315
    //create previous button
316
    if ($numpages > 1) {
317
        if ($cpage > 1) {
318
            $pages .= "<a href=\"add_dog.php?f=sire&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st - $perp) . "\">" . _MA_PEDIGREE_PREVIOUS . '</a>&nbsp;&nbsp';
319
        }
320
    }
321
    //create numbers
322
    for ($x = 1; $x < ($numpages + 1); ++$x) {
323
        //create line break after 20 number
324
        if (($x % 20) == 0) {
325
            $pages .= '<br />';
326
        }
327
        if ($x != $cpage) {
328
            $pages .= "<a href=\"add_dog.php?f=sire&r=1&l=" . $l . '&random=' . $random . '&st=' . ($perp * ($x - 1)) . "\">" . $x . '</a>&nbsp;&nbsp;';
329
        } else {
330
            $pages .= $x . '&nbsp;&nbsp';
331
        }
332
    }
333
    //create next button
334
    if ($numpages > 1) {
335
        if ($cpage < $numpages) {
336
            $pages .= "<a href=\"add_dog.php?f=sire&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st + $perp) . "\">" . _MA_PEDIGREE_NEXT . '</a>&nbsp;&nbsp';
337
        }
338
    }
339
340
    //query
341
    $queryString = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft = '0' and NAAM like '" . $l . "%'ORDER BY NAAM LIMIT " . $st . ', ' . $perp;
342
    $result      = $GLOBALS['xoopsDB']->query($queryString);
343
344
    $animal = new PedigreeAnimal();
345
    //test to find out how many user fields there are...
346
    $fields       = $animal->getNumOfFields();
347
    $numofcolumns = 1;
348
    $columns[]    = array('columnname' => 'Name');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$columns was never initialized. Although not strictly required by PHP, it is generally a good practice to add $columns = 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...
349
    for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
350
        $userField   = new PedigreeField($fields[$i], $animal->getConfig());
351
        $fieldType   = $userField->getSetting('fieldtype');
352
        $fieldObject = new $fieldType($userField, $animal);
353
        //create empty string
354
        $lookupvalues = '';
355
        if ($userField->isActive() && $userField->inList()) {
356
            if ($userField->hasLookup()) {
357
                $lookupvalues = $userField->lookupField($fields[$i]);
358
                //debug information
359
                //print_r($lookupvalues);
360
            }
361
            $columns[] = array('columnname' => $fieldObject->fieldname, 'columnnumber' => $userField->getId(), 'lookupval' => $lookupvalues);
362
            ++$numofcolumns;
363
            unset($lookupvalues);
364
        }
365
    }
366
367 View Code Duplication
    for ($i = 1; $i < $numofcolumns; ++$i) {
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...
368
        $empty[] = array('value' => '');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$empty was never initialized. Although not strictly required by PHP, it is generally a good practice to add $empty = 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...
369
    }
370
    $dogs [] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dogs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dogs = 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...
371
        'id'          => '0',
372
        'name'        => '',
373
        'gender'      => '',
374
        'link'        => "<a href=\"add_dog.php?f=dam&random=" . $random . "&selsire=0\">" . strtr(_MA_PEDIGREE_ADD_SIREUNKNOWN, array('[father]' => $pedigree->getConfig('father'))) . '</a>',
375
        'colour'      => '',
376
        'number'      => '',
377
        'usercolumns' => $empty
0 ignored issues
show
Bug introduced by
The variable $empty 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...
378
    );
379
380 View Code Duplication
    while (false !== ($row = $GLOBALS['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...
381
        //create picture information
382
        if ($row['foto'] != '') {
383
            $camera = " <img src=\"assets/images/camera.png\">";
384
        } else {
385
            $camera = '';
386
        }
387
        $name = stripslashes($row['NAAM']) . $camera;
388
        //empty array
389
        unset($columnvalue);
390
        //fill array
391
        for ($i = 1; $i < $numofcolumns; ++$i) {
392
            $x = $columns[$i]['columnnumber'];
393
            if (is_array($columns[$i]['lookupval'])) {
394
                foreach ($columns[$i]['lookupval'] as $key => $keyvalue) {
395
                    if ($key == $row['user' . $x]) {
396
                        $value = $keyvalue['value'];
397
                    }
398
                }
399
                //debug information
400
                ///echo $columns[$i]['columnname']."is an array !";
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
401
            } //format value - cant use object because of query count
402
            elseif (0 === strpos($row['user' . $x], 'http://')) {
403
                $value = "<a href=\"" . $row['user' . $x] . "\">" . $row['user' . $x] . '</a>';
404
            } else {
405
                $value = $row['user' . $x];
406
            }
407
            $columnvalue[] = array('value' => $value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$columnvalue was never initialized. Although not strictly required by PHP, it is generally a good practice to add $columnvalue = 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...
Bug introduced by
The variable $value 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...
408
        }
409
        $dogs[] = array(
410
            'id'          => $row['Id'],
411
            'name'        => $name,
412
            'gender'      => '<img src="assets/images/male.gif">',
413
            'link'        => "<a href=\"add_dog.php?f=dam&random=" . $random . '&selsire=' . $row['Id'] . "\">" . $name . '</a>',
414
            'colour'      => '',
415
            'number'      => '',
416
            'usercolumns' => $columnvalue
0 ignored issues
show
Bug introduced by
The variable $columnvalue 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...
417
        );
418
    }
419
420
    //add data to smarty template
421
    //assign dog
422
    $xoopsTpl->assign('dogs', $dogs);
423
    $xoopsTpl->assign('columns', $columns);
424
    $xoopsTpl->assign('numofcolumns', $numofcolumns);
425
    $xoopsTpl->assign('tsarray', PedigreeUtilities::sortTable($numofcolumns));
426
    //assign links
427
    $xoopsTpl->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELSIRE, array('[father]' => $pedigree->getConfig('father'))));
428
    $xoopsTpl->assign('pages', $pages);
429
}
430
431
function dam()
0 ignored issues
show
Coding Style introduced by
dam uses the super-global variable $_GET which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
dam 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...
432
{
433
    global $xoopsTpl, $xoopsUser, $xoopsDB, $pedigree;
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...
434
435
    //get module configuration
436
    $moduleHandler = xoops_getHandler('module');
437
    $module        = $moduleHandler->getByDirname('pedigree');
438
    $configHandler = xoops_getHandler('config');
439
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
$moduleConfig is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
440
    $empty         = array(); // an empty array
441
442
    //check for access
443
    $xoopsModule = XoopsModule::getByDirname('pedigree');
0 ignored issues
show
Unused Code introduced by
$xoopsModule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
444
    if (empty($xoopsUser)) {
445
        redirect_header('javascript:history.go(-1)', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
446
    }
447
    //    if (empty($random)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
448
    //$random = isset($_POST['random']) ? $_POST['random'] : null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
449
    //}
450
    //if (isset($_GET['random'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% 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...
451
    //$random = $_GET['random'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
452
    //}
453
    //if (empty($st)) {
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...
454
    //$st = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
455
    //}
456
    //if (isset($_GET['st'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% 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...
457
    //$st = $_GET['st'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
458
    //}
459
    $random = XoopsRequest::getInt('random', 0);
460
    $st     = XoopsRequest::getInt('st', 0, 'get');
461
    //find letter on which to start else set to 'a'
462
    //    if (isset($_GET['l'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% 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...
463
    //        $l = $_GET['l'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
464
    //    } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
465
    //        $l = 'a';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
466
    //    }
467
    $l = XoopsRequest::getString('l', 'a', 'get');
468
    //make the redirect
469 View Code Duplication
    if (!isset($_GET['r'])) {
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...
470
        //insert into pedigree_temp
471
        //        $query = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' SET father =' . $_GET['selsire'] . ' WHERE ID=' . $random;
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
472
        //        $GLOBALS['xoopsDB']->queryF($query);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% 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...
473
        $query = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' SET father =' . XoopsRequest::getInt('selsire', 0, 'get') . ' WHERE Id=' . $random;
474
        $GLOBALS['xoopsDB']->queryF($query);
475
        redirect_header('add_dog.php?f=dam&random=' . $random . '&st=' . $st . '&r=1&l=a', 1, strtr(_MA_PEDIGREE_ADD_SIREOK, array('[mother]' => $pedigree->getConfig('mother'))));
476
    }
477
478
    $xoopsTpl->assign('sire', '1');
479
    //create list of males dog to select from
480
    $perp = $pedigree->getConfig('perpage');
481
    //count total number of dogs
482
    $numdog = 'SELECT COUNT(Id) FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='1' AND NAAM LIKE '" . $l . "%'";
483
    $numres = $GLOBALS['xoopsDB']->query($numdog);
484
    list($numresults) = $GLOBALS['xoopsDB']->fetchRow($numres);
485
    $numpages = floor($numresults / $perp) + 1;
486
    if (($numpages * $perp) == ($numresults + $perp)) {
487
        --$numpages;
488
    }
489
    $cpage = floor($st / $perp) + 1;
490
    //create alphabet
491
    $pages = '';
492 View Code Duplication
    for ($i = 65; $i <= 90; ++$i) {
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...
493
        if ($l == chr($i)) {
494
            $pages .= "<b><a href=\"add_dog.php?f=dam&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a></b>&nbsp;';
495
        } else {
496
            $pages .= "<a href=\"add_dog.php?f=dam&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a>&nbsp;';
497
        }
498
    }
499
    $pages .= '-&nbsp;';
500
    $pages .= "<a href=\"add_dog.php?f=dam&r=1&random=" . $random . "&l=Ã…\">Ã…</a>&nbsp;";
501
    $pages .= "<a href=\"add_dog.php?f=dam&r=1&random=" . $random . "&l=Ö\">Ö</a>&nbsp;";
502
    $pages .= '<br />';
503
    //create previous button
504
    if ($numpages > 1) {
505
        if ($cpage > 1) {
506
            $pages .= "<a href=\"add_dog.php?f=dam&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st - $perp) . "\">" . _MA_PEDIGREE_PREVIOUS . '</a>&nbsp;&nbsp';
507
        }
508
    }
509
    //create numbers
510
    for ($x = 1; $x < ($numpages + 1); ++$x) {
511
        //create line break after 20 number
512
        if (($x % 20) == 0) {
513
            $pages .= '<br />';
514
        }
515
        if ($x != $cpage) {
516
            $pages .= "<a href=\"add_dog.php?f=dam&r=1&l=" . $l . '&random=' . $random . '&st=' . ($perp * ($x - 1)) . "\">" . $x . '</a>&nbsp;&nbsp;';
517
        } else {
518
            $pages .= $x . '&nbsp;&nbsp';
519
        }
520
    }
521
    //create next button
522
    if ($numpages > 1) {
523
        if ($cpage < $numpages) {
524
            $pages .= "<a href=\"add_dog.php?f=dam&l=" . $l . '&r=1&random=' . $random . '&st=' . ($st + $perp) . "\">" . _MA_PEDIGREE_NEXT . '</a>&nbsp;&nbsp;';
525
        }
526
    }
527
528
    //query
529
    $queryString = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft = '1' and NAAM LIKE '" . $l . "%' ORDER BY NAAM LIMIT " . $st . ', ' . $perp;
530
    $result      = $GLOBALS['xoopsDB']->query($queryString);
531
532
    $animal = new PedigreeAnimal();
533
    //test to find out how many user fields there are...
534
    $fields       = $animal->getNumOfFields();
535
    $numofcolumns = 1;
536
    $columns[]    = array('columnname' => 'Name');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$columns was never initialized. Although not strictly required by PHP, it is generally a good practice to add $columns = 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...
537
    for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
538
        $userField   = new PedigreeField($fields[$i], $animal->getConfig());
539
        $fieldType   = $userField->getSetting('fieldtype');
540
        $fieldObject = new $fieldType($userField, $animal);
541
        //create empty string
542
        $lookupvalues = '';
543
        if ($userField->isActive() && $userField->inList()) {
544
            if ($userField->hasLookup()) {
545
                $lookupvalues = $userField->lookupField($fields[$i]);
546
                //debug information
547
                //print_r($lookupvalues);
548
            }
549
            $columns[] = array('columnname' => $fieldObject->fieldname, 'columnnumber' => $userField->getId(), 'lookupval' => $lookupvalues);
550
            ++$numofcolumns;
551
            unset($lookupvalues);
552
        }
553
    }
554
555 View Code Duplication
    for ($i = 1; $i < $numofcolumns; ++$i) {
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...
556
        $empty[] = array('value' => '');
557
    }
558
    $dogs [] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dogs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dogs = 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...
559
        'id'          => '0',
560
        'name'        => '',
561
        'gender'      => '',
562
        'link'        => "<a href=\"add_dog.php?f=check&random=" . $random . "&seldam=0\">" . strtr(_MA_PEDIGREE_ADD_DAMUNKNOWN, array('[mother]' => $pedigree->getConfig('mother'))) . '</a>',
563
        'colour'      => '',
564
        'number'      => '',
565
        'usercolumns' => $empty
566
    );
567
568 View Code Duplication
    while (false !== ($row = $GLOBALS['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...
569
        //create picture information
570
        if ($row['foto'] != '') {
571
            $camera = " <img src=\"assets/images/camera.png\">";
572
        } else {
573
            $camera = '';
574
        }
575
        $name = stripslashes($row['NAAM']) . $camera;
576
        //empty array
577
        unset($columnvalue);
578
        //fill array
579
        for ($i = 1; $i < $numofcolumns; ++$i) {
580
            $x = $columns[$i]['columnnumber'];
581
            if (is_array($columns[$i]['lookupval'])) {
582
                foreach ($columns[$i]['lookupval'] as $key => $keyvalue) {
583
                    if ($key == $row['user' . $x]) {
584
                        $value = $keyvalue['value'];
585
                    }
586
                }
587
                //debug information
588
                ///echo $columns[$i]['columnname']."is an array !";
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
589
            } //format value - cant use object because of query count
590
            elseif (0 === strpos($row['user' . $x], 'http://')) {
591
                $value = "<a href=\"" . $row['user' . $x] . "\">" . $row['user' . $x] . '</a>';
592
            } else {
593
                $value = $row['user' . $x];
594
            }
595
            $columnvalue[] = array('value' => $value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$columnvalue was never initialized. Although not strictly required by PHP, it is generally a good practice to add $columnvalue = 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...
Bug introduced by
The variable $value 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...
596
        }
597
        $dogs[] = array(
598
            'id'          => $row['Id'],
599
            'name'        => $name,
600
            'gender'      => '<img src="assets/images/female.gif">',
601
            'link'        => "<a href=\"add_dog.php?f=check&random=" . $random . '&seldam=' . $row['Id'] . "\">" . $name . '</a>',
602
            'colour'      => '',
603
            'number'      => '',
604
            'usercolumns' => $columnvalue
0 ignored issues
show
Bug introduced by
The variable $columnvalue 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...
605
        );
606
    }
607
608
    //add data to smarty template
609
    //assign dog
610
    $xoopsTpl->assign('dogs', $dogs);
611
    $xoopsTpl->assign('columns', $columns);
612
    $xoopsTpl->assign('numofcolumns', $numofcolumns);
613
    $xoopsTpl->assign('tsarray', PedigreeUtilities::sortTable($numofcolumns));
614
    $xoopsTpl->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELDAM, array('[mother]' => $pedigree->getConfig('mother'))));
615
    $xoopsTpl->assign('pages', $pages);
616
}
617
618
function check()
0 ignored issues
show
Coding Style introduced by
check uses the super-global variable $_POST which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
check uses the super-global variable $_GET which is generally not recommended.

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

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

// Better
class Router
{
    private $host;

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

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

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

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
check 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...
619
{
620
    global $xoopsTpl, $xoopsUser, $xoopsDB, $pedigree;
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...
621
622
    //get module configuration
623
    $moduleHandler = xoops_getHandler('module');
624
    $module        = $moduleHandler->getByDirname('pedigree');
625
    $configHandler = xoops_getHandler('config');
626
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
$moduleConfig is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
627
628
    //check for access
629
    $xoopsModule = XoopsModule::getByDirname('pedigree');
0 ignored issues
show
Unused Code introduced by
$xoopsModule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
630
    if (empty($xoopsUser)) {
631
        redirect_header('index.php', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
632
    }
633
    if (empty($random)) {
0 ignored issues
show
Bug introduced by
The variable $random seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
634
        $random = $_POST['random'];
635
    }
636
    if (isset($_GET['random'])) {
637
        $random = $_GET['random'];
638
    }
639
640
    //query
641
    $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' WHERE Id = ' . $random;
642
    $result      = $GLOBALS['xoopsDB']->query($queryString);
643
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
644
        //create animal object
645
        $animal = new PedigreeAnimal();
646
        //test to find out how many user fields there are..
647
        $fields = $animal->getNumOfFields();
648
        sort($fields);
649
        $usersql = '';
650
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
651
            $userField   = new PedigreeField($fields[$i], $animal->getConfig());
652
            $fieldType   = $userField->getSetting('fieldtype');
653
            $fieldObject = new $fieldType($userField, $animal);
654
            if ($userField->isActive()) {
655
                $usersql .= ",'" . addslashes($row['user' . $fields[$i]]) . "'";
656
            } else {
657
                $usersql .= ",'" . $fieldObject->defaultvalue . "'";
658
            }
659
            //echo $fields[$i]."<br/>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
660
        }
661
        //insert into pedigree
662
        //$query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " VALUES ('','" . addslashes($row['NAAM']) . "','" . $row['id_owner'] . "','" . $row['id_breeder'] . "','" . $row['user'] . "','" . $row['roft'] . "','" . $_GET['seldam'] . "','" . $row['father'] . "','" . addslashes($row['foto']) . "',''" . $usersql . ')';
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
663
        $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " VALUES ('','" . $GLOBALS['xoopsDB']->escape($row['NAAM']) . "','" . $GLOBALS['xoopsDB']->escape($row['id_owner']) . "','" . $GLOBALS['xoopsDB']->escape($row['id_breeder']) . "','" . $GLOBALS['xoopsDB']->escape($row['user']) . "','" . $GLOBALS['xoopsDB']->escape($row['roft']) . "','" . $GLOBALS['xoopsDB']->escape($_GET['seldam']) . "','" . $GLOBALS['xoopsDB']->escape($row['father']) . "','" . $GLOBALS['xoopsDB']->escape($row['foto']) . "',''" . $usersql . ')';
664
        $GLOBALS['xoopsDB']->queryF($query);
665
        //echo $query; die();
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
666
    }
667
    $sqlquery = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " WHERE Id='" . $random . "'";
668
    $GLOBALS['xoopsDB']->queryF($sqlquery);
669
    redirect_header('latest.php', 1, strtr(_MA_PEDIGREE_ADD_OK, array('[animalType]' => $pedigree->getConfig('animalType'))));
670
}
671
672
//footer
673
include XOOPS_ROOT_PATH . '/footer.php';
674