Completed
Push — master ( 871d94...af939e )
by Michael
03:14
created

add_litter.php ➔ addlitter()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 79
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 48
nc 24
nop 0
dl 0
loc 79
rs 5.1215
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 43 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
$moduleDirName = basename(__DIR__);
6
xoops_loadLanguage('main', $moduleDirName);
7
// Include any common code for this module.
8
require_once(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php');
9
require_once(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/class_field.php');
10
11
$xoopsOption                  = array();
12
$xoopsOption['template_main'] = 'pedigree_addlitter.tpl';
13
include XOOPS_ROOT_PATH . '/header.php';
14
$GLOBALS['xoopsTpl']->assign('page_title', 'Pedigree database - add a litter');
15
16
//check for access
17
$xoopsModule = XoopsModule::getByDirname('pedigree');
18
if (empty($xoopsUser)) {
19
    redirect_header('index.php', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
20
}
21
22
//get module configuration
23
$moduleHandler = xoops_getHandler('module');
24
$module        = $moduleHandler->getByDirname($moduleDirName);
25
$configHandler = xoops_getHandler('config');
26
$moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
27
28
if (!isset($_GET['f'])) {
29
    addlitter();
30
} else {
31
    $f = $_GET['f'];
32
    if ($f === 'sire') {
33
        sire();
34
    }
35
    if ($f === 'dam') {
36
        dam();
37
    }
38
    if ($f === 'check') {
39
        check();
40
    }
41
}
42
43
function addlitter()
0 ignored issues
show
Coding Style introduced by
addlitter 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...
44
{
45
    global $xoopsUser, $xoopsDB, $xoopsOption;
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...
46
47
    //get module configuration
48
    $moduleHandler = xoops_getHandler('module');
49
    $module        = $moduleHandler->getByDirname('pedigree');
50
    $configHandler = xoops_getHandler('config');
51
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
52
53
    //create xoopsform
54
    include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
55
    $searchform = new XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_LITTER, array('[litter]' => $moduleConfig['litter'])), 'searchform', 'add_litter.php?f=sire', 'post');
56
    $searchform->addElement(new XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
57
    //create random value
58
    $random = (mt_rand() % 10000);
59
    $searchform->addElement(new XoopsFormHidden('random', $random));
60
    //find userid
61
    $searchform->addElement(new XoopsFormHidden('userid', $xoopsUser->getVar('uid')));
62
    //create animal object
63
    $animal = new PedigreeAnimal();
64
    //test to find out how many user fields there are...
65
    $fields  = $animal->getNumOfFields();
66
    $textbox = $gender_radio = $newEntry = array();
67
    //create form contents
68
    for ($count = 1; $count < 11; ++$count) {
69
        //name
70
        $searchform->addElement(new XoopsFormLabel($count . '.', strtr(_MA_PEDIGREE_KITT_NAME . $count . '.', array('[animalType]' => $moduleConfig['animalType']))));
71
        $textbox[$count] = new XoopsFormText('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', 'name' . $count, $size = 50, $maxsize = 50, '');
72
        $searchform->addElement($textbox[$count]);
73
        //gender
74
        $gender_radio[$count] = new XoopsFormRadio('<b>' . _MA_PEDIGREE_FLD_GEND . '</b>', 'roft' . $count, $value = '0');
75
        $gender_radio[$count]->addOptionArray(array('0' => strtr(_MA_PEDIGREE_FLD_MALE, array('[male]' => $moduleConfig['male'])), '1' => strtr(_MA_PEDIGREE_FLD_FEMA, array('[female]' => $moduleConfig['female']))));
76
        $searchform->addElement($gender_radio[$count]);
77
        //add userfields
78
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
79
            $userField   = new Field($fields[$i], $animal->getConfig());
80
            $fieldType   = $userField->getSetting('FieldType');
81
            $fieldObject = new $fieldType($userField, $animal);
82
            if ($userField->isActive() && $userField->getSetting('Litter') == '1' && !$userField->isLocked()) {
83
                $newEntry[$count][$i] = $fieldObject->newField($count);
84
                $searchform->addElement($newEntry[$count][$i]);
85
            }
86
        }
87
        //add empty place holder as divider
88
        $searchform->addElement(new XoopsFormLabel('&nbsp;', ''));
89
    }
90
91
    $searchform->addElement(new XoopsFormLabel(_MA_PEDIGREE_ADD_DATA, _MA_PEDIGREE_DATA_INFO . $moduleConfig['litter'] . '.</h2>'));
92
    //add userfields that are not shown in the litter
93
    for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
94
        $userField   = new Field($fields[$i], $animal->getConfig());
95
        $fieldType   = $userField->getSetting('FieldType');
96
        $fieldObject = new $fieldType($userField, $animal);
97
        if ($userField->isActive() && $userField->generalLitter() && !$userField->isLocked()) {
98
            //add the "-" character to the beginning of the fieldname !!!
99
            $newEntry[$i] = $fieldObject->newField('-');
100
            $searchform->addElement($newEntry[$i]);
101
        }
102
    }
103
    //add the breeder to the list for the entire litter
104
    //no need to add the owner here because they will be different for each animal in the litter.
105
    if ($moduleConfig['ownerbreeder'] == '1') {
106
        //breeder
107
        $breeder  = new XoopsFormSelect(_MA_PEDIGREE_FLD_BREE, 'id_breeder', $value = '', $size = 1, $multiple = false);
108
        $queryfok = 'SELECT ID, firstname, lastname from ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' order by `lastname`';
109
        $resfok   = $GLOBALS['xoopsDB']->query($queryfok);
110
        $breeder->addOption(0, $name = 'Unknown');
111
        while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
112
            $breeder->addOption($rowfok['Id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
113
        }
114
        $searchform->addElement($breeder);
115
    }
116
117
    //submit button
118
    $searchform->addElement(new XoopsFormButton('', 'submit', strtr(_MA_PEDIGREE_ADD_SIRE, array('[father]' => $moduleConfig['father'])), 'submit'));
119
    //send to template
120
    $searchform->assign($GLOBALS['xoopsTpl']);
121
}
122
123
function sire()
0 ignored issues
show
Best Practice introduced by
The function sire() has been defined more than once; this definition is ignored, only the first definition in add_dog.php (L189-424) is considered.

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

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

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

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
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 $_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 $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...
124
{
125
    global $xoopsUser, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
126
    $user  = $empty = $columnvalue = array();
127
    $value = '';
128
    //debug option !
129
    //print_r($_POST); die();
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
130
    //get module configuration
131
    $moduleHandler = xoops_getHandler('module');
132
    $module        = $moduleHandler->getByDirname('pedigree');
133
    $configHandler = xoops_getHandler('config');
134
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
135
136
    //check for access
137
    $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...
138
    if (empty($xoopsUser)) {
139
        redirect_header('javascript:history.go(-1)', 3, _NOPERM . '<br />' . _MA_PEDIGREE_REGIST);
140
    }
141
    //    $userid = $_POST['userid'];
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...
142
    //    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...
143
    //        $random = $_POST['random'];
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...
144
    //    }
145
    //    if (isset($_GET['random'])) {
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...
146
    //        $random = $_GET['random'];
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...
147
    //    }
148
    //    if (empty($st)) {
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...
149
    //        $st = 0;
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...
150
    //    }
151
    //    if (isset($_GET['st'])) {
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...
152
    //        $st = $_GET['st'];
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...
153
    //    }
154
    $userid     = XoopsRequest::getInt('userid', 0, 'post');
155
    $random     = XoopsRequest::getInt('random', 0);
156
    $st         = XoopsRequest::getInt('st', 0);
157
    $userfields = '';
0 ignored issues
show
Unused Code introduced by
$userfields 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...
158
    $name       = '';
159
    $roft       = '';
160
    for ($count = 1; $count < 11; ++$count) {
161
        $namelitter = 'name' . $count;
162
        $roftlitter = 'roft' . $count;
163
        //check for an empty name
164
        if ($_POST[$namelitter] !== '') {
165
            $name .= ':' . $_POST[$namelitter];
166
            $roft .= ':' . $_POST[$roftlitter];
167
        } else {
168
            if ($count == '1') {
169
                redirect_header('add_litter.php', 3, _MA_PEDIGREE_ADD_NAMEPLZ);
170
            }
171
        }
172
    }
173
    if (isset($_POST['id_breeder'])) {
174
        $id_breeder = $_POST['id_breeder'];
175
    } else {
176
        $id_breeder = '0';
177
    }
178
179
    //make the redirect
180
    if (!isset($_GET['r'])) {
181
        //create animal object
182
        $animal = new PedigreeAnimal();
183
        //test to find out how many user fields there are..
184
        $fields = $animal->getNumOfFields();
185
        sort($fields);
186
        $usersql = '';
0 ignored issues
show
Unused Code introduced by
$usersql 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...
187
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
188
            $userField   = new Field($fields[$i], $animal->getConfig());
189
            $fieldType   = $userField->getSetting('FieldType');
190
            $fieldObject = new $fieldType($userField, $animal);
191
            $defvalue    = $fieldObject->defaultvalue;
192
            //emtpy string to house the different values for this userfield
193
            $withinfield = '';
194
            for ($count = 1; $count < 11; ++$count) {
195
                if ($_POST['name' . $count] !== '') {
196
                    if (isset($_POST[$count . 'user' . $fields[$i]])) {
197
                        //debug option
198
                        //echo $count.'user'.$fields[$i]."=".$_POST[$count.'user'.$fields[$i]]."<br />";
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
199
                        $withinfield .= ':' . $_POST[$count . 'user' . $fields[$i]];
200
                    } else {
201
                        if ($userField->isActive() && $userField->generalLitter() && !$userField->isLocked()) {
202
                            //use $_POST value if this is a general litter field
203
                            $withinfield .= ':' . $_POST['-user' . $fields[$i]];
204
                        } else {
205
                            //create $withinfield for fields not added to the litter
206
                            $withinfield .= ':' . $defvalue;
207
                        }
208
                    }
209
                }
210
            }
211
            //debug option
212
            //echo "user".$fields[$i]." - ".$withinfield."<br />";
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...
213
            $user{$fields[$i]} = $withinfield;
214
        }
215
        //insert into pedigree_temp
216
        //      $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " VALUES ('" . $random . "','" . PedigreeUtilities::unHtmlEntities($name) . "','0','" . $id_breeder . "','" . $userid . "','" . $roft . "','','','', ''";
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
217
        $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " VALUES ('" . XoopsRequest::getInt($random) . "','" . XoopsRequest::getInt(unHtmlEntities($name)) . "','0','" . XoopsRequest::getInt($id_breeder) . "','" . XoopsRequest::getInt($userid) . "','" . XoopsRequest::getInt($roft) . "','','','', ''";
218
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
219
            $userField   = new Field($fields[$i], $animal->getConfig());
220
            $fieldType   = $userField->getSetting('FieldType');
221
            $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...
222
            //do we only need to create a query for active fields ?
223
            $query .= ",'" . $user{$fields[$i]} . "'";
224
        }
225
        $query .= ')';
226
        //debug options
227
        //echo $query."<br />"; die();
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...
228
        $GLOBALS['xoopsDB']->query($query);
229
        redirect_header('add_litter.php?f=sire&random=' . $random . '&st=' . $st . '&r=1&l=a', 1, strtr(_MA_PEDIGREE_ADD_SIREPLZ, array('[father]' => $moduleConfig['father'])));
230
    }
231
    //find letter on which to start else set to 'a'
232
    if (isset($_GET['l'])) {
233
        $l = $_GET['l'];
234
    } else {
235
        $l = 'a';
236
    }
237
    //assign 'sire' to the template
238
    $GLOBALS['xoopsTpl']->assign('sire', '1');
239
    //create list of males dog to select from
240
    $perp = $moduleConfig['perpage'];
241
    //count total number of dogs
242
    $numdog = 'SELECT ID from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='0' and NAAM LIKE '" . $l . "%'";
243
    $numres = $GLOBALS['xoopsDB']->query($numdog);
244
    //total number of dogs the query will find
245
    $numresults = $GLOBALS['xoopsDB']->getRowsNum($numres);
246
    //total number of pages
247
    $numpages = floor($numresults / $perp) + 1;
248
    if (($numpages * $perp) == ($numresults + $perp)) {
249
        --$numpages;
250
    }
251
    //find current page
252
    $cpage = floor($st / $perp) + 1;
253
    //create alphabet
254
    $pages = '';
255 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...
256
        if ($l == chr($i)) {
257
            $pages .= "<b><a href=\"add_litter.php?f=sire&r=1&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a></b>&nbsp;';
258
        } else {
259
            $pages .= "<a href=\"add_litter.php?f=sire&r=1&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a>&nbsp;';
260
        }
