Conditions | 21 |
Paths | 147 |
Total Lines | 81 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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); |
||
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++) { |
||
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++) { |
||
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(); |
||
113 | for ($i = 0; $i < count($word); $i++) { |
||
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"); |
||
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"); |
||
142 | } |
||
143 | } |
||
144 | return; |
||
145 | } |
||
191 |
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: