Conditions | 75 |
Paths | 1152 |
Total Lines | 351 |
Code Lines | 219 |
Lines | 51 |
Ratio | 14.53 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
34 | function importModule($key, $cur, $index_file, $unit_count, $module_srl, $guestbook_module_srl, $user_id, $module_name=null) |
||
35 | { |
||
36 | // Pre-create the objects needed |
||
37 | $this->oXmlParser = new XmlParser(); |
||
38 | // Get category information of the target module |
||
39 | $oDocumentController = getController('document'); |
||
40 | $oDocumentModel = getModel('document'); |
||
41 | $category_list = $category_titles = array(); |
||
42 | $category_list = $oDocumentModel->getCategoryList($module_srl); |
||
43 | if(count($category_list)) foreach($category_list as $key => $val) $category_titles[$val->title] = $val->category_srl; |
||
44 | // First handle categorty information |
||
45 | $category_file = preg_replace('/index$/i', 'category.xml', $index_file); |
||
46 | if(file_exists($category_file)) |
||
47 | { |
||
48 | // Create the xmlParser object |
||
49 | $xmlDoc = $this->oXmlParser->loadXmlFile($category_file); |
||
50 | // List category information |
||
51 | if($xmlDoc->categories->category) |
||
52 | { |
||
53 | $categories = array(); |
||
54 | $idx = 0; |
||
55 | $this->arrangeCategory($xmlDoc->categories, $categories, $idx, 0); |
||
56 | |||
57 | $match_sequence = array(); |
||
58 | foreach($categories as $k => $v) |
||
59 | { |
||
60 | $category = $v->name; |
||
61 | if(!$category || $category_titles[$category]) continue; |
||
62 | |||
63 | $obj = null; |
||
64 | $obj->title = $category; |
||
65 | $obj->module_srl = $module_srl; |
||
66 | if($v->parent) $obj->parent_srl = $match_sequence[$v->parent]; |
||
67 | $output = $oDocumentController->insertCategory($obj); |
||
68 | |||
69 | if($output->toBool()) $match_sequence[$v->sequence] = $category_titles[$category] = $output->get('category_srl'); |
||
70 | } |
||
71 | $oDocumentController->makeCategoryFile($module_srl); |
||
72 | } |
||
73 | FileHandler::removeFile($category_file); |
||
74 | } |
||
75 | $category_list = $category_titles = array(); |
||
76 | $category_list = $oDocumentModel->getCategoryList($module_srl); |
||
77 | if(count($category_list)) foreach($category_list as $key => $val) $category_titles[$val->title] = $val->category_srl; |
||
78 | // Get administrator information |
||
79 | $oMemberModel = getModel('member'); |
||
80 | $member_info = $oMemberModel->getMemberInfoByUserID($user_id); |
||
81 | $author_xml_id = 0; |
||
82 | |||
83 | if(!$cur) $cur = 0; |
||
84 | // Open an index file |
||
85 | $f = fopen($index_file,"r"); |
||
86 | // Pass if already read |
||
87 | for($i=0;$i<$cur;$i++) fgets($f, 1024); |
||
88 | // Read each line until the codition meets |
||
89 | for($idx=$cur;$idx<$cur+$unit_count;$idx++) |
||
90 | { |
||
91 | if(feof($f)) break; |
||
92 | // Find a location |
||
93 | $target_file = trim(fgets($f, 1024)); |
||
94 | |||
95 | if(!file_exists($target_file)) continue; |
||
96 | // Start importing data |
||
97 | $fp = fopen($target_file,"r"); |
||
98 | if(!$fp) continue; |
||
99 | |||
100 | $obj = null; |
||
101 | $obj->module_srl = $module_srl; |
||
102 | $obj->document_srl = getNextSequence(); |
||
103 | $obj->uploaded_count = 0; |
||
104 | |||
105 | $files = array(); |
||
106 | |||
107 | $started = false; |
||
108 | $buff = null; |
||
109 | // Start importing from the body data |
||
110 | while(!feof($fp)) |
||
111 | { |
||
112 | $str = fgets($fp, 1024); |
||
113 | // Prepare an item |
||
114 | if(substr($str,0,5) == '<post') |
||
115 | { |
||
116 | $started = true; |
||
117 | continue; |
||
118 | // Import the attachment |
||
119 | } |
||
120 | View Code Duplication | else if(substr($str,0,12) == '<attachment ') |
|
121 | { |
||
122 | if($this->importAttaches($fp, $module_srl, $obj->document_srl, $files, $str)) $obj->uploaded_count++; |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | if($started) $buff .= $str; |
||
127 | } |
||
128 | |||
129 | $xmlDoc = $this->oXmlParser->parse('<post>'.$buff); |
||
130 | |||
131 | $author_xml_id = $xmlDoc->post->author->body; |
||
132 | |||
133 | if($xmlDoc->post->category->body) |
||
134 | { |
||
135 | $tmp_arr = explode('/',$xmlDoc->post->category->body); |
||
136 | $category = trim($tmp_arr[count($tmp_arr)-1]); |
||
137 | if($category_titles[$category]) $obj->category_srl = $category_titles[$category]; |
||
138 | } |
||
139 | |||
140 | $obj->is_notice = 'N'; |
||
141 | $obj->status = in_array($xmlDoc->post->visibility->body, array('public','syndicated'))?$oDocumentModel->getConfigStatus('public'):$oDocumentModel->getConfigStatus('secret'); |
||
142 | $obj->title = $xmlDoc->post->title->body; |
||
143 | $obj->content = $xmlDoc->post->content->body; |
||
144 | $obj->password = md5($xmlDoc->post->password->body); |
||
145 | $obj->commentStatus = $xmlDoc->post->acceptcomment->body=='1'?'ALLOW':'DENY'; |
||
146 | $obj->allow_trackback = $xmlDoc->post->accepttrackback->body=='1'?'Y':'N'; |
||
147 | //$obj->allow_comment = $xmlDoc->post->acceptComment->body=='1'?'Y':'N'; |
||
148 | //$obj->allow_trackback = $xmlDoc->post->acceptTrackback->body=='1'?'Y':'N'; |
||
149 | $obj->regdate = date("YmdHis",$xmlDoc->post->published->body); |
||
150 | $obj->last_update = date("YmdHis", $xmlDoc->post->modified->body); |
||
151 | if(!$obj->last_update) $obj->last_update = $obj->regdate; |
||
152 | |||
153 | $tag = null; |
||
154 | $tmp_tags = null; |
||
155 | $tag = $xmlDoc->post->tag; |
||
156 | if($tag) |
||
157 | { |
||
158 | if(!is_array($tag)) $tag = array($tag); |
||
159 | foreach($tag as $key => $val) $tmp_tags[] = $val->body; |
||
160 | $obj->tags = implode(',',$tmp_tags); |
||
161 | } |
||
162 | |||
163 | $obj->readed_count = 0; |
||
164 | $obj->voted_count = 0; |
||
165 | $obj->nick_name = $member_info->nick_name; |
||
166 | $obj->user_name = $member_info->user_name; |
||
167 | $obj->user_id = $member_info->user_id; |
||
168 | $obj->member_srl = $member_info->member_srl; |
||
169 | $obj->email_address = $member_info->email_address; |
||
170 | $obj->homepage = $member_info->homepage; |
||
171 | $obj->ipaddress = $_REMOTE['SERVER_ADDR']; |
||
172 | $obj->list_order = $obj->update_order = $obj->document_srl*-1; |
||
173 | $obj->notify_message = 'N'; |
||
174 | // Change content information (attachment) |
||
175 | $obj->content = str_replace('[##_ATTACH_PATH_##]/','',$obj->content); |
||
176 | View Code Duplication | if(count($files)) |
|
177 | { |
||
178 | foreach($files as $key => $val) { |
||
179 | $obj->content = preg_replace('/(src|href)\=(["\']?)'.preg_quote($key).'(["\']?)/i','$1="'.$val->url.'"',$obj->content); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | $obj->content = preg_replace_callback('!\[##_Movie\|([^\|]*)\|(.*?)_##\]!is', array($this, '_replaceTTMovie'), $obj->content); |
||
184 | |||
185 | if(count($files)) |
||
186 | { |
||
187 | $this->files = $files; |
||
188 | $obj->content = preg_replace_callback('!\[##_([a-z0-9]+)\|([^\|]*)\|([^\|]*)\|(.*?)_##\]!is', array($this, '_replaceTTAttach'), $obj->content); |
||
189 | } |
||
190 | // Trackback inserted |
||
191 | $obj->trackback_count = 0; |
||
192 | if($xmlDoc->post->trackback) |
||
193 | { |
||
194 | $trackbacks = $xmlDoc->post->trackback; |
||
195 | if(!is_array($trackbacks)) $trackbacks = array($trackbacks); |
||
196 | if(count($trackbacks)) |
||
197 | { |
||
198 | foreach($trackbacks as $key => $val) |
||
199 | { |
||
200 | $tobj = null; |
||
201 | $tobj->trackback_srl = getNextSequence(); |
||
202 | $tobj->module_srl = $module_srl; |
||
203 | $tobj->document_srl = $obj->document_srl; |
||
204 | $tobj->url = $val->url->body; |
||
205 | $tobj->title = $val->title->body; |
||
206 | $tobj->blog_name = $val->site->body; |
||
207 | $tobj->excerpt = $val->excerpt->body; |
||
208 | $tobj->regdate = date("YmdHis",$val->received->body); |
||
209 | $tobj->ipaddress = $val->ip->body; |
||
210 | $tobj->list_order = -1*$tobj->trackback_srl; |
||
211 | $output = executeQuery('trackback.insertTrackback', $tobj); |
||
212 | if($output->toBool()) $obj->trackback_count++; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | // Comment |
||
217 | $obj->comment_count = 0; |
||
218 | if($xmlDoc->post->comment) |
||
219 | { |
||
220 | $comment = $xmlDoc->post->comment; |
||
221 | if(!is_array($comment)) $comment = array($comment); |
||
222 | foreach($comment as $key => $val) |
||
223 | { |
||
224 | $parent_srl = $this->insertComment($val, $module_srl, $obj->document_srl, $member_info, 0, $author_xml_id); |
||
225 | if($parent_srl === false) continue; |
||
226 | |||
227 | $obj->comment_count++; |
||
228 | View Code Duplication | if($val->comment) |
|
229 | { |
||
230 | $child_comment = $val->comment; |
||
231 | if(!is_array($child_comment)) $child_comment = array($child_comment); |
||
232 | foreach($child_comment as $k => $v) |
||
233 | { |
||
234 | $result = $this->insertComment($v, $module_srl, $obj->document_srl, $member_info, $parent_srl, $author_xml_id); |
||
235 | if($result !== false) $obj->comment_count++; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | if($module_name == 'textyle') |
||
242 | { |
||
243 | $args->document_srl = $obj->document_srl; |
||
244 | $args->module_srl = $obj->module_srl; |
||
245 | $args->logs = serialize(null); |
||
246 | $output = executeQuery('textyle.insertPublishLog', $args); |
||
247 | // Visibility value of published state |
||
248 | $status_published = array('public', 'syndicated'); |
||
249 | // Save state if not published |
||
250 | if(!in_array($xmlDoc->post->visibility->body, $status_published)) |
||
251 | { |
||
252 | $obj->module_srl = $member_info->member_srl; |
||
253 | } |
||
254 | } |
||
255 | // Document |
||
256 | $output = executeQuery('document.insertDocument', $obj); |
||
257 | |||
258 | View Code Duplication | if($output->toBool()) |
|
259 | { |
||
260 | // Tags |
||
261 | if($obj->tags) |
||
262 | { |
||
263 | $tag_list = explode(',',$obj->tags); |
||
264 | $tag_count = count($tag_list); |
||
265 | for($i=0;$i<$tag_count;$i++) |
||
266 | { |
||
267 | $args = new stdClass; |
||
268 | $args->tag_srl = getNextSequence(); |
||
269 | $args->module_srl = $module_srl; |
||
270 | $args->document_srl = $obj->document_srl; |
||
271 | $args->tag = trim($tag_list[$i]); |
||
272 | $args->regdate = $obj->regdate; |
||
273 | if(!$args->tag) continue; |
||
274 | $output = executeQuery('tag.insertTag', $args); |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 | |||
279 | fclose($fp); |
||
280 | FileHandler::removeFile($target_file); |
||
281 | } |
||
282 | |||
283 | fclose($f); |
||
284 | |||
285 | if(count($category_list)) foreach($category_list as $key => $val) $oDocumentController->updateCategoryCount($module_srl, $val->category_srl); |
||
286 | // Guestbook information |
||
287 | $guestbook_file = preg_replace('/index$/i', 'guestbook.xml', $index_file); |
||
288 | if(file_exists($guestbook_file)) |
||
289 | { |
||
290 | // Create the xmlParser object |
||
291 | $xmlDoc = $this->oXmlParser->loadXmlFile($guestbook_file); |
||
292 | // Handle guest book information |
||
293 | if($guestbook_module_srl && $xmlDoc->guestbook->comment) |
||
294 | { |
||
295 | $comment = $xmlDoc->guestbook->comment; |
||
296 | if(!is_array($comment)) $comment = array($comment); |
||
297 | |||
298 | if($module_name =='textyle') |
||
299 | { |
||
300 | foreach($comment as $key => $val) |
||
301 | { |
||
302 | $textyle_guestbook_srl = getNextSequence(); |
||
303 | |||
304 | if($val->comment) |
||
305 | { |
||
306 | $child_comment = $val->comment; |
||
307 | if(!is_array($child_comment)) $child_comment = array($child_comment); |
||
308 | foreach($child_comment as $k => $v) |
||
309 | { |
||
310 | $result = $this->insertTextyleGuestbookItem($v, $module_srl, $member_info,0,$textyle_guestbook_srl,$author_xml_id); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | $result = $this->insertTextyleGuestbookItem($val, $module_srl, $member_info,$textyle_guestbook_srl,0,$author_xml_id); |
||
315 | } |
||
316 | } |
||
317 | else |
||
318 | { |
||
319 | foreach($comment as $key => $val) |
||
320 | { |
||
321 | $obj = null; |
||
322 | $obj->module_srl = $guestbook_module_srl; |
||
323 | $obj->document_srl = getNextSequence(); |
||
324 | $obj->uploaded_count = 0; |
||
325 | $obj->is_notice = 'N'; |
||
326 | $obj->status = $val->secret->body=='1'?$oDocumentModel->getConfigStatus('secret'):$oDocumentModel->getConfigStatus('public'); |
||
327 | $obj->content = nl2br($val->content->body); |
||
328 | |||
329 | // Extract a title form the bocy |
||
330 | $obj->title = cut_str(strip_tags($obj->content),20,'...'); |
||
331 | if ($obj->title == '') $obj->title = 'Untitled'; |
||
332 | |||
333 | $obj->commentStatus = 'ALLOW'; |
||
334 | $obj->allow_trackback = 'N'; |
||
335 | $obj->regdate = date("YmdHis",$val->written->body); |
||
336 | $obj->last_update = date("YmdHis", $val->written->body); |
||
337 | if(!$obj->last_update) $obj->last_update = $obj->regdate; |
||
338 | $obj->tags = ''; |
||
339 | $obj->readed_count = 0; |
||
340 | $obj->voted_count = 0; |
||
341 | if($author_xml_id && $val->commenter->attrs->id == $author_xml_id) |
||
342 | { |
||
343 | $obj->password = ''; |
||
344 | $obj->nick_name = $member_info->nick_name; |
||
345 | $obj->user_name = $member_info->user_name; |
||
346 | $obj->user_id = $member_info->user_id; |
||
347 | $obj->member_srl = $member_info->member_srl; |
||
348 | $obj->email_address = $member_info->email_address; |
||
349 | $obj->homepage = $member_info->homepage; |
||
350 | } |
||
351 | else |
||
352 | { |
||
353 | $obj->password = $val->password->body; |
||
354 | $obj->nick_name = $val->commenter->name->body; |
||
355 | $obj->member_srl = 0; |
||
356 | $homepage = $val->commenter->homepage->body; |
||
357 | } |
||
358 | $obj->ipaddress = $val->commenter->ip->body; |
||
359 | $obj->list_order = $obj->update_order = $obj->document_srl*-1; |
||
360 | $obj->notify_message = 'N'; |
||
361 | $obj->trackback_count = 0; |
||
362 | |||
363 | $obj->comment_count = 0; |
||
364 | View Code Duplication | if($val->comment) |
|
365 | { |
||
366 | $child_comment = $val->comment; |
||
367 | if(!is_array($child_comment)) $child_comment = array($child_comment); |
||
368 | foreach($child_comment as $k => $v) |
||
369 | { |
||
370 | $result = $this->insertComment($v, $module_srl, $obj->document_srl, $member_info, 0,$author_xml_id); |
||
371 | if($result !== false) $obj->comment_count++; |
||
372 | } |
||
373 | } |
||
374 | |||
375 | // Document |
||
376 | $output = executeQuery('document.insertDocument', $obj); |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | FileHandler::removeFile($guestbook_file); |
||
381 | } |
||
382 | |||
383 | return $idx-1; |
||
384 | } |
||
385 | |||
735 |
If you suppress an error, we recommend checking for the error condition explicitly: