Completed
Push — master ( 1f1439...c96b51 )
by Daniel
08:29
created

userlist   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 87
ccs 36
cts 36
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_user_id() 0 14 3
A update() 0 19 3
A _get_userlist() 0 5 1
A _get_next_user() 0 10 2
A _get_next_key() 0 7 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class userlist
13
{
14
	/**
15
	 * @param string $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
16
	 * @param int $current_user
0 ignored issues
show
Bug introduced by
There is no parameter named $current_user. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
17
	 * @param bool $change_user
18
	 * @return int
19
	 */
20 5
	public static function get_user_id(array &$settings, $change_user)
21
	{
22 5
		$current_user = 0;
23 5
		$userlist = self::_get_userlist($settings['userlist']);
24
25 5
		if ($change_user && sizeof($userlist))
26 5
		{
27 5
			$current_user = self::_get_next_user($settings['current_user'], $userlist);
28
29 5
			$settings['current_user'] = $current_user;
30 5
		}
31
32 5
		return (int) $current_user;
33
	}
34
35
	/**
36
	 * if we're selecting from a list and there is no result, we remove the culprit and update the list
37
	 *
38
	 * @param array $settings
39
	 */
40 2
	public static function update(array &$settings)
41
	{
42 2
		$userlist = self::_get_userlist($settings['userlist']);
43
44 2
		if ($settings['qtype'] === 'featured' && sizeof($userlist))
45 2
		{
46 2
			$curr_key = (int) array_search($settings['current_user'], $userlist);
47
48
			// Remove the invalid user from the list
49 2
			$new_list = str_replace($settings['current_user'] . ',', '', $settings['userlist'] . ',');
50 2
			$new_list = trim($new_list, ',');
51
52 2
			$new_userlist = self::_get_userlist($new_list);
53 2
			$current_user =& $new_userlist[$curr_key - 1];
54 2
			$settings['current_user'] = (int) $current_user;
55 2
			$settings['userlist'] = $new_list;
56 2
			$settings['last_changed'] = 0;
57 2
		}
58 2
	}
59
60
	/**
61
	 * @param string $list
62
	 * @return array
63
	 */
64 5
	private static function _get_userlist($list)
65
	{
66 5
		$userlist = preg_replace("/\s*,?\s*(\r\n|\n\r|\n|\r)+\s*/", "\n", trim($list));
67 5
		return array_map('intval', array_filter(explode(',', $userlist)));
68
	}
69
70
	/**
71
	 * @param $current_user
72
	 * @param $userlist
73
	 * @return int
74
	 */
75 5
	private static function _get_next_user($current_user, array $userlist)
76
	{
77 5
		$key = 0;
78
		if ($current_user)
79 5
		{
80 3
			$key = self::_get_next_key($current_user, $userlist);
81 3
		}
82
83 5
		return $userlist[$key];
84
	}
85
86
	/**
87
	 * @param $current_user
88
	 * @param $userlist
89
	 * @return int
90
	 */
91 3
	private static function _get_next_key($current_user, array $userlist)
92
	{
93 3
		$end_key = sizeof($userlist) - 1;
94 3
		$curr_key = (int) array_search($current_user, $userlist);
95
96 3
		return ($curr_key >= 0 && $curr_key < $end_key) ? ($curr_key + 1) : 0;
97
	}
98
}
99