Completed
Push — master ( 8ca430...3024c9 )
by Michael
03:12
created

altcat.php ➔ makeTreeCheckTable()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 29
c 2
b 0
f 0
nc 42
nop 5
dl 0
loc 52
rs 6.8493

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

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
 * Module: XoopsTube
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 *
9
 * PHP version 5
10
 *
11
 * @category        Module
12
 * @package         Xoopstube
13
 * @author          XOOPS Development Team
14
 * @copyright       2001-2013 The XOOPS Project
15
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @version         $Id$
17
 * @link            http://sourceforge.net/projects/xoops/
18
 * @since           1.0.6
19
 */
20
21
include_once __DIR__ . '/admin_header.php';
22
23
global $xoopsModuleConfig;
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...
24
25
$op  = xtubeCleanRequestVars($_REQUEST, 'op', '');
26
$lid = xtubeCleanRequestVars($_REQUEST, 'lid', 0);
27
28
/**
29
 * @param        $xt
30
 * @param        $itemid
31
 * @param        $title
32
 * @param        $checks
33
 * @param string $order
34
 */
35
function makeTreeCheckTable($xt, $itemid, $title, $checks, $order = '')
0 ignored issues
show
Coding Style introduced by
makeTreeCheckTable 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...
36
{
37
    global $xtubemyts;
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...
38
39
    echo '<div style="text-align: left;">';
40
    echo '<form name="altcat" method="post" action="' . xoops_getenv('PHP_SELF') . '">';
41
    echo '<table width="100%" callspacing="1" class="outer">';
42
    $sql = 'SELECT ' . $xt->id . ', ' . $title . ' FROM ' . $xt->table . ' WHERE ' . $xt->pid . '=0' . ' ORDER BY ' . $title;
43
    if ($order != '') {
44
        $sql .= ' ORDER BY ' . $order;
45
    }
46
    $result = $xt->db->query($sql);
47
48
    while (list($cid, $name) = $xt->db->fetchRow($result)) {
49
        $checked  = array_key_exists($cid, $checks) ? "checked='checked'" : "";
50
        $disabled = ($cid == intval($_GET['cid'])) ? "disabled='yes'" : "";
51
        $level    = 1;
0 ignored issues
show
Unused Code introduced by
$level 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...
52
        echo '
53
        <tr style="text-align: left;">
54
         <td width="30%" class="head">' . $name . '</td>
55
         <td class="head">
56
             <input type="checkbox" name="cid-' . $cid . '" value="0" ' . $checked . ' ' . $disabled . '/>
57
         </td>
58
        </tr>';
59
        $arr = $xt->getChildTreeArray($cid, $order);
60
        foreach ($arr as $cat) {
61
            $cat['prefix'] = str_replace('.', '-', $cat['prefix']);
62
            $catpath       = '&nbsp;' . $cat['prefix'] . '&nbsp;' . $xtubemyts->htmlSpecialCharsStrip($cat[$title]);
63
            $checked       = array_key_exists($cat['cid'], $checks) ? "checked='checked'" : "";
64
            $disabled      = ($cat['cid'] == intval($_GET['cid'])) ? "disabled='yes'" : "";
65
            $level         = substr_count($cat['prefix'], '-') + 1;
0 ignored issues
show
Unused Code introduced by
$level 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...
66
//          echo "<tr><td>" . $catpath . "<input type='checkbox' name='cid-" . $cat['cid'] . "' value='0' " . $checked . " " . $disabled . "/></td></tr>\n";
67
            echo '
68
        <tr style="text-align: left;">
69
         <td width="30%" class="even">' . $catpath . '</td>
70
         <td class="even">
71
             <input type="checkbox" name="cid-' . $cat['cid'] . '" value="0" ' . $checked . ' ' . $disabled . '/>
72
         </td>
73
        </tr>';
74
        }
75
76
    }
77
    echo '<tr>
78
           <td width="30%"></td>
79
           <td style="text-align: left;">
80
            <input type="submit" class="mainbutton" value="save" />
81
            <input type="hidden" name="op" value="save" />
82
            <input type="hidden" name="lid" value="' . $itemid . '" />
83
            </td>
84
          </tr>';
85
    echo '</table></form></div>';
86
}
87
88
switch (strtolower($op)) {
89
    case 'save':
90
        // first delete all alternate categories for this topic
91
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('xoopstube_altcat') . ' WHERE lid=' . $lid;
92
        if (!$result = $xoopsDB->query($sql)) {
93
            XoopsErrorHandler_HandleError(E_USER_WARNING, $sql, __FILE__, __LINE__);
94
95
            return false;
96
        }
97
98
        $k = array_keys($_REQUEST);
99
        foreach ($k as $sid) {
100
            if (preg_match("/cid-([0-9]*)/", $sid, $cid)) {
101
                $sql
102
                    = 'INSERT INTO ' . $xoopsDB->prefix('xoopstube_altcat') . '(cid, lid) VALUES("' . $cid[1] . '","' . $lid . '")';
103
                if (!$result = $xoopsDB->query($sql)) {
104
                    XoopsErrorHandler_HandleError(E_USER_WARNING, $sql, __FILE__, __LINE__);
105
106
                    return false;
107
                }
108
            }
109
        }
110
        redirect_header('index.php', 1, _AM_XOOPSTUBE_ALTCAT_CREATED);
111
        break;
112
113
    case 'main':
114
    default:
115
        xoops_cp_header();
116
        //xtubeRenderAdminMenu(_AM_XOOPSTUBE_MALTCAT);
117
        echo '<fieldset><legend style="font-weight: bold; color: #0A3760;">' . _AM_XOOPSTUBE_ALTCAT_MODIFYF . '</legend>
118
          <div style="padding: 8px;">' . _AM_XOOPSTUBE_ALTCAT_INFOTEXT . '</div>
119
          </fieldset>';
120
121
        echo '<div style="text-align: left;"><h3> ' . $_REQUEST['title'] . ' </h3></div>';
122
        // Get an array of all alternate categories for this topic
123
        $sql     = $xoopsDB->query(
124
            'SELECT cid FROM ' . $xoopsDB->prefix('xoopstube_altcat') . ' WHERE lid="' . $lid . '" ORDER BY lid'
125
        );
126
        $altcats = array();
127
        while ($altcat = $xoopsDB->fetchArray($sql)) {
128
            $altcats[$altcat['cid']] = true;
129
        }
130
        $mytree = new XoopstubeTree($xoopsDB->prefix('xoopstube_cat'), 'cid', 'pid');
131
        makeTreeCheckTable($mytree, $lid, 'title', $altcats);
132
        xoops_cp_footer();
133
}
134