add_search_words()   F
last analyzed

Complexity

Conditions 21
Paths 147

Size

Total Lines 81
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 51
nc 147
nop 4
dl 0
loc 81
rs 3.775
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
2
/***************************************************************************
3
*                              functions_search.php
4
*                              -------------------
5
*     begin                : Wed Sep 05 2001
6
*     copyright            : (C) 2002 The phpBB Group
7
*     email                : [email protected]
8
*
9
*     $Id: functions_search.php,v 1.8.2.16 2003/06/30 17:18:37 acydburn Exp $
10
*
11
****************************************************************************/
12
13
/***************************************************************************
14
 *
15
 *   This program is free software; you can redistribute it and/or modify
16
 *   it under the terms of the GNU General Public License as published by
17
 *   the Free Software Foundation; either version 2 of the License, or
18
 *   (at your option) any later version.
19
 *
20
 ***************************************************************************/
21
22
function clean_words($mode, &$entry) {
23
    static $drop_char_match =   array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?',
24
        '%', '-', '~', '+', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!');
25
    static $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ',
26
        ' ', '',  ' ',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' , ' ', ' ', ' ', ' ',  ' ', ' ');
27
28
    $entry = ' ' . strip_tags(strtolower($entry)) . ' ';
29
30
    if ($mode == 'post') {
31
        // Replace line endings by a space
32
        $entry = preg_replace('/[\n\r]/is', ' ', $entry);
33
        // HTML entities like &nbsp;
34
        $entry = preg_replace('/\b&[a-z]+;\b/', ' ', $entry);
35
        // Remove URL's
36
        $entry = preg_replace('/\b[a-z0-9]+:\/\/[a-z0-9\.\-]+(\/[a-z0-9\?\.%_\-\+=&\/]+)?/', ' ', $entry);
37
        // Quickly remove BBcode.
38
        $entry = preg_replace('/\[img:[a-z0-9]{10,}\].*?\[\/img:[a-z0-9]{10,}\]/', ' ', $entry);
39
        $entry = preg_replace('/\[\/?url(=.*?)?\]/', ' ', $entry);
40
        $entry = preg_replace('/\[\/?[a-z\*=\+\-]+(\:?[0-9a-z]+)?:[a-z0-9]{10,}(\:[a-z0-9]+)?=?.*?\]/', ' ', $entry);
41
    }
42
43
    //
44
    // Filter out strange characters like ^, $, &, change "it's" to "its"
45
    //
46
    for ($i = 0; $i < count($drop_char_match); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
47
        $entry =  str_replace($drop_char_match[$i], $drop_char_replace[$i], $entry);
48
    }
49
50
    if ($mode == 'post') {
51
        $entry = str_replace('*', ' ', $entry);
52
53
        // 'words' that consist of <3 or >20 characters are removed.
54
        $entry = preg_replace('/[ ]([\S]{1,2}|[\S]{21,})[ ]/', ' ', $entry);
55
    }
56
57
    return $entry;
58
}
59
60
function split_words(&$entry, $mode = 'post') {
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
function split_words(&$entry, /** @scrutinizer ignore-unused */ $mode = 'post') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    return explode(' ', trim(preg_replace('#\s+#', ' ', $entry)));
62
}
63
64
function add_search_words($mode, $post_id, $post_text, $post_title = '') {
65
    include("../../config/connect.php");
66
67
    $search_raw_words = array();
68
    $post_text = clean_words('post', $post_text);
69
    $post_title = clean_words('post', $post_title);
70
    $search_raw_words['text'] = split_words($post_text);
71
    $search_raw_words['title'] = split_words($post_title);
72
73
    @set_time_limit(0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

73
    /** @scrutinizer ignore-unhandled */ @set_time_limit(0);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
74
75
    $word = array();
76
    $word_insert_sql = array();
77
    while (list($word_in, $search_matches) = @each($search_raw_words)) {
78
        $word_insert_sql[$word_in] = '';
79
        if (!empty($search_matches)) {
80
            for ($i = 0; $i < count($search_matches); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
81
                $search_matches[$i] = trim($search_matches[$i]);
82
83
                if ($search_matches[$i] != '') {
84
                    $word[] = $search_matches[$i];
85
                    if (!strstr($word_insert_sql[$word_in], "'" . $search_matches[$i] . "'")) {
86
                        $word_insert_sql[$word_in] .= ($word_insert_sql[$word_in] != "") ? ", '" .
87
                            $search_matches[$i] . "'" : "'" . $search_matches[$i] . "'";
88
                    }
89
                }
90
            }
91
        }
92
    }
93
94
    if (count($word)) {
95
        sort($word);
96
97
        $prev_word = '';
98
        $word_text_sql = '';
99
        $temp_word = array();
100
        for ($i = 0; $i < count($word); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
101
            if ($word[$i] != $prev_word) {
102
                $temp_word[] = $word[$i];
103
                $word_text_sql .= (($word_text_sql != '') ? ', ' : '') . "'" . $word[$i] . "'";
104
            }
105
            $prev_word = $word[$i];
106
        }
107
        $word = $temp_word;
108
109
        $check_words = array();
110
111
        $value_sql = '';
112
        $match_word = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $match_word is dead and can be removed.
Loading history...
113
        for ($i = 0; $i < count($word); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
114
            $new_match = true;
115
            if (isset($check_words[$word[$i]])) {
116
                $new_match = false;
117
            }
118
119
            if ($new_match) {
120
                $value_sql .= (($value_sql != '') ? ', ' : '') . '(\'' . $word[$i] . '\')';
121
            }
122
        }
123
124
        if ($value_sql != '') {
125
            $sql = "INSERT IGNORE INTO news_search_wordlist (news_word_text)
126
						VALUES $value_sql";
127
128
            $query = $mysqli->query($sql) or die("Inserting in news_search_wordlist failed");
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
Comprehensibility Best Practice introduced by
The variable $mysqli seems to be never defined.
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
129
        }
130
    }
131
132
    while (list($word_in, $match_sql) = @each($word_insert_sql)) {
133
        $title_match = ($word_in == 'title') ? 1 : 0;
134
135
        if ($match_sql != '') {
136
            $sql = "INSERT IGNORE INTO news_search_wordmatch (news_id, news_word_id, news_title_match)
137
				SELECT $post_id, news_word_id, $title_match
138
					FROM news_search_wordlist
139
					WHERE news_word_text IN ($match_sql)";
140
141
            $query = $mysqli->query($sql) or die("Inserting in news_search_wordmatch failed");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
142
        }
143
    }
144
    return;
145
}
146
147
function remove_search_post($post_id_sql) {
148
    $words_removed = false;
149
150
    $sql = "SELECT news_word_id
151
				FROM news_search_wordmatch
152
				WHERE news_id IN ($post_id_sql)
153
				GROUP BY news_word_id";
154
    if ($result = mysql_query($sql)) {
155
        $word_id_sql = '';
156
        while ($row = mysql_fetch_array($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of mysql_fetch_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
        while ($row = mysql_fetch_array(/** @scrutinizer ignore-type */ $result)) {
Loading history...
157
            $word_id_sql .= ($word_id_sql != '') ? ', ' . $row['news_word_id'] : $row['news_word_id'];
158
        }
159
160
        $sql = "SELECT news_word_id
161
					FROM news_search_wordmatch
162
					WHERE news_word_id IN ($word_id_sql)
163
					GROUP BY news_word_id
164
					HAVING COUNT(news_word_id) = 1";
165
        if ($result = mysql_query($sql)) {
166
            $word_id_sql = '';
167
            while ($row = mysql_fetch_array($result)) {
168
                $word_id_sql .= ($word_id_sql != '') ? ', ' . $row['news_word_id'] : $row['news_word_id'];
169
            }
170
171
            if ($word_id_sql != '') {
172
                $sql = "DELETE FROM news_search_wordlist
173
							WHERE news_word_id IN ($word_id_sql)";
174
                if (!mysql_query($sql)) {
175
                    die("Could not delete word list entry");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
176
                }
177
178
                $words_removed = mysql_affected_rows();
179
            }
180
        }
181
    }
182
183
    $sql = "DELETE FROM news_search_wordmatch
184
		WHERE news_id IN ($post_id_sql)";
185
    if (!mysql_query($sql)) {
186
        die("Error in deleting post");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
187
    }
188
189
    return $words_removed;
190
}
191