261
    }
262
    $pages .= '-&nbsp;';
263
    $pages .= "<a href=\"add_litter.php?f=sire&r=1&random=" . $random . "&l=Ã…\">Ã…</a>&nbsp;";
264
    $pages .= "<a href=\"add_litter.php?f=sire&r=1&random=" . $random . "&l=Ö\">Ö</a>&nbsp;";
265
    //create linebreak
266
    $pages .= '<br />';
267
    //create previous button
268
    if ($numpages > 1) {
269
        if ($cpage > 1) {
270
            $pages .= "<a href=\"add_litter.php?f=sire&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st - $perp) . "\">" . _MA_PEDIGREE_PREVIOUS . '</a>&nbsp;&nbsp';
271
        }
272
    }
273
    //create numbers
274
    for ($x = 1; $x < ($numpages + 1); ++$x) {
275
        //create line break after 20 number
276
        if (($x % 20) == 0) {
277
            $pages .= '<br />';
278
        }
279
        if ($x != $cpage) {
280
            $pages .= "<a href=\"add_litter.php?f=sire&r=1&l=" . $l . '&random=' . $random . '&st=' . ($perp * ($x - 1)) . "\">" . $x . '</a>&nbsp;&nbsp;';
281
        } else {
282
            $pages .= $x . '&nbsp;&nbsp';
283
        }
284
    }
