birthday_list   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 198
Duplicated Lines 8.59 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
dl 17
loc 198
ccs 63
cts 91
cp 0.6923
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A get_template_acp() 10 10 1
A install() 0 5 1
A format_birthday() 0 13 2
A uninstall() 0 5 1
D get_template_side() 0 84 14

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 Birthday List
14
*/
15
class birthday_list 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 = 'BIRTHDAYS';
31
32
	/**
33
	* Default module-image:
34
	* file must be in "{T_THEME_PATH}/images/portal/"
35
	*/
36
	public $image_src = 'portal_birthday.png';
37
38
	/**
39
	* module-language file
40
	* file must be in "language/{$user->lang}/mods/portal/"
41
	*/
42
	public $language = 'portal_birthday_list_module';
43
44
	/** @var \phpbb\config\config */
45
	protected $config;
46
47
	/** @var \phpbb\template\template */
48
	protected $template;
49
50
	/** @var \phpbb\db\driver\driver_interface */
51
	protected $db;
52
53
	/** @var \phpbb\user */
54
	protected $user;
55
56
	/**
57
	* Construct a birthday_list object
58
	*
59
	* @param \phpbb\config\config $config phpBB config
60
	* @param \phpbb\template\template $template phpBB template
61
	* @param \phpbb\db\driver\driver_interface $db Database driver
62
	* @param \phpbb\user $user phpBB user object
63
	*/
64 46 View Code Duplication
	public function __construct($config, $template, $db, $user)
65
	{
66 46
		$this->config = $config;
67 46
		$this->template = $template;
68 46
		$this->db = $db;
69 46
		$this->user = $user;
70 46
	}
71
72
	/**
73
	* {@inheritdoc}
74
	*/
75 1
	public function get_template_side($module_id)
76
	{
77
		// Generate birthday list if required ... / borrowed from index.php 3.0.6
78 1
		$birthday_list = $birthday_ahead_list = '';
79
80 1
		if ($this->config['load_birthdays'] && $this->config['allow_birthdays'])
81 1
		{
82 1
			$time = $this->user->create_datetime();
83 1
			$now = phpbb_gmgetdate($time->getTimestamp() + $time->getOffset());
84 1
			$cache_days = $this->config['board3_birthdays_ahead_' . $module_id];
85 1
			$sql_days = '';
86 1
			while ($cache_days > 0)
87
			{
88 1
				$day = phpbb_gmgetdate($time->getTimestamp() + 86400 * $cache_days + $time->getOffset());
89 1
				$like_expression = $this->db->sql_like_expression($this->db->get_any_char() . (sprintf('%2d-%2d-', $day['mday'], $day['mon'])) . $this->db->get_any_char());
90 1
				$sql_days .= " OR u.user_birthday " . $like_expression . "";
91 1
				$cache_days--;
92 1
			}
93
94 1
			switch ($this->db->get_sql_layer())
95
			{
96 1
				case 'mssql':
97 1
				case 'mssql_odbc':
98
					$order_by = 'u.user_birthday ASC';
99
				break;
100
101 1
				default:
102 1
					$order_by = 'SUBSTRING(u.user_birthday FROM 4 FOR 2) ASC, SUBSTRING(u.user_birthday FROM 1 FOR 2) ASC, u.username_clean ASC';
103 1
				break;
104 1
			}
105
106
			$sql_array = array(
107 1
				'SELECT'		=> 'u.user_id, u.username, u.user_colour, u.user_birthday',
108 1
				'FROM'		=> array(USERS_TABLE	=> 'u'),
109
				'LEFT_JOIN'	=> array(
110
					array(
111 1
						'FROM'		=> array(BANLIST_TABLE	=> 'b'),
112 1
						'ON'		=> 'u.user_id = b.ban_userid',
113 1
					),
114 1
				),
115
				'WHERE'		=> "(b.ban_id IS NULL
116
						OR b.ban_exclude = 1)
117 1
					AND (u.user_birthday " . $this->db->sql_like_expression($this->db->get_any_char() . sprintf('%2d-%2d-', $now['mday'], $now['mon']) . $this->db->get_any_char()) . " {$sql_days})
118 1
					AND " . $this->db->sql_in_set('u.user_type', array(USER_NORMAL , USER_FOUNDER)),
119 1
				'ORDER BY'	=> $order_by,
120 1
			);
121 1
			$sql = $this->db->sql_build_query('SELECT', $sql_array);
122 1
			$result = $this->db->sql_query($sql, 300);
123 1
			$today = sprintf('%2d-%2d-', $now['mday'], $now['mon']);
124
125 1
			while ($row = $this->db->sql_fetchrow($result))
126
			{
127
				if (substr($row['user_birthday'], 0, 6) == $today)
128
				{
129
					$birthday_list = true;
130
					$this->template->assign_block_vars('board3_birthday_list', array(
131
						'USER'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
132
						'AGE'		=> ($age = (int) substr($row['user_birthday'], -4)) ? ' (' . ($now['year'] - $age) . ')' : '',
133
					));
134
				}
135
				else if ($this->config['board3_birthdays_ahead_' . $module_id] > 0)
136
				{
137
					$birthday_ahead_list = true;
138
					$this->template->assign_block_vars('board3_birthday_ahead_list', array(
139
						'USER'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
140
						'AGE'		=> ($age = (int) substr($row['user_birthday'], -4)) ? ' (' . ($now['year'] - $age) . ')' : '',
141
						'DATE'		=> $this->format_birthday($this->user, $row['user_birthday'], 'd M'),
142
					));
143
				}
144
			}
145 1
			$this->db->sql_freeresult($result);
146 1
		}
