Completed
Push — master ( d043ad...19080b )
by Paul
03:04
created

abstract_module   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 12
loc 66
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A show_ucp_complete() 0 21 2
A check_table_for_user() 12 12 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
 * 2FA extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Paul Sohier
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
12
namespace paul999\tfa\modules;
13
14
abstract class abstract_module implements module_interface
15
{
16
	/**
17
	 * @var \phpbb\db\driver\driver_interface
18
	 */
19
	protected $db;
20
21
	/**
22
	 * @var \phpbb\user
23
	 */
24
	protected $user;
25
26
	/**
27
	 * @var \phpbb\template\template
28
	 */
29
	protected $template;
30
31
	/**
32
	 * This method is called to show the UCP page.
33
	 * You can assign template variables to the template, or do anything else here.
34
	 *
35
	 * @param string $table
36
	 */
37
	protected function show_ucp_complete($table)
38
	{
39
		$sql = 'SELECT *
40
			FROM ' . $table . '
41
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
42
			ORDER BY registration_id ASC';
43
44
		$result = $this->db->sql_query($sql);
45
46
		while ($row = $this->db->sql_fetchrow($result))
47
		{
48
			$this->template->assign_block_vars('keys', array(
49
				'CLASS'         => $this->get_name(),
50
				'ID'            => $row['registration_id'],
51
				'REGISTERED'    => $this->user->format_date($row['registered']),
52
				'LAST_USED'     => $this->user->format_date($row['last_used']),
53
				'TYPE'			=> $this->user->lang($this->get_translatable_name()),
54
			));
55
		}
56
		$this->db->sql_freeresult($result);
57
	}
58
59
	/**
60
	 * Check if the provided user as a specific key in the table provided
61
	 *
62
	 * @param string $table Table to check in
63
	 * @param int    $user_id The specific user
64
	 *
65
	 * @return bool
66
	 */
67 View Code Duplication
	protected function check_table_for_user($table, $user_id)
0 ignored issues
show
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...
68
	{
69
		$sql = 'SELECT COUNT(registration_id) as reg_id 
70
					FROM ' . $table . ' 
71
					WHERE 
72
						user_id = ' . (int) $user_id;
73
		$result = $this->db->sql_query($sql);
74
		$row = $this->db->sql_fetchrow($result);
75
		$this->db->sql_freeresult($result);
76
77
		return $row && $row['reg_id'] > 0;
78
	}
79
}