statistics   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 234
Duplicated Lines 3.42 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 8
loc 234
ccs 0
cts 106
cp 0
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A get_template_side() 0 55 3
A get_template_acp() 0 7 1
B get_topics_count() 0 39 5
A get_first_day_average() 0 4 2
A get_totals_language() 0 9 3
A get_average_language() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
*
4
* @package Board3 Portal v2.1
5
* @copyright (c) 2013 Board3 Group ( www.board3.de )
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace board3\portal\modules;
11
12
/**
13
* @package Statistics
14
*/
15
class statistics extends module_base
16
{
17
	/**
18
	* Allowed columns: Just sum up your options (Exp: left + right = 10)
19
	* top		1
20
	* left		2
21
	* center	4
22
	* right		8
23
	* bottom	16
24
	*/
25
	public $columns = 10;
26
27
	/**
28
	* Default modulename
29
	*/
30
	public $name = 'STATISTICS';
31
32
	/**
33
	* Default module-image:
34
	* file must be in "{T_THEME_PATH}/images/portal/"
35
	*/
36
	public $image_src = 'portal_statistics.png';
37
38
	/**
39
	* module-language file
40
	* file must be in "language/{$user->lang}/mods/portal/"
41
	*/
42
	public $language = 'portal_statistics_module';
43
44
	/**
45
	* custom acp template
46
	* file must be in "adm/style/portal/"
47
	*/
48
	public $custom_acp_tpl = '';
49
50
	/** @var \phpbb\cache\service */
51
	protected $cache;
52
53
	/** @var \phpbb\config\config */
54
	protected $config;
55
56
	/** @var \phpbb\db\driver\driver_interface */
57
	protected $db;
58
59
	/** @var \phpbb\template\template */
60
	protected $template;
61
62
	/** @var \phpbb\user */
63
	protected $user;
64
65
	/** @var double Board days */
66
	protected $board_days;
67
68
	/**
69
	* Construct a search object
70
	*
71
	* @param \phpbb\cache\service $cache phpBB cache system
72
	* @param \phpbb\config\config $config phpBB config
73
	* @param \phpbb\db\driver\driver_interface $db phpBB database system
74
	* @param \phpbb\template\template $template phpBB template
75
	* @param \phpbb\user $user phpBB user object
76
	*/
77 View Code Duplication
	public function __construct($cache, $config, $db, $template, $user)
78
	{
79
		$this->cache = $cache;
80
		$this->config = $config;
81
		$this->db = $db;
82
		$this->template = $template;
83
		$this->user = $user;
84
	}
85
86
	/**
87
	* {@inheritdoc}
88
	*/
89
	public function get_template_side($module_id)
90
	{
91
		// Get totals language strings
92
		$l_total_user_s = $this->get_totals_language($this->config['num_users'], 'TOTAL_USERS');
93
		$l_total_post_s = $this->get_totals_language($this->config['num_posts'], 'TOTAL_POSTS', 'TOTAL_POSTS_COUNT');
94
		$l_total_topic_s = $this->get_totals_language($this->config['num_topics'], 'TOTAL_TOPICS');
95
96
		// Average statistics
97
		$this->board_days = (double) ((time() - $this->config['board_startdate']) / 86400);
98
99
		$topics_per_day		= round($this->config['num_topics'] / $this->board_days, 0);
100
		$posts_per_day		= round($this->config['num_posts'] / $this->board_days, 0);
101
		$users_per_day		= round($this->config['num_users'] / $this->board_days, 0);
102
		$topics_per_user	= round($this->config['num_topics'] / $this->config['num_users'], 0);
103
		$posts_per_user		= round($this->config['num_posts'] / $this->config['num_users'], 0);
104
		$posts_per_topic	= ($this->config['num_topics']) ? round($this->config['num_posts'] / $this->config['num_topics'], 0) : 0;
105
106
		// Mitigate incorrect averages on first day
107
		$topics_per_day = $this->get_first_day_average($topics_per_day, $this->config['num_topics']);
108
		$posts_per_day = $this->get_first_day_average($posts_per_day, $this->config['num_posts']);
109
		$users_per_day = $this->get_first_day_average($users_per_day, $this->config['num_users']);
110
		$topics_per_user = $this->get_first_day_average($topics_per_user, $this->config['num_topics']);
111
		$posts_per_user = $this->get_first_day_average($posts_per_user, $this->config['num_topics']);
112
		$posts_per_topic = $this->get_first_day_average($posts_per_topic, $this->config['num_posts']);
113
114
		// Get language variables for averages
115
		$l_topics_per_day_s = $this->get_average_language($this->config['num_topics'], 'TOPICS_PER_DAY');
116
		$l_posts_per_day_s = $this->get_average_language($this->config['num_posts'], 'POSTS_PER_DAY');
117
		$l_users_per_day_s = $this->get_average_language($this->config['num_users'], 'USERS_PER_DAY');
118
		$l_topics_per_user_s = $this->get_average_language($this->config['num_topics'], 'TOPICS_PER_USER');
119
		$l_posts_per_user_s = $this->get_average_language($this->config['num_posts'], 'POSTS_PER_USER');
120
		$l_posts_per_topic_s = $this->get_average_language($this->config['num_posts'], 'POSTS_PER_TOPIC');
121
122
		$topics_count = $this->get_topics_count();
123
124
		// Assign specific vars
125
		$this->template->assign_vars(array(
126
			'B3_TOTAL_POSTS'				=> $l_total_post_s,
127
			'B3_TOTAL_TOPICS'				=> $l_total_topic_s,
128
			'B3_TOTAL_USERS'				=> $l_total_user_s,
129
			'B3_NEWEST_USER'				=> sprintf($this->user->lang['NEWEST_USER'], get_username_string('full', $this->config['newest_user_id'], $this->config['newest_username'], $this->config['newest_user_colour'])),
130
			'B3_ANNOUNCE_COUNT'				=> $topics_count[POST_ANNOUNCE],
131
			'B3_STICKY_COUNT'				=> $topics_count[POST_STICKY],
132
			'B3_TOTAL_ATTACH'				=> ($this->config['allow_attachments']) ? $this->config['num_files'] : 0,
133
134
			// average stat
135
			'B3_TOPICS_PER_DAY'		=> sprintf($this->user->lang[$l_topics_per_day_s], $topics_per_day),
136
			'B3_POSTS_PER_DAY'		=> sprintf($this->user->lang[$l_posts_per_day_s], $posts_per_day),
137
			'B3_USERS_PER_DAY'		=> sprintf($this->user->lang[$l_users_per_day_s], $users_per_day),
138
			'B3_TOPICS_PER_USER'	=> sprintf($this->user->lang[$l_topics_per_user_s], $topics_per_user),
139
			'B3_POSTS_PER_USER'		=> sprintf($this->user->lang[$l_posts_per_user_s], $posts_per_user),
140
			'B3_POSTS_PER_TOPIC'	=> sprintf($this->user->lang[$l_posts_per_topic_s], $posts_per_topic),
141
		));
142
		return 'statistics_side.html';
143
	}
144
145
	/**
146
	* {@inheritdoc}
147
	*/
148
	public function get_template_acp($module_id)
149
	{
150
		return array(
151
			'title'	=> 'STATISTICS',
152
			'vars'	=> array(),
153
		);
154
	}
155
156
	/**
157
	* Get topics count by type
158
	*
159
	* @return array	Topics count array with type in array keys and count
160
	*		in array values
161
	*/
162
	public function get_topics_count()
163
	{
164
		if (($return_ary = $this->cache->get('_b3p_topics_type_count')) === false)
165
		{
166
			$return_ary = array(
167
				POST_ANNOUNCE => 0,
168
				POST_STICKY => 0,
169
			);
170
171
			$sql_in = array(
172
				POST_ANNOUNCE,
173
				POST_STICKY,
174
			);
175
176
			$sql = 'SELECT DISTINCT(topic_id) AS topic_id, topic_type AS type
177
						FROM ' . TOPICS_TABLE . '
178
						WHERE ' . $this->db->sql_in_set('topic_type', $sql_in, false);
179
			$result = $this->db->sql_query($sql);
180
			while ($row = $this->db->sql_fetchrow($result))
181
			{
182
				switch ($row['type'])
183
				{
184
					case POST_ANNOUNCE:
185
						++$return_ary[POST_ANNOUNCE];
186
					break;
187
188
					case POST_STICKY:
189
						++$return_ary[POST_STICKY];
190
					break;
191
				}
192
			}
193
			$this->db->sql_freeresult($result);
194
195
			// cache topics type count for 1 hour
196
			$this->cache->put('_b3p_topics_type_count', $return_ary, 3600);
197
		}
198
199
		return $return_ary;
200
	}
201
202
	/**
203
	 * Get correct average per day on first day.
204
	 * The per day average will be higher than the total amount. This will
205
	 * result in incorrect statistics.
206
	 *
207
	 * @param int $average Average per day
208
	 * @param int $total Total value
209
	 *
210
	 * @return int Corrected average per day, if correction was necessary
211
	 */
212
	protected function get_first_day_average($average, $total)
213
	{
214
		return ($average > $total) ? $total : $average;
215
	}
216
217
	/**
218
	 * Get language string for totals
219
	 *
220
	 * @param int $total The total value
221
	 * @param string $language_variable Language variable of the total
222
	 * @param string $count_language_variable Optional language variable for count
223
	 *
224
	 * @return string Language string for total
225
	 */
226
	protected function get_totals_language($total, $language_variable, $count_language_variable = '')
227
	{
228
		if ($count_language_variable === '')
229
		{
230
			$count_language_variable = $language_variable;
231
		}
232
233
		return ($total == 0) ? sprintf($this->user->lang[$language_variable . '_ZERO'], $total) : sprintf($this->user->lang[$count_language_variable][2], $total);
234
	}
235
236
	/**
237
	 * Get language variable for averages
238
	 *
239
	 * @param int $total The total value
240
	 * @param string $language_variable Language variable of the total
241
	 *
242
	 * @return string Language string for total
243
	 */
244
	protected function get_average_language($total, $language_variable)
245
	{
246
		return ($total == 0) ? $language_variable . '_ZERO' : $language_variable . '_OTHER';
247
	}
248
}
249