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

base::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
c 1
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\ideas\ext;
17
use phpbb\language\language;
18
use phpbb\user;
19
20
/**
21
 * Base ideas class
22
 */
23
class base
24
{
25
	/** @var auth */
26
	protected $auth;
27
28
	/* @var config */
29
	protected $config;
30
31
	/* @var driver_interface */
32
	protected $db;
33
34
	/** @var language */
35
	protected $language;
36
37
	/* @var user */
38
	protected $user;
39
40
	/** @var string */
41
	protected $table_ideas;
42
43
	/** @var string */
44
	protected $table_votes;
45
46
	/** @var string */
47
	protected $table_topics;
48
49
	/** @var string */
50
	protected $php_ext;
51
52
	/**
53
	 * Constructor
54
	 *
55
	 * @param auth             $auth
56
	 * @param config           $config
57
	 * @param driver_interface $db
58
	 * @param language         $language
59
	 * @param user             $user
60
	 * @param string           $table_ideas
61
	 * @param string           $table_votes
62
	 * @param string           $table_topics
63
	 * @param string           $phpEx
64
	 */
65
	public function __construct(auth $auth, config $config, driver_interface $db, language $language, user $user, $table_ideas, $table_votes, $table_topics, $phpEx)
66
	{
67
		$this->auth = $auth;
68
		$this->config = $config;
69
		$this->db = $db;
70
		$this->language = $language;
71
		$this->user = $user;
72
73
		$this->php_ext = $phpEx;
74
75
		$this->table_ideas = $table_ideas;
76
		$this->table_votes = $table_votes;
77
		$this->table_topics = $table_topics;
78
	}
79
80
	/**
81
	 * Helper method for inserting new idea data
82
	 *
83
	 * @param array  $data  The array of data to insert
84
	 * @param string $table The name of the table
85
	 *
86
	 * @return int The ID of the inserted row
87
	 */
88
	protected function insert_idea_data(array $data, $table)
89
	{
90
		$sql = 'INSERT INTO ' . $table . '
91
		' . $this->db->sql_build_array('INSERT', $data);
92
		$this->db->sql_query($sql);
93
94
		return (int) $this->db->sql_nextid();
95
	}
96
97
	/**
98
	 * Helper method for updating idea data
99
	 *
100
	 * @param array  $data  The array of data to insert
101
	 * @param int    $id    The ID of the idea
102
	 * @param string $table The name of the table
103
	 *
104
	 * @return void
105
	 */
106
	protected function update_idea_data(array $data, $id, $table)
107
	{
108
		$sql = 'UPDATE ' . $table . '
109
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
110
			WHERE idea_id = ' . (int) $id;
111
		$this->db->sql_query($sql);
112
	}
113
114
	/**
115
	 * Helper method for deleting idea data
116
	 *
117
	 * @param int    $id    The ID of the idea
118
	 * @param string $table The name of the table
119
	 *
120
	 * @return bool True if idea was deleted, false otherwise
121
	 */
122
	protected function delete_idea_data($id, $table)
123
	{
124
		$sql = 'DELETE FROM ' . $table . '
125
			WHERE idea_id = ' . (int) $id;
126
		$this->db->sql_query($sql);
127
128
		return (bool) $this->db->sql_affectedrows();
129
	}
130
}
131