Completed
Pull Request — master (#132)
by Matt
01:19
created

base::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\factory;
12
13
use phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\db\driver\driver_interface;
16
use phpbb\language\language;
17
use phpbb\user;
18
19
/**
20
 * Base ideas class
21
 */
22
class base
23
{
24
	/** @var auth */
25
	protected $auth;
26
27
	/* @var config */
28
	protected $config;
29
30
	/* @var driver_interface */
31
	protected $db;
32
33
	/** @var language */
34
	protected $language;
35
36
	/* @var user */
37
	protected $user;
38
39
	/** @var string */
40
	protected $table_ideas;
41
42
	/** @var string */
43
	protected $table_votes;
44
45
	/** @var string */
46
	protected $table_topics;
47
48
	/** @var string */
49
	protected $php_ext;
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @param auth             $auth
55
	 * @param config           $config
56
	 * @param driver_interface $db
57
	 * @param language         $language
58
	 * @param user             $user
59
	 * @param string           $table_ideas
60
	 * @param string           $table_votes
61
	 * @param string           $table_topics
62
	 * @param string           $phpEx
63
	 */
64
	public function __construct(auth $auth, config $config, driver_interface $db, language $language, user $user, $table_ideas, $table_votes, $table_topics, $phpEx)
65
	{
66
		$this->auth = $auth;
67
		$this->config = $config;
68
		$this->db = $db;
69
		$this->language = $language;
70
		$this->user = $user;
71
72
		$this->php_ext = $phpEx;
73
74
		$this->table_ideas = $table_ideas;
75
		$this->table_votes = $table_votes;
76
		$this->table_topics = $table_topics;
77
	}
78
79
	/**
80
	 * Helper method for inserting new idea data
81
	 *
82
	 * @param array  $data  The array of data to insert
83
	 * @param string $table The name of the table
84
	 *
85
	 * @return int The ID of the inserted row
86
	 */
87
	protected function insert_idea_data(array $data, $table)
88
	{
89
		$sql = 'INSERT INTO ' . $table . '
90
		' . $this->db->sql_build_array('INSERT', $data);
91
		$this->db->sql_query($sql);
92
93
		return (int) $this->db->sql_nextid();
94
	}
95
96
	/**
97
	 * Helper method for updating idea data
98
	 *
99
	 * @param array  $data  The array of data to insert
100
	 * @param int    $id    The ID of the idea
101
	 * @param string $table The name of the table
102
	 *
103
	 * @return void
104
	 */
105
	protected function update_idea_data(array $data, $id, $table)
106
	{
107
		$sql = 'UPDATE ' . $table . '
108
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
109
			WHERE idea_id = ' . (int) $id;
110
		$this->db->sql_query($sql);
111
	}
112
113
	/**
114
	 * Helper method for deleting idea data
115
	 *
116
	 * @param int    $id    The ID of the idea
117
	 * @param string $table The name of the table
118
	 *
119
	 * @return bool True if idea was deleted, false otherwise
120
	 */
121
	protected function delete_idea_data($id, $table)
122
	{
123
		$sql = 'DELETE FROM ' . $table . '
124
			WHERE idea_id = ' . (int) $id;
125
		$this->db->sql_query($sql);
126
127
		return (bool) $this->db->sql_affectedrows();
128
	}
129
}
130