Completed
Push — master ( ded118...cf7347 )
by Michael
02:48
created

search.inc.php ➔ lx_search()   F

Complexity

Conditions 24
Paths 840

Size

Total Lines 129
Code Lines 82

Duplication

Lines 8
Ratio 6.2 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 24
eloc 82
c 1
b 0
f 0
nc 840
nop 5
dl 8
loc 129
rs 2.248

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

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
 * $Id: search.inc.php v 1.0 8 May 2004 hsalazar Exp $
4
 * Module: Lexikon
5
 * Version: v 1.00
6
 * Release Date: 8 May 2004
7
 * Author: hsalazar
8
 * Licence: GNU
9
 */
10
if( ! defined( 'XOOPS_ROOT_PATH' ) ) die( 'XOOPS root path not defined' ) ;
11
12
function lx_search( $queryarray, $andor, $limit, $offset, $userid ) {
13
14
  global $xoopsDB, $xoopsUser;
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...
15
  // -- search comments + highlighter
16
  $highlight = false;
0 ignored issues
show
Unused Code introduced by
$highlight 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...
17
  $searchincomments = false ;
0 ignored issues
show
Unused Code introduced by
$searchincomments 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...
18
  include_once XOOPS_ROOT_PATH.'/modules/lexikon/include/common.inc.php';
19
  include_once XOOPS_ROOT_PATH."/modules/lexikon/include/functions.php";
20
  $highlight = lx_getmoduleoption('config_highlighter');
21
    //if ( is_object($xoopsUser) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
22
        $searchincomments = CONFIG_SEARCH_COMMENTS;
23
    //	} 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...
24
    //		$searchincomments = false ;
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
25
    //	}
26
    $module_handler =& xoops_gethandler('module');
27
    $module =& $module_handler->getByDirname('lexikon');
28
  $module_id = $module->getVar('mid');
29
    // Permissions
30
    $gperm_handler =& xoops_gethandler('groupperm');
31
    $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
32
    $allowed_cats = $gperm_handler->getItemIds("lexikon_view", $groups, $module_id);
33
    $catids= implode(',', $allowed_cats);
34
            
35
    $sql = "SELECT entryID, categoryID, term, definition, ref, uid, datesub FROM " . $xoopsDB -> prefix( "lxentries" ) . " WHERE submit = 0 AND offline = 0 ";
36
    $sql .= " AND categoryID IN ($catids) ";
37
38
    if ( $userid != 0 ) {
39
        $sql .= " AND uid=".$userid." ";
40
    }
41
    if ($highlight) {
42
        if ($queryarray == ''){
43
            $keywords= '';
0 ignored issues
show
Unused Code introduced by
$keywords 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...
44
            $hightlight_key = '';
45
        } else {
46
            $keywords=implode('+', $queryarray);
47
            //$keywords='&keywords='.urlencode(trim(implode(' ',$queryarray)));
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...
48
            $hightlight_key = "&amp;keywords=" . $keywords;
49
        }
50
    }
51
    // because count() returns 1 even if a supplied variable
52
    // is not an array, we must check if $querryarray is really an array
53
    $count = count( $queryarray );
54
    if ( $count > 0 && is_array( $queryarray ) ) {
55
        $sql .= "AND ((term LIKE '%$queryarray[0]%' OR definition LIKE '%$queryarray[0]%' OR ref LIKE '%$queryarray[0]%')";
56 View Code Duplication
        for ( $i = 1; $i < $count; $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...
57
            $sql .= " $andor ";
58
            $sql .= "(term LIKE '%$queryarray[$i]%' OR definition LIKE '%$queryarray[$i]%' OR ref LIKE '%$queryarray[$i]%')";
59
        }
60
        $sql .= ") ";
61
62
    }
63
    $sql .= "ORDER BY entryID DESC";
64
    $result = $xoopsDB -> query( $sql, $limit, $offset );
65
    $ret = array();
66
    $i = 0;
67
68
    while ( $myrow = $xoopsDB -> fetchArray( $result ) ) {
69
        $display = true;
70
        if($module_id && $gperm_handler) {
71
            if (!$gperm_handler->checkRight("lexikon_view", $myrow['categoryID'], $groups, $module_id)) {
72
            //if (!$gperm_handler->checkRight("lexikon_view", $categoryID, $groups, $module_id)) {
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...
73
                $display = false;
74
                }
75
        }
76
        if ($display) {
77
            $ret[$i]['image'] = "images/lx.png";
78
            $ret[$i]['link'] = "entry.php?entryID=" . $myrow['entryID'] . $hightlight_key;
0 ignored issues
show
Bug introduced by
The variable $hightlight_key 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...
79
            $ret[$i]['title'] = $myrow['term'];
80
            $ret[$i]['time'] = $myrow['datesub'];
81
            $ret[$i]['uid'] = $myrow['uid'];
82
            $i++;
83
        }
84
    }
85
    //return $ret;
86
    //}
87
    // --- comments search ---
88
    if ($searchincomments && (isset($limit) && $i<=$limit)) {
89
        include_once XOOPS_ROOT_PATH.'/include/comment_constants.php';
90
        $ind = $i;
91
        $sql = "SELECT com_id, com_modid, com_itemid, com_created, com_uid, com_title, com_text, com_status
92
               FROM ".$xoopsDB->prefix("xoopscomments")."
93
               WHERE (com_id>0) AND (com_modid=$module_id) AND (com_status=".XOOPS_COMMENT_ACTIVE.") ";
94
        if ( $userid != 0 ) {
95
            $sql .= " AND com_uid=".$userid." ";
96
        }
97
98
        if ( is_array($queryarray) && $count = count($queryarray) ) {
99
            $sql .= " AND ((com_title LIKE '%$queryarray[0]%' OR com_text LIKE '%$queryarray[0]%')";
100 View Code Duplication
            for ($i=1;$i<$count;$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...
101
                $sql .= " $andor ";
102
                $sql .= "(com_title LIKE '%$queryarray[$i]%' OR com_text LIKE '%$queryarray[$i]%')";
103
            }
104
            $sql .= ") ";
105
        }
106
        $i=$ind;
107
        $sql .= "ORDER BY com_created DESC";
108
        $result = $xoopsDB->query( $sql, $limit, $offset );
109
        while ($myrow = $xoopsDB->fetchArray($result)) {
110
            $display=true;
111
            //permission
112
            /*if($module_id && $gperm_handler) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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...
113
                if (!$gperm_handler->checkRight("lexikon_view", $myrow['com_itemid'], $groups, $module_id)) {
114
                //if (!$gperm_handler->checkRight("lexikon_view", $myrow['categoryID'], $groups, $xoopsModule -> getVar('mid'))) {
115
                    $display = false;
116
                }
117
            }*/
118
            list( $entryID, $offline ) = $xoopsDB->fetchRow($xoopsDB->query("
0 ignored issues
show
Unused Code introduced by
The assignment to $entryID is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
119
                                         SELECT entryID, offline
120
                                         FROM ".$xoopsDB->prefix("lxentries")." WHERE entryID = ".$myrow['com_itemid'].""));
121
            if ($offline == 1) {
122
                $display = false;
123
            }
124
            if ($i+1>$limit) {
125
                $display = false;
126
            }
127
128
            if ($display) {
129
                $ret[$i]['image'] = "images/lx.png";
130
                $ret[$i]['link'] = "entry.php?entryID=".$myrow['com_itemid'] . $hightlight_key;
131
                $ret[$i]['title'] = $myrow['com_title'];
132
                $ret[$i]['time'] = $myrow['com_created'];
133
                $ret[$i]['uid'] = $myrow['com_uid'];
134
                $i++;
135
            }
136
        }
137
    }
138
139
    return $ret;
140
}
141