285
    //create next button
286
    if ($numpages > 1) {
287
        if ($cpage < $numpages) {
288
            $pages .= "<a href=\"add_litter.php?f=sire&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st + $perp) . "\">" . _MA_PEDIGREE_NEXT . '</a>&nbsp;&nbsp';
289
        }
290
    }
291
    //query
292
    $queryString = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft = '0' and NAAM LIKE '" . $l . "%' ORDER BY NAAM LIMIT " . $st . ', ' . $perp;
293
    $result      = $GLOBALS['xoopsDB']->query($queryString);
294
295
    $animal = new PedigreeAnimal();
296
    //test to find out how many user fields there are...
297
    $fields       = $animal->getNumOfFields();
298
    $numofcolumns = 1;
299
    $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...
300 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...
301
        $userField   = new Field($fields[$i], $animal->getConfig());
302
        $fieldType   = $userField->getSetting('FieldType');
303
        $fieldObject = new $fieldType($userField, $animal);
304
        //create empty string
305
        $lookupvalues = '';
306
        if ($userField->isActive() && $userField->inList()) {
307
            if ($userField->hasLookup()) {
308
                $lookupvalues = $userField->lookupField($fields[$i]);
309
                //debug information
310
                //print_r($lookupvalues);
311
            }
312
            $columns[] = array('columnname' => $fieldObject->fieldname, 'columnnumber' => $userField->getId(), 'lookupval' => $lookupvalues);
313
            ++$numofcolumns;
314
            unset($lookupvalues);
315
        }
316
    }
317
318 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...
319
        $empty[] = array('value' => '');
320
    }
321
    $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...
322
        'id'          => '0',
323
        'name'        => '',
324
        'gender'      => '',
325
        'link'        => "<a href=\"add_litter.php?f=dam&random=" . $random . "&selsire=0\">" . strtr(_MA_PEDIGREE_ADD_SIREUNKNOWN, array('[father]' => $moduleConfig['father'])) . '</a>',
326
        'colour'      => '',
327
        'number'      => '',
328
        'usercolumns' => $empty
329
    );
330
331 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...
332
        //create picture information
333
        if ($row['foto'] != '') {
334
            $camera = " <img src=\"assets/images/camera.png\">";
335
        } else {
336
            $camera = '';
337
        }
338
        $name = stripslashes($row['NAAM']) . $camera;
339
        //empty array
340
        unset($columnvalue);
341
        //fill array
342
        for ($i = 1; $i < $numofcolumns; ++$i) {
343
            $x = $columns[$i]['columnnumber'];
344
            if (is_array($columns[$i]['lookupval'])) {
345
                foreach ($columns[$i]['lookupval'] as $key => $keyvalue) {
346
                    if ($key == $row['user' . $x]) {
347
                        $value = $keyvalue['value'];
348
                    }
349
                }
350
                //debug information
351
                ///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...
352
            } //format value - cant use object because of query count
353
            elseif (0 === strpos($row['user' . $x], 'http://')) {
354
                $value = "<a href=\"" . $row['user' . $x] . "\">" . $row['user' . $x] . '</a>';
355
            } else {
356
                $value = $row['user' . $x];
357
            }
358
            $columnvalue[] = array('value' => $value);
359
        }
