GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( baac3d...439f66 )
by gyeong-won
17:54
created

commentModel::getTotalCommentList()   F

Complexity

Conditions 33
Paths 4608

Size

Total Lines 142
Code Lines 82

Duplication

Lines 50
Ratio 35.21 %

Importance

Changes 0
Metric Value
cc 33
eloc 82
nc 4608
nop 2
dl 50
loc 142
rs 2
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
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
4
/**
5
 * commentModel class
6
 * model class of the comment module
7
 *
8
 * @author NAVER ([email protected])
9
 * @package /modules/comment
10
 * @version 0.1
11
 */
12
class commentModel extends comment
13
{
14
15
	/**
16
	 * Initialization
17
	 * @return void
18
	 */
19
	function init()
20
	{
21
22
	}
23
24
	/**
25
	 * display the pop-up menu of the post
26
	 * Print, scrap, vote-up(recommen), vote-down(non-recommend), report features added
27
	 * @return void
28
	 */
29
	function getCommentMenu()
30
	{
31
		// get the post's id number and the current login information
32
		$comment_srl = Context::get('target_srl');
33
		$mid = Context::get('cur_mid');
0 ignored issues
show
Unused Code introduced by
$mid 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...
34
		$logged_info = Context::get('logged_info');
35
		$act = Context::get('cur_act');
0 ignored issues
show
Unused Code introduced by
$act 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...
36
37
		// array values for menu_list, "comment post, target, url"
38
		$menu_list = array();
39
40
		// call a trigger
41
		ModuleHandler::triggerCall('comment.getCommentMenu', 'before', $menu_list);
0 ignored issues
show
Documentation introduced by
$menu_list is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
43
		$oCommentController = getController('comment');
44
45
		// feature that only member can do
46
		if($logged_info->member_srl)
47
		{
48
			$oCommentModel = getModel('comment');
49
			$columnList = array('comment_srl', 'module_srl', 'member_srl', 'ipaddress');
50
			$oComment = $oCommentModel->getComment($comment_srl, FALSE, $columnList);
51
			$module_srl = $oComment->get('module_srl');
52
			$member_srl = $oComment->get('member_srl');
53
54
			$oModuleModel = getModel('module');
55
			$comment_config = $oModuleModel->getModulePartConfig('document', $module_srl);
56
57
			if($comment_config->use_vote_up != 'N' && $member_srl != $logged_info->member_srl)
58
			{
59
				// Add a vote-up button for positive feedback
60
				$url = sprintf("doCallModuleAction('comment','procCommentVoteUp','%s')", $comment_srl);
61
				$oCommentController->addCommentPopupMenu($url, 'cmd_vote', '', 'javascript');
62
			}
63
64 View Code Duplication
			if($comment_config->use_vote_down != 'N' && $member_srl != $logged_info->member_srl)
65
			{
66
				// Add a vote-down button for negative feedback
67
				$url = sprintf("doCallModuleAction('comment','procCommentVoteDown','%s')", $comment_srl);
68
				$oCommentController->addCommentPopupMenu($url, 'cmd_vote_down', '', 'javascript');
69
			}
70
71
			// Add the report feature against abused posts
72
			$url = sprintf("doCallModuleAction('comment','procCommentDeclare','%s')", $comment_srl);
73
			$oCommentController->addCommentPopupMenu($url, 'cmd_declare', '', 'javascript');
74
		}
75
76
		// call a trigger (after)
77
		ModuleHandler::triggerCall('comment.getCommentMenu', 'after', $menu_list);
78
79 View Code Duplication
		if($this->grant->manager){
0 ignored issues
show
Bug introduced by
The property grant does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
			$str_confirm = Context::getLang('confirm_move');
81
			$url = sprintf("if(!confirm('%s')) return; var params = new Array(); params['comment_srl']='%s'; params['mid']=current_mid;params['cur_url']=current_url; exec_xml('comment', 'procCommentAdminMoveToTrash', params)", $str_confirm, $comment_srl);
82
			$oCommentController->addCommentPopupMenu($url,'cmd_trash','','javascript');
83
84
		}
85
86
		// find a comment by IP matching if an administrator.
87 View Code Duplication
		if($logged_info->is_admin == 'Y')
88
		{
89
			$oCommentModel = getModel('comment');
90
			$oComment = $oCommentModel->getComment($comment_srl);
91
92
			if($oComment->isExists())
93
			{
94
				// Find a post of the corresponding ip address
95
				$url = getUrl('', 'module', 'admin', 'act', 'dispCommentAdminList', 'search_target', 'ipaddress', 'search_keyword', $oComment->getIpAddress());
96
				$oCommentController->addCommentPopupMenu($url, 'cmd_search_by_ipaddress', $icon_path, 'TraceByIpaddress');
0 ignored issues
show
Bug introduced by
The variable $icon_path does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
97
98
				$url = sprintf("var params = new Array(); params['ipaddress_list']='%s'; exec_xml('spamfilter', 'procSpamfilterAdminInsertDeniedIP', params, completeCallModuleAction)", $oComment->getIpAddress());
99
				$oCommentController->addCommentPopupMenu($url, 'cmd_add_ip_to_spamfilter', '', 'javascript');
100
			}
101
		}
102
103
		// Changing a language of pop-up menu
104
		$menus = Context::get('comment_popup_menu_list');
105
		$menus_count = count($menus);
106
107 View Code Duplication
		for($i = 0; $i < $menus_count; $i++)
108
		{
109
			$menus[$i]->str = Context::getLang($menus[$i]->str);
110
		}
111
112
		// get a list of final organized pop-up menus
113
		$this->add('menus', $menus);
114
	}
115
116
	/**
117
	 * Check if you have a permission to comment_srl
118
	 * use only session information
119
	 * @param int $comment_srl
120
	 * @return bool
121
	 */
122
	function isGranted($comment_srl)
123
	{
124
		return $_SESSION['own_comment'][$comment_srl];
125
	}
126
127
	/**
128
	 * Returns the number of child comments
129
	 * @param int $comment_srl
130
	 * @return int
131
	 */
132
	function getChildCommentCount($comment_srl)
133
	{
134
		$args = new stdClass();
135
		$args->comment_srl = $comment_srl;
136
		$output = executeQuery('comment.getChildCommentCount', $args, NULL, 'master');
0 ignored issues
show
Unused Code introduced by
The call to executeQuery() has too many arguments starting with 'master'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
137
		return (int) $output->data->count;
138
	}
139
140
	/**
141
	 * Returns the number of child comments
142
	 * @param int $comment_srl
143
	 * @return int
144
	 */
145
	function getChildComments($comment_srl)
146
	{
147
		$args = new stdClass();
148
		$args->comment_srl = $comment_srl;
149
		$output = executeQueryArray('comment.getChildComments', $args, NULL, 'master');
0 ignored issues
show
Unused Code introduced by
The call to executeQueryArray() has too many arguments starting with 'master'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
150
		return $output->data;
151
	}
152
153
	/**
154
	 * Get the comment
155
	 * @param int $comment_srl
156
	 * @param bool $is_admin
157
	 * @param array $columnList
158
	 * @return commentItem
159
	 */
160
	function getComment($comment_srl = 0, $is_admin = FALSE, $columnList = array())
161
	{
162
		$oComment = new commentItem($comment_srl, $columnList);
163
		if($is_admin)
164
		{
165
			$oComment->setGrant();
166
		}
167
168
		return $oComment;
169
	}
170
171
	/**
172
	 * Get the comment list(not paginating)
173
	 * @param string|array $comment_srl_list
174
	 * @param array $columnList
175
	 * @return array
176
	 */
177
	function getComments($comment_srl_list, $columnList = array())
178
	{
179
		if(is_array($comment_srl_list))
180
		{
181
			$comment_srls = implode(',', $comment_srl_list);
182
		}
183
184
		// fetch from a database
185
		$args = new stdClass();
186
		$args->comment_srls = $comment_srls;
0 ignored issues
show
Bug introduced by
The variable $comment_srls 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...
187
		$output = executeQuery('comment.getComments', $args, $columnList);
188
		if(!$output->toBool())
189
		{
190
			return;
191
		}
192
193
		$comment_list = $output->data;
194
		if(!$comment_list)
195
		{
196
			return;
197
		}
198
		if(!is_array($comment_list))
199
		{
200
			$comment_list = array($comment_list);
201
		}
202
203
		$comment_count = count($comment_list);
0 ignored issues
show
Unused Code introduced by
$comment_count 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...
204 View Code Duplication
		foreach($comment_list as $key => $attribute)
205
		{
206
			if(!$attribute->comment_srl)
207
			{
208
				continue;
209
			}
210
211
			$oComment = NULL;
0 ignored issues
show
Unused Code introduced by
$oComment 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...
212
			$oComment = new commentItem();
213
			$oComment->setAttribute($attribute);
214
			if($is_admin)
0 ignored issues
show
Bug introduced by
The variable $is_admin does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
215
			{
216
				$oComment->setGrant();
217
			}
218
219
			$result[$attribute->comment_srl] = $oComment;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
220
		}
221
		return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
222
	}
223
224
	/**
225
	 * Get the total number of comments in corresponding with document_srl.
226
	 * @param int $document_srl
227
	 * @return int
228
	 */
229
	function getCommentCount($document_srl)
230
	{
231
		$args = new stdClass();
232
		$args->document_srl = $document_srl;
233
234
		// get the number of comments on the document module
235
		$oDocumentModel = getModel('document');
236
		$columnList = array('document_srl', 'module_srl');
237
238
		$oDocument = $oDocumentModel->getDocument($document_srl, FALSE, TRUE, $columnList);
239
240
		// return if no doc exists.
241
		if(!$oDocument->isExists())
242
		{
243
			return;
244
		}
245
246
		// get a list of comments
247
		$module_srl = $oDocument->get('module_srl');
248
249
		//check if module is using validation system
250
		$oCommentController = getController('comment');
251
252
		$using_validation = $oCommentController->isModuleUsingPublishValidation($module_srl);
253
		if($using_validation)
254
		{
255
			$args->status = 1;
256
		}
257
258
		$output = executeQuery('comment.getCommentCount', $args, NULL, 'master');
0 ignored issues
show
Unused Code introduced by
The call to executeQuery() has too many arguments starting with 'master'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
259
		$total_count = $output->data->count;
260
261
		return (int) $total_count;
262
	}
263
264
	/**
265
	 * Get the total number of comments in corresponding with document_srl.
266
	 * @param string $date
267
	 * @param array $moduleSrlList
268
	 * @return int
269
	 */
270
	function getCommentCountByDate($date = '', $moduleSrlList = array())
271
	{
272
		if($date)
273
		{
274
			$args->regDate = date('Ymd', strtotime($date));
0 ignored issues
show
Bug introduced by
The variable $args does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
275
		}
276
277
		if(count($moduleSrlList) > 0)
278
		{
279
			$args->module_srl = $moduleSrlList;
280
		}
281
282
		$output = executeQuery('comment.getCommentCount', $args);
283
		if(!$output->toBool())
284
		{
285
			return 0;
286
		}
287
288
		return $output->data->count;
289
	}
290
291
	/**
292
	 * Get the total number of comments in corresponding with module_srl.
293
	 * @param int $module_srl
294
	 * @param bool $published
295
	 * @return int
296
	 */
297
	function getCommentAllCount($module_srl, $published = null)
298
	{
299
		$args = new stdClass();
300
		$args->module_srl = $module_srl;
301
302
		if(is_null($published))
303
		{
304
			// check if module is using comment validation system
305
			$oCommentController = getController("comment");
306
			$is_using_validation = $oCommentController->isModuleUsingPublishValidation($module_srl);
307
			if($is_using_validation)
308
			{
309
				$args->status = 1;
310
			}
311
		}
312
		else
313
		{
314
			if($published)
315
			{
316
				$args->status = 1;
317
			}
318
			else
319
			{
320
				$args->status = 0;
321
			}
322
		}
323
324
		$output = executeQuery('comment.getCommentCount', $args);
325
		$total_count = $output->data->count;
326
327
		return (int) $total_count;
328
	}
329
330
	/**
331
	 * Get the module info without duplication
332
	 * @return array
333
	 */
334
	function getDistinctModules()
335
	{
336
		return array();
337
338
		/*
339
		$output = executeQueryArray('comment.getDistinctModules');
340
		$module_srls = $output->data;
341
		$oModuleModel = getModel('module');
342
		$result = array();
343
		if($module_srls)
344
		{
345
			foreach($module_srls as $module)
346
			{
347
				$module_info = $oModuleModel->getModuleInfoByModuleSrl($module->module_srl);
348
				$result[$module->module_srl] = $module_info->mid;
349
			}
350
		}
351
		return $result;
352
		*/
353
	}
354
355
	/**
356
	 * Get the comment in corresponding with mid.
357
	 * @todo add commentItems to cache too
358
	 * @param object $obj
359
	 * @param array $columnList
360
	 * @return array
361
	 */
362
	function getNewestCommentList($obj, $columnList = array())
363
	{
364
		$args = new stdClass();
365
366
		if(!is_object($obj))
367
		{
368
			$obj = new stdClass();
369
		}
370
371
		if($obj->mid)
372
		{
373
			$oModuleModel = getModel('module');
374
			$obj->module_srl = $oModuleModel->getModuleSrlByMid($obj->mid);
375
			unset($obj->mid);
376
		}
377
378
		// check if module_srl is an arrary.
379
		if(is_array($obj->module_srl))
380
		{
381
			$args->module_srl = implode(',', $obj->module_srl);
382
		}
383
		else
384
		{
385
			$args->module_srl = $obj->module_srl;
386
		}
387
388
		$args->document_srl = $obj->document_srl;
389
		$args->list_count = $obj->list_count;
390
391
		if(strpos($args->module_srl, ",") === false)
392
		{
393
			if($args->module_srl)
394
			{
395
				// check if module is using comment validation system
396
				$oCommentController = getController("comment");
397
				$is_using_validation = $oCommentController->isModuleUsingPublishValidation($obj->module_srl);
398
				if($is_using_validation)
399
				{
400
					$args->status = 1;
401
				}
402
			}
403
		}
404
405
		$output = executeQuery('comment.getNewestCommentList', $args, $columnList);
406
407
		if(!$output->toBool())
408
		{
409
			return $output;
410
		}
411
412
		$comment_list = $output->data;
413
		if($comment_list)
414
		{
415
			if(!is_array($comment_list))
416
			{
417
				$comment_list = array($comment_list);
418
			}
419
420
			$comment_count = count($comment_list);
0 ignored issues
show
Unused Code introduced by
$comment_count 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...
421
422 View Code Duplication
			foreach($comment_list as $key => $attribute)
423
			{
424
				if(!$attribute->comment_srl)
425
				{
426
					continue;
427
				}
428
429
				$oComment = NULL;
0 ignored issues
show
Unused Code introduced by
$oComment 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...
430
				$oComment = new commentItem();
431
				$oComment->setAttribute($attribute);
432
433
				$result[$key] = $oComment;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
434
			}
435
			$output->data = $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
436
		}
437
438
		return $result;
439
	}
440
441
	/**
442
	 * Get a comment list of the doc in corresponding woth document_srl.
443
	 * @param int $document_srl
444
	 * @param int $page
445
	 * @param bool $is_admin
446
	 * @param int $count
447
	 * @return object
448
	 */
449
	function getCommentList($document_srl, $page = 0, $is_admin = FALSE, $count = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $is_admin is not used and could be removed.

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

Loading history...
450
	{
451
		if(!isset($document_srl))
452
		{
453
			return;
454
		}
455
456
		// get the number of comments on the document module
457
		$oDocumentModel = getModel('document');
458
		$columnList = array('document_srl', 'module_srl', 'comment_count');
459
		$oDocument = $oDocumentModel->getDocument($document_srl, FALSE, TRUE, $columnList);
460
461
		// return if no doc exists.
462
		if(!$oDocument->isExists())
463
		{
464
			return;
465
		}
466
467
		// return if no comment exists
468
		if($oDocument->getCommentCount() < 1)
469
		{
470
			return;
471
		}
472
473
		// get a list of comments
474
		$module_srl = $oDocument->get('module_srl');
475
476
		if(!$count)
477
		{
478
			$comment_config = $this->getCommentConfig($module_srl);
479
			$comment_count = $comment_config->comment_count;
480
			if(!$comment_count)
481
			{
482
				$comment_count = 50;
483
			}
484
		}
485
		else
486
		{
487
			$comment_count = $count;
488
		}
489
490
		// get a very last page if no page exists
491
		if(!$page)
492
		{
493
			$page = (int) ( ($oDocument->getCommentCount() - 1) / $comment_count) + 1;
494
		}
495
496
		// get a list of comments
497
		$args = new stdClass();
498
		$args->document_srl = $document_srl;
499
		$args->list_count = $comment_count;
500
		$args->page = $page;
501
		$args->page_count = 10;
502
503
		//check if module is using validation system
504
		$oCommentController = getController('comment');
505
		$using_validation = $oCommentController->isModuleUsingPublishValidation($module_srl);
506
		if($using_validation)
507
		{
508
			$args->status = 1;
509
		}
510
511
		$output = executeQueryArray('comment.getCommentPageList', $args);
512
513
		// return if an error occurs in the query results
514
		if(!$output->toBool())
515
		{
516
			return;
517
		}
518
519
		// insert data into CommentPageList table if the number of results is different from stored comments
520
		if(!$output->data)
521
		{
522
			$this->fixCommentList($oDocument->get('module_srl'), $document_srl);
523
			$output = executeQueryArray('comment.getCommentPageList', $args);
524
			if(!$output->toBool())
525
			{
526
				return;
527
			}
528
		}
529
530
		return $output;
531
	}
532
533
	/**
534
	 * Update a list of comments in corresponding with document_srl
535
	 * Take care of previously used data than GA version
536
	 * @param int $module_srl
537
	 * @param int $document_srl
538
	 * @return void
539
	 */
540
	function fixCommentList($module_srl, $document_srl)
541
	{
542
		// create a lock file to prevent repeated work when performing a batch job
543
		$lock_file = "./files/cache/tmp/lock." . $document_srl;
544
545
		if(file_exists($lock_file) && filemtime($lock_file) + 60 * 60 * 10 < $_SERVER['REQUEST_TIME'])
546
		{
547
			return;
548
		}
549
550
		FileHandler::writeFile($lock_file, '');
551
552
		// get a list
553
		$args = new stdClass();
554
		$args->document_srl = $document_srl;
555
		$args->list_order = 'list_order';
556
		$output = executeQuery('comment.getCommentList', $args);
557
		if(!$output->toBool())
558
		{
559
			return $output;
560
		}
561
562
		$source_list = $output->data;
563
		if(!is_array($source_list))
564
		{
565
			$source_list = array($source_list);
566
		}
567
568
		// Sort comments by the hierarchical structure
569
		$comment_count = count($source_list);
570
571
		$root = new stdClass;
572
		$list = array();
573
		$comment_list = array();
574
575
		// get the log-in information for logged-in users
576
		$logged_info = Context::get('logged_info');
0 ignored issues
show
Unused Code introduced by
$logged_info 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...
577
578
		// generate a hierarchical structure of comments for loop
579
		for($i = $comment_count - 1; $i >= 0; $i--)
580
		{
581
			$comment_srl = $source_list[$i]->comment_srl;
582
			$parent_srl = $source_list[$i]->parent_srl;
583
			if(!$comment_srl)
584
			{
585
				continue;
586
			}
587
588
			// generate a list
589
			$list[$comment_srl] = $source_list[$i];
590
591
			if($parent_srl)
592
			{
593
				$list[$parent_srl]->child[] = &$list[$comment_srl];
594
			}
595
			else
596
			{
597
				$root->child[] = &$list[$comment_srl];
598
			}
599
		}
600
		$this->_arrangeComment($comment_list, $root->child, 0, NULL);
601
602
		// insert values to the database
603
		if(count($comment_list))
604
		{
605
			foreach($comment_list as $comment_srl => $item)
606
			{
607
				$comment_args = new stdClass();
608
				$comment_args->comment_srl = $comment_srl;
609
				$comment_args->document_srl = $document_srl;
610
				$comment_args->head = $item->head;
611
				$comment_args->arrange = $item->arrange;
612
				$comment_args->module_srl = $module_srl;
613
				$comment_args->regdate = $item->regdate;
614
				$comment_args->depth = $item->depth;
615
616
				executeQuery('comment.insertCommentList', $comment_args);
617
			}
618
		}
619
620
		// remove the lock file if successful.
621
		FileHandler::removeFile($lock_file);
622
	}
623
624
	/**
625
	 * Relocate comments in the hierarchical structure
626
	 * @param array $comment_list
627
	 * @param array $list
628
	 * @param int $depth
629
	 * @param object $parent
630
	 * @return void
631
	 */
632
	function _arrangeComment(&$comment_list, $list, $depth, $parent = NULL)
633
	{
634
		if(!count($list))
635
		{
636
			return;
637
		}
638
639
		foreach($list as $key => $val)
640
		{
641
			if($parent)
642
			{
643
				$val->head = $parent->head;
644
			}
645
			else
646
			{
647
				$val->head = $val->comment_srl;
648
			}
649
650
			$val->arrange = count($comment_list) + 1;
651
652
			if($val->child)
653
			{
654
				$val->depth = $depth;
655
				$comment_list[$val->comment_srl] = $val;
656
				$this->_arrangeComment($comment_list, $val->child, $depth + 1, $val);
657
				unset($val->child);
658
			}
659
			else
660
			{
661
				$val->depth = $depth;
662
				$comment_list[$val->comment_srl] = $val;
663
			}
664
		}
665
	}
666
667
	/**
668
	 * Get all the comments in time decending order(for administrators)
669
	 * @param object $obj
670
	 * @param array $columnList
671
	 * @return object
672
	 */
673
	function getTotalCommentList($obj, $columnList = array())
674
	{
675
		$query_id = 'comment.getTotalCommentList';
676
677
		// Variables
678
		$args = new stdClass();
679
		$args->sort_index = 'list_order';
680
		$args->page = $obj->page ? $obj->page : 1;
681
		$args->list_count = $obj->list_count ? $obj->list_count : 20;
682
		$args->page_count = $obj->page_count ? $obj->page_count : 10;
683
		$args->s_module_srl = $obj->module_srl;
684
		$args->exclude_module_srl = $obj->exclude_module_srl;
685
686
		// check if module is using comment validation system
687
		$oCommentController = getController("comment");
688
		$is_using_validation = $oCommentController->isModuleUsingPublishValidation($obj->module_srl);
689
		if($is_using_validation)
690
		{
691
			$args->s_is_published = 1;
692
		}
693
694
		// Search options
695
		$search_target = $obj->search_target ? $obj->search_target : trim(Context::get('search_target'));
696
		$search_keyword = $obj->search_keyword ? $obj->search_keyword : trim(Context::get('search_keyword'));
697
		if($search_target && $search_keyword)
698
		{
699
			switch($search_target)
700
			{
701 View Code Duplication
				case 'content' :
702
					if($search_keyword)
703
					{
704
						$search_keyword = str_replace(' ', '%', $search_keyword);
705
					}
706
707
					$args->s_content = $search_keyword;
708
					break;
709
710 View Code Duplication
				case 'user_id' :
711
					if($search_keyword)
712
					{
713
						$search_keyword = str_replace(' ', '%', $search_keyword);
714
					}
715
716
					$args->s_user_id = $search_keyword;
717
					$query_id = 'comment.getTotalCommentListWithinMember';
718
					$args->sort_index = 'comments.list_order';
719
					break;
720
721 View Code Duplication
				case 'user_name' :
722
					if($search_keyword)
723
					{
724
						$search_keyword = str_replace(' ', '%', $search_keyword);
725
					}
726
727
					$args->s_user_name = $search_keyword;
728
					break;
729
730 View Code Duplication
				case 'nick_name' :
731
					if($search_keyword)
732
					{
733
						$search_keyword = str_replace(' ', '%', $search_keyword);
734
					}
735
736
					$args->s_nick_name = $search_keyword;
737
					break;
738
739 View Code Duplication
				case 'email_address' :
740
					if($search_keyword)
741
					{
742
						$search_keyword = str_replace(' ', '%', $search_keyword);
743
					}
744
745
					$args->s_email_address = $search_keyword;
746
					break;
747
748 View Code Duplication
				case 'homepage' :
749
					if($search_keyword)
750
					{
751
						$search_keyword = str_replace(' ', '%', $search_keyword);
752
					}
753
754
					$args->s_homepage = $search_keyword;
755
					break;
756
757
				case 'regdate' :
758
					$args->s_regdate = $search_keyword;
759
					break;
760
761
				case 'last_update' :
762
					$args->s_last_upate = $search_keyword;
763
					break;
764
765
				case 'ipaddress' :
766
					$args->s_ipaddress = $search_keyword;
767
					break;
768
769
				case 'is_secret' :
770
					$args->s_is_secret = $search_keyword;
771
					break;
772
773
				case 'is_published' :
774
					if($search_keyword == 'Y')
775
					{
776
						$args->s_is_published = 1;
777
					}
778
779
					if($search_keyword == 'N')
780
					{
781
						$args->s_is_published = 0;
782
					}
783
784
					break;
785
786
				case 'module':
787
					$args->s_module_srl = (int) $search_keyword;
788
					break;
789
790
				case 'member_srl' :
791
					$args->{"s_" . $search_target} = (int) $search_keyword;
792
					break;
793
			}
794
		}
795
796
		// comment.getTotalCommentList query execution
797
		$output = executeQueryArray($query_id, $args, $columnList);
798
799
		// return when no result or error occurance
800
		if(!$output->toBool() || !count($output->data))
801
		{
802
			return $output;
803
		}
804
805
		foreach($output->data as $key => $val)
806
		{
807
			unset($_oComment);
808
			$_oComment = new CommentItem(0);
809
			$_oComment->setAttribute($val);
810
			$output->data[$key] = $_oComment;
811
		}
812
813
		return $output;
814
	}
815
816
	/**
817
	 * Get all the comment count in time decending order(for administrators)
818
	 * @param object $obj
819
	 * @return int
820
	 */
821
	function getTotalCommentCount($obj)
822
	{
823
		$query_id = 'comment.getTotalCommentCountByGroupStatus';
824
825
		// Variables
826
		$args = new stdClass();
827
		$args->s_module_srl = $obj->module_srl;
828
		$args->exclude_module_srl = $obj->exclude_module_srl;
829
830
		// Search options
831
		$search_target = $obj->search_target ? $obj->search_target : trim(Context::get('search_target'));
832
		$search_keyword = $obj->search_keyword ? $obj->search_keyword : trim(Context::get('search_keyword'));
833
834
		if($search_target && $search_keyword)
835
		{
836
			switch($search_target)
837
			{
838 View Code Duplication
				case 'content' :
839
					if($search_keyword)
840
					{
841
						$search_keyword = str_replace(' ', '%', $search_keyword);
842
					}
843
844
					$args->s_content = $search_keyword;
845
					break;
846
847 View Code Duplication
				case 'user_id' :
848
					if($search_keyword)
849
					{
850
						$search_keyword = str_replace(' ', '%', $search_keyword);
851
					}
852
853
					$args->s_user_id = $search_keyword;
854
					$query_id = 'comment.getTotalCommentCountWithinMemberByGroupStatus';
855
					break;
856
857 View Code Duplication
				case 'user_name' :
858
					if($search_keyword)
859
					{
860
						$search_keyword = str_replace(' ', '%', $search_keyword);
861
					}
862
					$args->s_user_name = $search_keyword;
863
864
					break;
865
866 View Code Duplication
				case 'nick_name' :
867
					if($search_keyword)
868
					{
869
						$search_keyword = str_replace(' ', '%', $search_keyword);
870
					}
871
872
					$args->s_nick_name = $search_keyword;
873
					break;
874
875 View Code Duplication
				case 'email_address' :
876
					if($search_keyword)
877
					{
878
						$search_keyword = str_replace(' ', '%', $search_keyword);
879
					}
880
881
					$args->s_email_address = $search_keyword;
882
					break;
883
884 View Code Duplication
				case 'homepage' :
885
					if($search_keyword)
886
					{
887
						$search_keyword = str_replace(' ', '%', $search_keyword);
888
					}
889
890
					$args->s_homepage = $search_keyword;
891
					break;
892
893
				case 'regdate' :
894
					$args->s_regdate = $search_keyword;
895
					break;
896
897
				case 'last_update' :
898
					$args->s_last_upate = $search_keyword;
899
					break;
900
901
				case 'ipaddress' :
902
					$args->s_ipaddress = $search_keyword;
903
					break;
904
905
				case 'is_secret' :
906
					$args->s_is_secret = $search_keyword;
907
					break;
908
909
				case 'member_srl' :
910
					$args->{"s_" . $search_target} = (int) $search_keyword;
911
					break;
912
			}
913
		}
914
915
		$output = executeQueryArray($query_id, $args);
916
917
		// return when no result or error occurance
918
		if(!$output->toBool() || !count($output->data))
919
		{
920
			return $output;
921
		}
922
923
		return $output->data;
924
	}
925
926
	/**
927
	 * Return a configuration of comments for each module
928
	 * @param int $module_srl
929
	 * @return object
930
	 */
931
	function getCommentConfig($module_srl)
932
	{
933
		$oModuleModel = getModel('module');
934
		$comment_config = $oModuleModel->getModulePartConfig('comment', $module_srl);
935
		if(!is_object($comment_config))
936
		{
937
			$comment_config = new stdClass();
938
		}
939
940
		if(!isset($comment_config->comment_count))
941
		{
942
			$comment_config->comment_count = 50;
943
		}
944
945
		return $comment_config;
946
	}
947
948
	/**
949
	 * Return a list of voting member
950
	 * @return void
951
	 */
952
	function getCommentVotedMemberList()
953
	{
954
		$comment_srl = Context::get('comment_srl');
955
		if(!$comment_srl)
956
		{
957
			return new Object(-1, 'msg_invalid_request');
958
		}
959
960
		$point = Context::get('point');
961
		if($point != -1)
962
		{
963
			$point = 1;
964
		}
965
966
		$oCommentModel = getModel('comment');
967
		$oComment = $oCommentModel->getComment($comment_srl, FALSE, FALSE);
968
		$module_srl = $oComment->get('module_srl');
969
		if(!$module_srl)
970
		{
971
			return new Object(-1, 'msg_invalid_request');
972
		}
973
974
		$oModuleModel = getModel('module');
975
		$comment_config = $oModuleModel->getModulePartConfig('comment', $module_srl);
976
977
		$args = new stdClass();
978
979 View Code Duplication
		if($point == -1)
980
		{
981
			if($comment_config->use_vote_down != 'S')
982
			{
983
				return new Object(-1, 'msg_invalid_request');
984
			}
985
986
			$args->below_point = 0;
987
		}
988
		else
989
		{
990
			if($comment_config->use_vote_up != 'S')
991
			{
992
				return new Object(-1, 'msg_invalid_request');
993
			}
994
995
			$args->more_point = 0;
996
		}
997
998
		$args->comment_srl = $comment_srl;
999
		$output = executeQueryArray('comment.getVotedMemberList', $args);
1000
		if(!$output->toBool())
1001
		{
1002
			return $output;
1003
		}
1004
1005
		$oMemberModel = getModel('member');
1006 View Code Duplication
		if($output->data)
1007
		{
1008
			foreach($output->data as $k => $d)
1009
			{
1010
				$profile_image = $oMemberModel->getProfileImage($d->member_srl);
1011
				$output->data[$k]->src = $profile_image->src;
1012
			}
1013
		}
1014
1015
		$this->add('voted_member_list', $output->data);
1016
	}
1017
1018
	/**
1019
	 * Return a secret status by secret field
1020
	 * @return array
1021
	 */
1022
	function getSecretNameList()
1023
	{
1024
		global $lang;
1025
1026
		if(!isset($lang->secret_name_list))
1027
		{
1028
			return array('Y' => 'Secret', 'N' => 'Public');
1029
		}
1030
		else
1031
		{
1032
			return $lang->secret_name_list;
1033
		}
1034
	}
1035
1036
	/**
1037
	 * Get the total number of comments in corresponding with member_srl.
1038
	 * @param int $member_srl
1039
	 * @return int
1040
	 */
1041
	function getCommentCountByMemberSrl($member_srl)
1042
	{
1043
		$args = new stdClass();
1044
		$args->member_srl = $member_srl;
1045
		$output = executeQuery('comment.getCommentCountByMemberSrl', $args);
1046
		return (int) $output->data->count;
1047
	}
1048
1049
1050
	/**
1051
	 * Get comment list of the doc in corresponding woth member_srl.
1052
	 * @param int $member_srl
1053
	 * @param array $columnList
1054
	 * @param int $page
1055
	 * @param bool $is_admin
1056
	 * @param int $count
1057
	 * @return object
1058
	 */
1059 View Code Duplication
	function getCommentListByMemberSrl($member_srl, $columnList = array(), $page = 0, $is_admin = FALSE, $count = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $is_admin is not used and could be removed.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
1060
	{
1061
		$args = new stdClass();
1062
		$args->member_srl = $member_srl;
1063
		$args->list_count = $count;
1064
		$output = executeQuery('comment.getCommentListByMemberSrl', $args, $columnList);
1065
		$comment_list = $output->data;
1066
1067
		if(!$comment_list) return array();
1068
		if(!is_array($comment_list)) $comment_list = array($comment_list);
1069
1070
		return $comment_list;
1071
1072
	}
1073
1074
}
1075
/* End of file comment.model.php */
1076
/* Location: ./modules/comment/comment.model.php */
1077