Completed
Push — master ( c1777d...d193e2 )
by Michael
13:22
created

random_term.php ➔ b_lxentries_random_show()   D

Complexity

Conditions 9
Paths 104

Size

Total Lines 136
Code Lines 110

Duplication

Lines 15
Ratio 11.03 %

Importance

Changes 0
Metric Value
cc 9
eloc 110
nc 104
nop 0
dl 15
loc 136
rs 4.7741
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 16 and the first side effect is on line 11.

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
 * Module: Lexikon - glossary module
5
 * Version: v 1.00
6
 * Release Date: 8 May 2004
7
 * Author: hsalazar
8
 * Licence: GNU
9
 */
10
11
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
12
13
/**
14
 * @return array
15
 */
16
function b_lxentries_random_show()
17
{
18
    global $xoopsDB, $xoopsUser, $xoopsConfig, $xoopsModule;
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...
19
    $myts = MyTextSanitizer::getInstance();
20
21
    /** @var XoopsModuleHandler $moduleHandler */
22
    $moduleHandler = xoops_getHandler('module');
23
    $lexikon       = $moduleHandler->getByDirname('lexikon');
24
25 View Code Duplication
    if (!isset($lxConfig)) {
0 ignored issues
show
Bug introduced by
The variable $lxConfig seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

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...
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...
26
        $configHandler = xoops_getHandler('config');
27
        $lxConfig      = $configHandler->getConfigsByCat(0, $lexikon->getVar('mid'));
28
    }
29
    $groups       = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
30
    $gpermHandler = xoops_getHandler('groupperm');
31
    $module_id    = $lexikon->getVar('mid');
32
    $allowed_cats = $gpermHandler->getItemIds('lexikon_view', $groups, $module_id);
33
    $catids       = implode(',', $allowed_cats);
34
    $catperms     = " AND categoryID IN ($catids) ";
35
36
    $adminlinks     = '';
37
    $block          = array();
38
    $block['title'] = _MB_LEXIKON_RANDOMTITLE;
39
40
    list($numrows) = $xoopsDB->fetchRow($xoopsDB->query('SELECT COUNT(entryID) FROM ' . $xoopsDB->prefix('lxentries') . " WHERE offline= '0' AND block = '1' " . $catperms . ' '));
41
42 View Code Duplication
    if ($numrows > 1) {
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...
43
        --$numrows;
44
        mt_srand((double)microtime() * 1000000);
45
        $entrynumber = mt_rand(0, $numrows);
46
    } else {
47
        $entrynumber = 0;
48
    }
49
50
    $result = $xoopsDB->query('SELECT entryID, categoryID, term, definition FROM ' . $xoopsDB->prefix('lxentries') . " WHERE offline = '0' AND block = '1' " . $catperms . " LIMIT $entrynumber, 1");
51
52
    while ($myrow = $xoopsDB->fetchArray($result)) {
53
        //$entryID = (int)($entryID);
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...
54
        $entryID = (int)$myrow['entryID'];
55
        $term    = ucfirst($myts->displayTarea($myrow['term']));
56
57 View Code Duplication
        if (!XOOPS_USE_MULTIBYTES) {
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...
58
            $deftemp    = xoops_substr($myrow['definition'], 0, $lxConfig['rndlength'] - 1);
59
            $definition = $myts->displayTarea($deftemp, 1, 1, 1, 1, 1);
60
        }
61
62
        $categoryID = $myrow['categoryID'];
63
        $result_cat = $xoopsDB->query('SELECT categoryID, name FROM ' . $xoopsDB->prefix('lxcategories') . " WHERE categoryID = $categoryID");
64
        list($categoryID, $name) = $xoopsDB->fetchRow($result_cat);
65
        $categoryname = $myts->displayTarea($name);
66
67
        //TODO switch to central icons repository
68
69
        if ($xoopsUser) {
70
            if ($xoopsUser->isAdmin()) {
71
                $adminlinks = "<a href=\""
72
                              . XOOPS_URL
73
                              . '/modules/'
74
                              . $lexikon->dirname()
75
                              . '/admin/entry.php?op=mod&entryID='
76
                              . $entryID
77
                              . "\" target=\"_blank\"><img src=\""
78
                              . XOOPS_URL
79
                              . '/modules/'
80
                              . $lexikon->dirname()
81
                              . "/assets/images/edit.gif\"  border=\"0\" alt=\""
82
                              . _MB_LEXIKON_EDITTERM
83
                              . "\" width=\"16\" height=\"16\"></a>&nbsp;<a href=\""
84
                              . XOOPS_URL
85
                              . '/modules/'
86
                              . $lexikon->dirname()
87
                              . '/admin/entry.php?op=del&entryID='
88
                              . $entryID
89
                              . "\" target=\"_self\"><img src=\""
90
                              . XOOPS_URL
91
                              . '/modules/'
92
                              . $lexikon->dirname()
93
                              . "/assets/images/delete.gif\" border=\"0\" alt=\""
94
                              . _MB_LEXIKON_DELTERM
95
                              . "\" width=\"16\" height=\"16\"></a>&nbsp;";
96
            }
97
        }
98
        $userlinks = "<a href=\""
99
                     . XOOPS_URL
100
                     . '/modules/'
101
                     . $lexikon->dirname()
102
                     . '/print.php?entryID='
103
                     . $entryID
104
                     . "\" target=\"_blank\"><img src=\""
105
                     . XOOPS_URL
106
                     . '/modules/'
107
                     . $lexikon->dirname()
108
                     . "/assets/images/print.gif\" border=\"0\" alt=\""
109
                     . _MB_LEXIKON_PRINTTERM
110
                     . "\" width=\"16\" height=\"16\"></a>&nbsp;<a href=\"mailto:?subject="
111
                     . sprintf(_MB_LEXIKON_INTENTRY, $xoopsConfig['sitename'])
112
                     . '&amp;body='
113
                     . sprintf(_MB_LEXIKON_INTENTRYFOUND, $xoopsConfig['sitename'])
114
                     . ':  '
115
                     . XOOPS_URL
116
                     . '/modules/'
117
                     . $lexikon->dirname()
118
                     . '/entry.php?entryID='
119
                     . $entryID
120
                     . " \" target=\"_blank\"><img src=\""
121
                     . XOOPS_URL
122
                     . '/modules/'
123
                     . $lexikon->dirname()
124
                     . "/assets/images/friend.gif\" border=\"0\" alt=\""
125
                     . _MB_LEXIKON_SENDTOFRIEND
126
                     . "\" width=\"16\" height=\"16\"></a>&nbsp;";
127
128
        if ($lxConfig['multicats'] == 1) {
129
            $block['content'] = "<div style=\"font-size: 12px; font-weight: bold; background-color: #ccc; padding: 4px; margin: 0;\"><a href=\""
130
                                . XOOPS_URL
131
                                . '/modules/'
132
                                . $lexikon->dirname()
133
                                . "/category.php?categoryID=$categoryID\">$categoryname</a></div>";
134
            $block['content'] .= "<div style=\"padding: 4px 0 0 0; color: #456;\"><h5 style=\"margin: 0;\">$adminlinks $userlinks <a href=\""
135
                                 . XOOPS_URL
136
                                 . '/modules/'
137
                                 . $lexikon->dirname()
138
                                 . "/entry.php?entryID=$entryID\">$term</a></h5>$definition</div>";
0 ignored issues
show
Bug introduced by
The variable $definition 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...
139
        } else {
140
            $block['content'] = "<div style=\"padding: 4px; color: #456;\"><h5 style=\"margin: 0;\">$adminlinks $userlinks <a style=\"margin: 0;\" href=\""
141
                                . XOOPS_URL
142
                                . '/modules/'
143
                                . $lexikon->dirname()
144
                                . "/entry.php?entryID=$entryID\">$term</a></h5>$definition</div>";
145
        }
146
    }
147
148
    $block['content'] .= "<div style=\"text-align: right; font-size: x-small;\"><a href=\"" . XOOPS_URL . '/modules/' . $lexikon->dirname() . "/index.php\">" . _MB_LEXIKON_SEEMORE . '</a></div>';
149
150
    return $block;
151
}
152