360
        $dogs[] = array(
361
            'id'          => $row['Id'],
362
            'name'        => $name,
363
            'gender'      => '<img src="assets/images/male.gif">',
364
            'link'        => "<a href=\"add_litter.php?f=dam&random=" . $random . '&selsire=' . $row['Id'] . "\">" . $name . '</a>',
365
            'colour'      => '',
366
            'number'      => '',
367
            'usercolumns' => $columnvalue
368
        );
369
    }
370
371
    //add data to smarty template
372
    //assign dog
373
    $GLOBALS['xoopsTpl']->assign('dogs', $dogs);
374
    $GLOBALS['xoopsTpl']->assign('columns', $columns);
375
    $GLOBALS['xoopsTpl']->assign('numofcolumns', $numofcolumns);
376
    $GLOBALS['xoopsTpl']->assign('tsarray', PedigreeUtilities::sortTable($numofcolumns));
377
    $GLOBALS['xoopsTpl']->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELSIRE, array('[father]' => $moduleConfig['father'])));
378
    $GLOBALS['xoopsTpl']->assign('pages', $pages);
379
}
380
381
function dam()
0 ignored issues
show
Best Practice introduced by
The function dam() has been defined more than once; this definition is ignored, only the first definition in add_dog.php (L426-611) is considered.

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

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

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

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
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...
382
{
383
    global $xoopsUser, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
384
    $value       = '';
385
    $columnvalue = $empty = array();
386
    //get module configuration
387
    $moduleHandler = xoops_getHandler('module');
388
    $module        = $moduleHandler->getByDirname('pedigree');
389
    $configHandler = xoops_getHandler('config');
390
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
391
392
    //    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...
393
    //        $random = $_POST['random'];
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...
394
    //    }
395
    //    if (isset($_GET['random'])) {
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...
396
    //        $random = $_GET['random'];
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...
397
    //    }
398
    //    if (empty($st)) {
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...
399
    //        $st = 0;
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...
400
    //    }
401
    //    if (isset($_GET['st'])) {
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...
402
    //        $st = $_GET['st'];
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...
403
    //    }
404
    $random = XoopsRequest::getInt('random', 0);
405
    $st     = XoopsRequest::getInt('st', 0, 'get');
406
    //make the redirect
407 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...
408
        //insert into pedigree_temp
409
        //      $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...
410
        $query = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' SET father =' . XoopsRequest::getInt('selsire', 0, 'get') . ' WHERE ID=' . $random;
411
        $GLOBALS['xoopsDB']->queryF($query);
412
        redirect_header('add_litter.php?f=dam&random=' . $random . '&st=' . $st . '&r=1', 1, strtr(_MA_PEDIGREE_ADD_SIREOK, array('[mother]' => $moduleConfig['mother'])));
413
    }
414
    //find letter on which to start else set to 'a'
415
    //    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...
416
    //        $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...
417
    //    } 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...
418
    //        $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...
419
    //    }
420
    $l = XoopsRequest::getString('l', 'a', 'get');
421
    //assign sire to the template
422
    $GLOBALS['xoopsTpl']->assign('sire', '1');
423
    //create list of males dog to select from
424
    //    $perp = $moduleConfig['perpage'];
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...
425
    $perp = (int)$moduleConfig['perpage'];
426
    //count total number of dogs
427
    //  $numdog = 'SELECT ID from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='1' and NAAM LIKE '" . $l . "%'";
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
428
    $numdog = 'SELECT ID from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='1' and NAAM LIKE '" . $GLOBALS['xoopsDB']->escape($l) . "%'";
429
    $numres = $GLOBALS['xoopsDB']->query($numdog);
430
    //total number of dogs the query will find
431
    $numresults = $GLOBALS['xoopsDB']->getRowsNum($numres);
432
    //total number of pages
433
    $numpages = floor($numresults / $perp) + 1;
434
    if (($numpages * $perp) == ($numresults + $perp)) {
435
        --$numpages;
436
    }
437
    //find current page
438
    $cpage = floor($st / $perp) + 1;
439
    //create alphabet
440
    $pages = '';
441 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...
442
        if ($l == chr($i)) {
443
            $pages .= "<b><a href=\"add_litter.php?f=dam&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a></b>&nbsp;';
444
        } else {
445
            $pages .= "<a href=\"add_litter.php?f=dam&r=1&random=" . $random . '&l=' . chr($i) . "\">" . chr($i) . '</a>&nbsp;';
446
        }
