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 — master ( 44a98c...636838 )
by gyeong-won
06:04
created

commentModel::getCommentConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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
		// call trigger (before)
512
		$trigger_output = ModuleHandler::triggerCall('comment.getCommentList', 'before', $args);
513
		if($trigger_output instanceof BaseObject && !$trigger_output->toBool())
514
		{
515
			return $output;
0 ignored issues
show
Bug introduced by
The variable $output seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

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