Completed
Pull Request — master (#131)
by Matt
01:30
created

idea   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 288
Duplicated Lines 29.17 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 25
lcom 2
cbo 2
dl 84
loc 288
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get_idea() 11 11 1
A get_idea_by_topic_id() 11 11 1
A get_status_from_id() 0 4 1
A set_status() 0 8 1
A set_duplicate() 15 15 3
A set_rfc() 16 16 3
A set_ticket() 15 15 3
A set_implemented() 16 16 3
A set_title() 0 15 2
A get_title() 0 11 2
A submit() 0 19 2
A delete() 0 19 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
 * 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
 * Class for handling a single idea
21
 */
22
class idea extends base
23
{
24
	/** @var \phpbb\ideas\factory\vote */
25
	protected $vote;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param auth             $auth
31
	 * @param config           $config
32
	 * @param driver_interface $db
33
	 * @param language         $language
34
	 * @param user             $user
35
	 * @param string           $table_ideas
36
	 * @param string           $table_votes
37
	 * @param string           $table_topics
38
	 * @param string           $phpEx
39
	 * @param vote             $vote
40
	 */
41
	public function __construct(auth $auth, config $config, driver_interface $db, language $language, user $user, $table_ideas, $table_votes, $table_topics, $phpEx, vote $vote)
42
	{
43
		parent::__construct($auth, $config, $db, $language, $user, $table_ideas, $table_votes, $table_topics, $phpEx);
44
		$this->vote = $vote;
45
	}
46
47
	/**
48
	 * Returns the specified idea.
49
	 *
50
	 * @param int $id The ID of the idea to return.
51
	 *
52
	 * @return array|false The idea row set, or false if not found.
53
	 */
54 View Code Duplication
	public function get_idea($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...
55
	{
56
		$sql = 'SELECT *
57
			FROM ' . $this->table_ideas . '
58
			WHERE idea_id = ' . (int) $id;
59
		$result = $this->db->sql_query_limit($sql, 1);
60
		$row = $this->db->sql_fetchrow($result);
61
		$this->db->sql_freeresult($result);
62
63
		return $row;
64
	}
65
66
	/**
67
	 * Returns an idea specified by its topic ID.
68
	 *
69
	 * @param int $id The ID of the idea to return.
70
	 *
71
	 * @return array|false The idea row set, or false if not found.
72
	 */
73 View Code Duplication
	public function get_idea_by_topic_id($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...
74
	{
75
		$sql = 'SELECT idea_id
76
			FROM ' . $this->table_ideas . '
77
			WHERE topic_id = ' . (int) $id;
78
		$result = $this->db->sql_query_limit($sql, 1);
79
		$idea_id = (int) $this->db->sql_fetchfield('idea_id');
80
		$this->db->sql_freeresult($result);
81
82
		return $this->get_idea($idea_id);
83
	}
84
85
	/**
86
	 * Returns the status name from the status ID specified.
87
	 *
88
	 * @param int $id ID of the status.
89
	 *
90
	 * @return string|bool The status name if it exists, false otherwise.
91
	 */
92
	public function get_status_from_id($id)
93
	{
94
		return $this->language->lang(array_search($id, self::$statuses));
95
	}
96
97
	/**
98
	 * Updates the status of an idea.
99
	 *
100
	 * @param int $idea_id The ID of the idea.
101
	 * @param int $status  The ID of the status.
102
	 *
103
	 * @return void
104
	 */
105
	public function set_status($idea_id, $status)
106
	{
107
		$sql_ary = array(
108
			'idea_status' => (int) $status,
109
		);
110
111
		$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
112
	}
113
114
	/**
115
	 * Sets the ID of the duplicate for an idea.
116
	 *
117
	 * @param int    $idea_id   ID of the idea to be updated.
118
	 * @param string $duplicate Idea ID of duplicate.
119
	 *
120
	 * @return bool True if set, false if invalid.
121
	 */
122 View Code Duplication
	public function set_duplicate($idea_id, $duplicate)
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...
123
	{
124
		if ($duplicate && !is_numeric($duplicate))
125
		{
126
			return false;
127
		}
128
129
		$sql_ary = array(
130
			'duplicate_id'	=> (int) $duplicate,
131
		);
132
133
		$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
134
135
		return true;
136
	}
137
138
	/**
139
	 * Sets the RFC link of an idea.
140
	 *
141
	 * @param int    $idea_id ID of the idea to be updated.
142
	 * @param string $rfc     Link to the RFC.
143
	 *
144
	 * @return bool True if set, false if invalid.
145
	 */
146 View Code Duplication
	public function set_rfc($idea_id, $rfc)
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...
147
	{
148
		$match = '/^https?:\/\/area51\.phpbb\.com\/phpBB\/viewtopic\.php/';
149
		if ($rfc && !preg_match($match, $rfc))
150
		{
151
			return false;
152
		}
153
154
		$sql_ary = array(
155
			'rfc_link'	=> $rfc, // string is escaped by build_array()
156
		);
157
158
		$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
159
160
		return true;
161
	}
162
163
	/**
164
	 * Sets the ticket ID of an idea.
165
	 *
166
	 * @param int    $idea_id ID of the idea to be updated.
167
	 * @param string $ticket  Ticket ID.
168
	 *
169
	 * @return bool True if set, false if invalid.
170
	 */
171 View Code Duplication
	public function set_ticket($idea_id, $ticket)
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...
172
	{
173
		if ($ticket && !is_numeric($ticket))
174
		{
175
			return false;
176
		}
177
178
		$sql_ary = array(
179
			'ticket_id'	=> (int) $ticket,
180
		);
181
182
		$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
183
184
		return true;
185
	}
186
187
	/**
188
	 * Sets the implemented version of an idea.
189
	 *
190
	 * @param int    $idea_id ID of the idea to be updated.
191
	 * @param string $version Version of phpBB the idea was implemented in.
192
	 *
193
	 * @return bool True if set, false if invalid.
194
	 */
195 View Code Duplication
	public function set_implemented($idea_id, $version)
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...
196
	{
197
		$match = '/^\d\.\d\.\d+(\-\w+)?$/';
198
		if ($version && !preg_match($match, $version))
199
		{
200
			return false;
201
		}
202
203
		$sql_ary = array(
204
			'implemented_version'	=> $version, // string is escaped by build_array()
205
		);
206
207
		$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
208
209
		return true;
210
	}
211
212
	/**
213
	 * Sets the title of an idea.
214
	 *
215
	 * @param int    $idea_id ID of the idea to be updated.
216
	 * @param string $title   New title.
217
	 *
218
	 * @return boolean True if updated, false if invalid length.
219
	 */
220
	public function set_title($idea_id, $title)
221
	{
222
		if (utf8_clean_string($title) === '')
223
		{
224
			return false;
225
		}
226
227
		$sql_ary = array(
228
			'idea_title' => truncate_string($title, self::SUBJECT_LENGTH),
229
		);
230
231
		$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
232
233
		return true;
234
	}
235
236
	/**
237
	 * Get the title of an idea.
238
	 *
239
	 * @param int $id ID of an idea
240
	 *
241
	 * @return string The idea's title, empty string if not found
242
	 */
243
	public function get_title($id)
244
	{
245
		$sql = 'SELECT idea_title
246
			FROM ' . $this->table_ideas . '
247
			WHERE idea_id = ' . (int) $id;
248
		$result = $this->db->sql_query_limit($sql, 1);
249
		$idea_title = $this->db->sql_fetchfield('idea_title');
250
		$this->db->sql_freeresult($result);
251
252
		return $idea_title ?: '';
253
	}
254
255
	/**
256
	 * Submit new idea data to the ideas table
257
	 *
258
	 * @param array $data An array of post data from a newly posted idea
259
	 *
260
	 * @return int The ID of the new idea.
261
	 */
262
	public function submit($data)
263
	{
264
		$sql_ary = [
265
			'idea_title'	=> $data['topic_title'],
266
			'idea_author'	=> $data['poster_id'],
267
			'idea_date'		=> $data['post_time'],
268
			'topic_id'		=> $data['topic_id'],
269
		];
270
271
		$idea_id = $this->insert_idea_data($sql_ary, $this->table_ideas);
272
273
		// Initial vote
274
		if (($idea = $this->get_idea($idea_id)) !== false)
275
		{
276
			$this->vote->submit($idea, $data['poster_id'], 1);
277
		}
278
279
		return $idea_id;
280
	}
281
282
	/**
283
	 * Deletes an idea and the topic to go with it.
284
	 *
285
	 * @param int $id       The ID of the idea to be deleted.
286
	 * @param int $topic_id The ID of the idea topic. Optional, but preferred.
287
	 *
288
	 * @return boolean Whether the idea was deleted or not.
289
	 */
290
	public function delete($id, $topic_id = 0)
291
	{
292
		if (!$topic_id)
293
		{
294
			$idea = $this->get_idea($id);
295
			$topic_id = $idea['topic_id'];
296
		}
297
298
		// Delete topic
299
		delete_posts('topic_id', $topic_id);
300
301
		// Delete idea
302
		$deleted = $this->delete_idea_data($id, $this->table_ideas);
303
304
		// Delete votes
305
		$this->delete_idea_data($id, $this->table_votes);
306
307
		return $deleted;
308
	}
309
}
310