447
    }
448
    $pages .= '-&nbsp;';
449
    $pages .= "<a href=\"add_litter.php?f=dam&r=1&random=" . $random . "&l=Ã…\">Ã…</a>&nbsp;";
450
    $pages .= "<a href=\"add_litter.php?f=dam&r=1&random=" . $random . "&l=Ö\">Ö</a>&nbsp;";
451
    //create linebreak
452
    $pages .= '<br />';
453
    //create previous button
454
    if ($numpages > 1) {
455
        if ($cpage > 1) {
456
            $pages .= "<a href=\"add_litter.php?f=dam&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st - $perp) . "\">" . _MA_PEDIGREE_PREVIOUS . '</a>&nbsp;&nbsp';
457
        }
458
    }
459
    //create numbers
460
    for ($x = 1; $x < ($numpages + 1); ++$x) {
461
        //create line break after 20 number
462
        if (($x % 20) == 0) {
463
            $pages .= '<br />';
464
        }
465
        if ($x != $cpage) {
466
            $pages .= "<a href=\"add_litter.php?f=dam&r=1&l=" . $l . '&random=' . $random . '&st=' . ($perp * ($x - 1)) . "\">" . $x . '</a>&nbsp;&nbsp;';
467
        } else {
468
            $pages .= $x . '&nbsp;&nbsp';
469
        }
470
    }
471
    //create next button
472
    if ($numpages > 1) {
473
        if ($cpage < $numpages) {
474
            $pages .= "<a href=\"add_litter.php?f=dam&r=1&l=" . $l . '&random=' . $random . '&st=' . ($st + $perp) . "\">" . _MA_PEDIGREE_NEXT . '</a>&nbsp;&nbsp';
475
        }
476
    }
477
    //query
478
    $queryString = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft = '1' and NAAM LIKE '" . $l . "%' ORDER BY NAAM LIMIT " . $st . ', ' . $perp;
479
    $result      = $GLOBALS['xoopsDB']->query($queryString);
480
481
    $animal = new PedigreeAnimal();
482
    //test to find out how many user fields there are...
483
    $fields       = $animal->getNumOfFields();
484
    $numofcolumns = 1;
485
    $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...
486 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...
487
        $userField   = new Field($fields[$i], $animal->getConfig());
488
        $fieldType   = $userField->getSetting('FieldType');
489
        $fieldObject = new $fieldType($userField, $animal);
490
        //create empty string
491
        $lookupvalues = '';
492
        if ($userField->isActive() && $userField->inList()) {
493
            if ($userField->hasLookup()) {
494
                $lookupvalues = $userField->lookupField($fields[$i]);
495
                //debug information
496
                //print_r($lookupvalues);
497
            }
498
            $columns[] = array('columnname' => $fieldObject->fieldname, 'columnnumber' => $userField->getId(), 'lookupval' => $lookupvalues);
499
            ++$numofcolumns;
500
            unset($lookupvalues);
501
        }
502
    }
503
504 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...
505
        $empty[] = array('value' => '');
506
    }
507
    $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...
508
        'id'          => '0',
509
        'name'        => '',
510
        'gender'      => '',
511
        'link'        => "<a href=\"add_litter.php?f=check&random=" . $random . "&seldam=0\">" . strtr(_MA_PEDIGREE_ADD_DAMUNKNOWN, array('[mother]' => $moduleConfig['mother'])) . '</a>',
512
        'colour'      => '',
513
        'number'      => '',
514
        'usercolumns' => $empty
515
    );
516
517 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...
518
        //create picture information
519
        if ($row['foto'] != '') {
520
            $camera = " <img src=\"assets/images/camera.png\">";
521
        } else {
522
            $camera = '';
523
        }
524
        $name = stripslashes($row['NAAM']) . $camera;
525
        //empty array
526
        unset($columnvalue);
527
        //fill array
528
        for ($i = 1; $i < $numofcolumns; ++$i) {
529
            $x = $columns[$i]['columnnumber'];
530
            if (is_array($columns[$i]['lookupval'])) {
531
                foreach ($columns[$i]['lookupval'] as $key => $keyvalue) {
532
                    if ($key == $row['user' . $x]) {
533
                        $value = $keyvalue['value'];
534
                    }
535
                }
536
                //debug information
537
                ///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...
538
            } //format value - cant use object because of query count
539
            elseif (0 === strpos($row['user' . $x], 'http://')) {
540
                $value = "<a href=\"" . $row['user' . $x] . "\">" . $row['user' . $x] . '</a>';
541
            } else {
542
                $value = $row['user' . $x];
543
            }
544
            $columnvalue[] = array('value' => $value);
545
        }
