Top_Poster_Block::setup()   B
last analyzed

Complexity

Conditions 10
Paths 120

Size

Total Lines 103
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 45
nc 120
nop 2
dl 0
loc 103
rs 7.2
c 2
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
3
/**
4
 * @package SimplePortal
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
13
/**
14
 * Top Posters block, shows the top posters on the site, with avatar and name
15
 *
16
 * @param mixed[] $parameters
17
 *        'limit' => number of top posters to show
18
 *        'type' => period to determine the top poster, 0 all time, 1 today, 2 week, 3 month
19
 * @param int $id - not used in this block
20
 * @param boolean $return_parameters if true returns the configuration options for the block
21
 */
22
class Top_Poster_Block extends SP_Abstract_Block
23
{
24
	/**
25
	 * @var array
26
	 */
27
	protected $color_ids = array();
28
29
	/**
30
	 * Constructor, used to define block parameters
31
	 *
32
	 * @param Database|null $db
33
	 */
34
	public function __construct($db = null)
35
	{
36
		$this->block_parameters = array(
37
			'limit' => 'int',
38
			'type' => 'select',
39
		);
40
41
		parent::__construct($db);
42
	}
43
44
	/**
45
	 * Initializes a block for use.
46
	 *
47
	 * - Called from portal.subs as part of the sportal_load_blocks process
48
	 *
49
	 * @param mixed[] $parameters
50
	 * @param int $id
51
	 */
52
	public function setup($parameters, $id)
53
	{
54
		global $txt, $scripturl;
55
56
		$limit = !empty($parameters['limit']) ? (int) $parameters['limit'] : 5;
57
		$type = !empty($parameters['type']) ? (int) $parameters['type'] : 0;
58
59
		// If not top poster of all time we need to set a start time
60
		if (!empty($type))
61
		{
62
			$start_time = time();
63
64
			// Today
65
			if ($type == 1)
66
			{
67
				list($year, $month, $day) = explode('-', date('Y-m-d'));
68
				$start_time = mktime(0, 0, 0, $month, $day, $year);
0 ignored issues
show
Bug introduced by
$day of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
				$start_time = mktime(0, 0, 0, $month, /** @scrutinizer ignore-type */ $day, $year);
Loading history...
Bug introduced by
$month of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
				$start_time = mktime(0, 0, 0, /** @scrutinizer ignore-type */ $month, $day, $year);
Loading history...
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
				$start_time = mktime(0, 0, 0, $month, $day, /** @scrutinizer ignore-type */ $year);
Loading history...
69
			}
70
			// This week
71
			elseif ($type == 2)
72
			{
73
				$start_time = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N") * 3600 * 24);
74
			}
75
			// This month
76
			elseif ($type == 3)
77
			{
78
				$months = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
79
				$start_time = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (3600 * 24 * $months[(int) date("m", time())]);
80
			}
81
82
			$start_time = forum_time(false, $start_time);
83
84
			$request = $this->_db->query('', '
85
				SELECT
86
					mem.id_member, mem.real_name, COUNT(*) as posts, mem.email_address,
87
					mem.avatar, a.id_attach, a.attachment_type, a.filename
88
				FROM {db_prefix}messages AS m
89
					LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
90
					LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
91
				WHERE m.poster_time > {int:start_time}
92
					AND m.id_member != 0
93
				GROUP BY mem.id_member
94
				ORDER BY posts DESC
95
				LIMIT {int:limit}',
96
				array(
97
					'start_time' => $start_time,
98
					'limit' => $limit,
99
				)
100
			);
101
		}
102
		// Or from the start of time
103
		else
104
		{
105
			$request = $this->_db->query('', '
106
				SELECT
107
					m.id_member, m.real_name, m.posts, m.avatar, m.email_address,
108
					a.id_attach, a.attachment_type, a.filename
109
				FROM {db_prefix}members AS m
110
					LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = m.id_member)
111
				ORDER BY posts DESC
112
				LIMIT {int:limit}',
113
				array(
114
					'limit' => $limit,
115
				)
116
			);
117
		}
118
		$this->data['members'] = array();
119
		while ($row = $this->_db->fetch_assoc($request))
120
		{
121
			if (!empty($row['id_member']))
122
			{
123
				$this->color_ids[$row['id_member']] = $row['id_member'];
124
			}
125
126
			// Load the member data
127
			$this->data['members'][] = array(
128
				'id' => $row['id_member'],
129
				'name' => $row['real_name'],
130
				'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
131
				'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
132
				'posts' => comma_format($row['posts']),
133
				'avatar' => determineAvatar(array(
134
					'avatar' => $row['avatar'],
135
					'filename' => $row['filename'],
136
					'id_attach' => $row['id_attach'],
137
					'email_address' => $row['email_address'],
138
					'attachment_type' => $row['attachment_type'],
139
				)),
140
			);
141
		}
142
		$this->_db->free_result($request);
143
144
		// Profile colors?
145
		$this->_color_ids();
146
147
		if (empty($this->data['members']))
148
		{
149
			$this->data['error_msg'] = $txt['error_sp_no_members_found'];
150
			$this->setTemplate('template_sp_topPoster_error');
151
		}
152
		else
153
		{
154
			$this->setTemplate('template_sp_topPoster');
155
		}
156
	}
157
158
	/**
159
	 * Provide the color profile id's
160
	 */
161
	private function _color_ids()
162
	{
163
		global $color_profile;
164
165
		if (sp_loadColors($this->color_ids) !== false)
0 ignored issues
show
introduced by
The condition sp_loadColors($this->color_ids) !== false is always true.
Loading history...
166
		{
167
			foreach ($this->data['members'] as $k => $p)
168
			{
169
				if (!empty($color_profile[$p['id']]['link']))
170
				{
171
					$this->data['members'][$k]['link'] = $color_profile[$p['id']]['link'];
172
				}
173
			}
174
		}
175
	}
176
}
177
178
/**
179
 * Main template for this block
180
 *
181
 * @param mixed[] $data
182
 */
183
function template_sp_topPoster_error($data)
184
{
185
	echo $data['error_msg'];
186
}
187
188
/**
189
 * Main template for this block
190
 *
191
 * @param mixed[] $data
192
 */
193
function template_sp_topPoster($data)
194
{
195
	global $scripturl, $txt;
196
197
	// And output the block
198
	echo '
199
		<table class="sp_fullwidth">';
200
201
	foreach ($data['members'] as $member)
202
	{
203
		echo '
204
			<tr>
205
				<td class="sp_top_poster centertext">', !empty($member['avatar']['href']) ? '
206
					<a href="' . $scripturl . '?action=profile;u=' . $member['id'] . '">
207
						<img src="' . $member['avatar']['href'] . '" alt="' . $member['name'] . '" />
208
					</a>' : '', '
209
				</td>
210
				<td>
211
					', $member['link'], '<br />
212
					<span class="smalltext">', $member['posts'], ' ', $txt['posts'], '</span>
213
				</td>
214
			</tr>';
215
	}
216
217
	echo '
218
		</table>';
219
}
220