147
148
		// Assign index specific vars
149 1
		$this->template->assign_vars(array(
150 1
			'BIRTHDAY_LIST'					=> $birthday_list,
151 1
			'BIRTHDAYS_AHEAD_LIST'			=> ($this->config['board3_birthdays_ahead_' . $module_id]) ? $birthday_ahead_list : '',
152 1
			'L_BIRTHDAYS_AHEAD'				=> sprintf($this->user->lang['BIRTHDAYS_AHEAD'], $this->config['board3_birthdays_ahead_' . $module_id]),
153 1
			'S_DISPLAY_BIRTHDAY_LIST'		=> ($this->config['load_birthdays']) ? true : false,
154 1
			'S_DISPLAY_BIRTHDAY_AHEAD_LIST'	=> ($this->config['board3_birthdays_ahead_' . $module_id] > 0) ? true : false,
155 1
		));
156
157 1
		return 'birthdays_side.html';
158
	}
159
160
	/**
161
	* {@inheritdoc}
162
	*/
163 1 View Code Duplication
	public function get_template_acp($module_id)
164
	{
165
		return array(
166 1
			'title'	=> 'ACP_PORTAL_BIRTHDAYS_SETTINGS',
167
			'vars'	=> array(
168 1
				'legend1'					=> 'ACP_PORTAL_BIRTHDAYS_SETTINGS',
169 1
				'board3_birthdays_ahead_' . $module_id	=> array('lang' => 'PORTAL_BIRTHDAYS_AHEAD',	'validate' => 'int',	'type' => 'text:3:3',		'explain' => true),
170 1
			),
171 1
		);
172
	}
173
174
	/**
175
	* {@inheritdoc}
176
	*/
177 1
	public function install($module_id)
178
	{
179 1
		$this->config->set('board3_birthdays_ahead_' . $module_id, 30);
180 1
		return true;
181
	}
182
183
	/**
184
	* {@inheritdoc}
185
	*/
186 1
	public function uninstall($module_id, $db)
187
	{
188 1
		$this->config->delete('board3_birthdays_ahead_' . $module_id);
189 1
		return true;
190
	}
191
192
	/**
193
	* Format birthday for span title
194
	*
195
	* @param object $user phpBB user object
196
	* @param string $birthday User's birthday from database
197
	* @param string $date_settings Settings for date() function
198
	*/
199
	protected function format_birthday($user, $birthday, $date_settings)
200
	{
201
		if (!preg_match('/[0-9]{1,2}-[ ]?[0-9]{1,2}-[0-9]{4}/', $birthday))
202
		{
203
			return '';
204
		}
205
206
		$date = explode('-', $birthday);
207
		$time = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
208
		$lang_dates = array_filter($user->lang['datetime'], 'is_string');
209
210
		return strtr(date($date_settings, $time), $lang_dates);
211
	}
212
}
213