546
        $dogs[] = array(
547
            'id'          => $row['Id'],
548
            'name'        => $name,
549
            'gender'      => '<img src="assets/images/female.gif">',
550
            'link'        => "<a href=\"add_litter.php?f=check&random=" . $random . '&seldam=' . $row['Id'] . "\">" . $name . '</a>',
551
            'colour'      => '',
552
            'number'      => '',
553
            'usercolumns' => $columnvalue
554
        );
555
    }
556
557
    //add data to smarty template
558
    //assign dog
559
    $GLOBALS['xoopsTpl']->assign('dogs', $dogs);
560
    $GLOBALS['xoopsTpl']->assign('columns', $columns);
561
    $GLOBALS['xoopsTpl']->assign('numofcolumns', $numofcolumns);
562
    $GLOBALS['xoopsTpl']->assign('tsarray', PedigreeUtilities::sortTable($numofcolumns));
563
    $GLOBALS['xoopsTpl']->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELDAM, array('[mother]' => $moduleConfig['mother'])));
564
    $GLOBALS['xoopsTpl']->assign('pages', $pages);
565
}
566
567
function check()
0 ignored issues
show
Best Practice introduced by
The function check() has been defined more than once; this definition is ignored, only the first definition in add_breeder.php (L27-47) is considered.

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

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

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

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
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...
568
{
569
    global $xoopsUser, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
570
    $userfields = '';
571
    //get module configuration
572
    $moduleHandler = xoops_getHandler('module');
573
    $module        = $moduleHandler->getByDirname('pedigree');
574
    $configHandler = xoops_getHandler('config');
575
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
576
577
    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...
578
        $random = $_POST['random'];
579
    }
580
    if (isset($_GET['random'])) {
581
        $random = $_GET['random'];
582
    }
583
    //query
584
    $queryString = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' WHERE ID = ' . $random;
585
    $result      = $GLOBALS['xoopsDB']->query($queryString);
586
    $seldam      = XoopsRequest::getInt('seldam', 0, 'get');
587
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
588
        //pull data apart.
589
        if ($row['NAAM'] !== '') {
590
            $genders = explode(':', $row['roft']);
591
            $names   = explode(':', $row['NAAM']);
592
            for ($c = 1, $cMax = count($names); $c < $cMax; ++$c) {
593
                //              $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " VALUES ('','" . addslashes($names[$c]) . "','0','" . $row['id_breeder'] . "','" . $row['user'] . "','" . $genders[$c] . "','" . $_GET['seldam'] . "','" . $row['father'] . "','',''";
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...
594
                $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " VALUES ('','" . $GLOBALS['xoopsDB']->escape($names[$c]) . "','0','" . $GLOBALS['xoopsDB']->escape($row['id_breeder']) . "','" . $GLOBALS['xoopsDB']->escape($row['user']) . "','" . $GLOBALS['xoopsDB']->escape($genders[$c]) . "','" . $GLOBALS['xoopsDB']->escape($seldam) . "','" . $GLOBALS['xoopsDB']->escape($row['father']) . "','',''";
595
                //create animal object
596
                $animal = new PedigreeAnimal();
597
                //test to find out how many user fields there are..
598
                $fields = $animal->getNumOfFields();
599
                sort($fields);
600
                $usersql = '';
0 ignored issues
show
Unused Code introduced by
$usersql 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...
601
                for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
602
                    $userfields{$fields[$i]} = explode(':', $row['user' . $fields[$i]]);
603
                    $query .= ",'" . $userfields{$fields[$i]}
604
                        [$c] . "'";
605
                }
606
                //insert into pedigree
607
                $query .= ');';
608
                $GLOBALS['xoopsDB']->queryF($query);
609
            }
610
        }
611
        $sqlquery = 'DELETE from ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " where ID='" . $random . "'";
0 ignored issues
show
Unused Code introduced by
$sqlquery 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...
612
    }
613
    redirect_header('latest.php', 1, strtr(_MA_PEDIGREE_ADD_LIT_OK, array('[animalTypes]' => $moduleConfig['animalTypes'])));
614
}
615
616
//footer
617
include XOOPS_ROOT_PATH . '/footer.php';
618