Completed
Push — develop ( 843010...da46c8 )
by Daniel
07:37
created

helper   D

Complexity

Total Complexity 96

Size/Duplication

Total Lines 421
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 96
lcom 1
cbo 0
dl 0
loc 421
ccs 0
cts 180
cp 0
rs 4.8717
c 0
b 0
f 0

38 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_edit_url() 0 5 2
A get_delete_url() 0 5 3
A get_quote_url() 0 4 3
A get_viewtopic_url() 0 4 1
A get_print_topic_url() 0 4 2
A get_email_topic_url() 0 4 3
A get_search_users_posts_url() 0 4 1
A get_info_url() 0 5 2
A get_approve_url() 0 4 1
A get_mcp_edit_url() 0 4 1
A get_mcp_report_url() 0 4 2
A get_mcp_restore_url() 0 4 3
A get_notes_url() 0 4 2
A get_warning_url() 0 4 4
A get_mcp_queue_url() 0 4 1
A get_mcp_review_url() 0 4 1
A display_attachments_notice() 0 4 2
A permanent_delete_allowed() 0 5 3
A user_is_poster() 0 4 1
A can_report_post() 0 4 1
A topic_has_unapproved_posts() 0 4 3
A topic_is_reported() 0 4 4
A topic_is_locked() 0 4 3
A post_is_unapproved() 0 4 4
A get_cp_param() 0 9 4
B edit_allowed() 0 8 5
A quote_allowed() 0 6 4
A post_is_quotable() 0 4 2
B delete_allowed() 0 9 8
A softdelete_allowed() 0 5 4
A cannot_edit() 0 4 2
A cannot_edit_time() 0 4 2
A cannot_edit_locked() 0 4 2
A cannot_delete() 0 7 4
A cannot_delete_lastpost() 0 4 1
A cannot_delete_time() 0 4 2
A cannot_delete_locked() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use helper, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services;
11
12
class helper
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\db */
18
	protected $config;
19
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/** @var string */
24
	protected $phpbb_root_path;
25
26
	/** @var string */
27
	protected $php_ext;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\auth\auth		$auth				Auth object
33
	 * @param \phpbb\config\db		$config				Config object
34
	 * @param \phpbb\user			$user				User object
35
	 * @param string				$phpbb_root_path	Path to the phpbb includes directory.
36
	 * @param string				$php_ext			php file extension
37
	*/
38
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\db $config, \phpbb\user $user, $phpbb_root_path, $php_ext)
39
	{
40
		$this->auth = $auth;
41
		$this->config = $config;
42
		$this->user = $user;
43
		$this->phpbb_root_path = $phpbb_root_path;
44
		$this->php_ext = $php_ext;
45
	}
46
47
	/**
48
	 * @param array $post_data
49
	 * @param array $topic_data
50
	 * @param string $cp_mode
51
	 * @return string
52
	 */
53
	public function get_edit_url(array $post_data, array $topic_data, $cp_mode = '')
54
	{
55
		$cp_param = $this->get_cp_param($post_data, $topic_data, $cp_mode);
56
		return ($this->edit_allowed($post_data, $topic_data)) ? append_sid("{$this->phpbb_root_path}posting.$this->php_ext", "mode=edit&amp;f={$topic_data['forum_id']}&amp;p={$post_data['post_id']}" . $cp_param) : '';
57
	}
58
59
	/**
60
	 * @param array $post_data
61
	 * @param array $topic_data
62
	 * @param string $cp_mode
63
	 * @return string
64
	 */
65
	public function get_delete_url(array $post_data, array $topic_data, $cp_mode = '')
66
	{
67
		$cp_param = $this->get_cp_param($post_data, $topic_data, $cp_mode);
68
		return ($this->delete_allowed($post_data, $topic_data)) ? append_sid("{$this->phpbb_root_path}posting.$this->php_ext", 'mode=' . (($this->softdelete_allowed($topic_data, $post_data)) ? 'soft_delete' : 'delete') . "&amp;f={$post_data['forum_id']}&amp;p={$post_data['post_id']}" . $cp_param) : '';
0 ignored issues
show
Unused Code introduced by
The call to helper::softdelete_allowed() has too many arguments starting with $post_data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
	}
70
71
	/**
72
	 * @param array $post_data
73
	 * @param array $topic_data
74
	 * @return string
75
	 */
76
	public function get_quote_url(array $post_data, array $topic_data)
77
	{
78
		return ($this->post_is_quotable($post_data, $topic_data) && $this->quote_allowed($topic_data)) ? append_sid("{$this->phpbb_root_path}posting.$this->php_ext", "mode=quote&amp;f={$topic_data['forum_id']}&amp;p={$post_data['post_id']}") : '';
79
	}
80
81
	/**
82
	 * @param array $topic_data
83
	 * @return string
84
	 */
85
	public function get_viewtopic_url(array $topic_data)
86
	{
87
		return append_sid("{$this->phpbb_root_path}viewtopic.$this->php_ext", "f={$topic_data['forum_id']}&amp;t={$topic_data['topic_id']}");
88
	}
89
90
	/**
91
	 * @param array $topic_data
92
	 * @return string
93
	 */
94
	public function get_print_topic_url(array $topic_data)
95
	{
96
		return ($this->auth->acl_get('f_print', $topic_data['forum_id'])) ? $topic_data['topic_url'] . '?view=print' : '';
97
	}
98
99
	/**
100
	 * @param array $topic_data
101
	 * @return string
102
	 */
103
	public function get_email_topic_url(array $topic_data)
104
	{
105
		return ($this->auth->acl_get('f_email', $topic_data['forum_id']) && $this->config['email_enable']) ? append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=email&amp;t=' . $topic_data['topic_id']) : '';
106
	}
107
108
	/**
109
	 * @param int $forum_id
110
	 * @param string $username
111
	 * @return string
112
	 */
113
	public function get_search_users_posts_url($forum_id, $username)
114
	{
115
		return append_sid("{$this->phpbb_root_path}search.{$this->php_ext}", "author={$username}&amp;" . urlencode('fid[]') . "=$forum_id&amp;sc=0&amp;sf=titleonly&amp;sr=topics");
116
	}
117
118
	/**
119
	 * @param array $post_data
120
	 * @return string
121
	 */
122
	public function get_info_url(array $post_data)
123
	{
124
		$forum_id = $post_data['forum_id'];
125
		return ($this->auth->acl_get('m_info', $forum_id)) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", "i=main&amp;mode=post_details&amp;f=$forum_id&amp;p=" . $post_data['post_id'], true, $this->user->session_id) : '';
126
	}
127
128
	/**
129
	 * @param array $post_data
130
	 * @param string $viewtopic_url
131
	 * @return string
132
	 */
133
	public function get_approve_url(array $post_data, $viewtopic_url)
134
	{
135
		return append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", "i=queue&amp;p={$post_data['post_id']}&amp;f={$post_data['forum_id']}&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url . '&amp;p=' . $post_data['post_id'] . '#p' . $post_data['post_id'])));
136
	}
137
138
	/**
139
	 * @param array $post_data
140
	 * @param array $topic_data
141
	 * @return string
142
	 */
143
	public function get_mcp_edit_url(array $post_data, array $topic_data)
144
	{
145
		return append_sid("{$this->phpbb_root_path}posting.$this->php_ext", "mode=edit&amp;f={$topic_data['forum_id']}&amp;p={$post_data['post_id']}&amp;cp=mcp");
146
	}
147
148
	/**
149
	 * @param array $post_data
150
	 * @return string
151
	 *
152
	public function get_mcp_approve_url(array $post_data)
153
	{
154
		return append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&amp;mode=approve_details&amp;f=' . $post_data['forum_id'] . '&amp;p=' . $post_data['post_id'], true, $this->user->session_id);
155
	}
156
	*/
157
158
	/**
159
	 * @param array $post_data
160
	 * @return string
161
	 */
162
	public function get_mcp_report_url(array $post_data)
163
	{
164
		return ($this->auth->acl_get('m_report', $post_data['forum_id'])) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=reports&amp;mode=report_details&amp;f=' . $post_data['forum_id'] . '&amp;p=' . $post_data['post_id'], true, $this->user->session_id) : '';
165
	}
166
167
	/**
168
	 * @param array $post_data
169
	 * @param array $topic_data
170
	 * @return string
171
	 */
172
	public function get_mcp_restore_url(array $post_data, array $topic_data)
173
	{
174
		return ($this->auth->acl_get('m_approve', $post_data['forum_id'])) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&amp;mode=' . (($topic_data['topic_visibility'] != ITEM_DELETED) ? 'deleted_posts' : 'deleted_topics') . '&amp;f=' . $post_data['forum_id'] . '&amp;p=' . $post_data['post_id'], true, $this->user->session_id) : '';
175
	}
176
177
	/**
178
	 * @param array $post_data
179
	 * @return string
180
	 */
181
	public function get_notes_url(array $post_data)
182
	{
183
		return ($this->auth->acl_getf_global('m_')) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=notes&amp;mode=user_notes&amp;u=' . $post_data['poster_id'], true, $this->user->session_id) : '';
184
	}
185
186
	/**
187
	 * @param array $post_data
188
	 * @return string
189
	 */
190
	public function get_warning_url(array $post_data)
191
	{
192
		return ($this->auth->acl_get('m_warn') && !$this->user_is_poster($post_data['poster_id']) && $post_data['poster_id'] != ANONYMOUS) ? append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=warn&amp;mode=warn_post&amp;f=' . $post_data['forum_id'] . '&amp;p=' . $post_data['post_id'], true, $this->user->session_id) : '';
193
	}
194
195
	/**
196
	 * @param int $topic_id
197
	 * @return string
198
	 */
199
	public function get_mcp_queue_url($topic_id)
200
	{
201
		return append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", "i=queue&amp;mode=unapproved_posts&amp;t=$topic_id", true, $this->user->session_id);
202
	}
203
204
	/**
205
	 * @param string $content_type
206
	 * @param int $topic_id
207
	 * @return string
208
	 */
209
	public function get_mcp_review_url($content_type, $topic_id)
210
	{
211
		return append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", "i=-blitze-content-mcp-content_module&amp;mode=content&amp;do=view&amp;type=$content_type&amp;t=$topic_id");
212
	}
213
214
	/**
215
	 * @param array $post_data
216
	 * @return bool
217
	 */
218
	public function display_attachments_notice(array $post_data)
219
	{
220
		return (!$this->auth->acl_get('f_download', $post_data['forum_id']) && $post_data['post_attachment']);
221
	}
222
223
	/**
224
	 * @param array $post_data
225
	 * @return bool
226
	 */
227
	public function permanent_delete_allowed(array $post_data)
228
	{
229
		return ($this->auth->acl_get('m_delete', $post_data['forum_id']) ||
230
		($this->auth->acl_get('f_delete', $post_data['forum_id']) && $this->user->data['user_id'] == $post_data['poster_id']));
231
	}
232
233
	/**
234
	 * @param int $poster_id
235
	 * @return bool
236
	 */
237
	public function user_is_poster($poster_id)
238
	{
239
		return ($poster_id == $this->user->data['user_id']);
240
	}
241
242
	/**
243
	 * @param int $forum_id
244
	 * @return bool
245
	 */
246
	public function can_report_post($forum_id)
247
	{
248
		return ($this->auth->acl_get('f_report', $forum_id));
249
	}
250
251
	/**
252
	 * @param array $topic_data
253
	 * @return bool
254
	 */
255
	public function topic_has_unapproved_posts(array $topic_data)
256
	{
257
		return ($topic_data['topic_visibility'] == ITEM_APPROVED && $topic_data['topic_posts_unapproved'] && $this->auth->acl_get('m_approve', $topic_data['forum_id']));
258
	}
259
260
	/**
261
	 * @param array $topic_data
262
	 * @return bool
263
	 */
264
	public function topic_is_reported(array $topic_data)
265
	{
266
		return ($topic_data['topic_reported'] && !$topic_data['topic_moved_id'] && $this->auth->acl_get('m_report', $topic_data['forum_id'])) ? true : false;
267
	}
268
269
	/**
270
	 * @param array $topic_data
271
	 * @return bool
272
	 */
273
	public function topic_is_locked(array $topic_data)
274
	{
275
		return ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true;
276
	}
277
278
	/**
279
	 * @param array $post_data
280
	 * @return bool
281
	 */
282
	public function post_is_unapproved(array $post_data)
283
	{
284
		return (($post_data['post_visibility'] == ITEM_UNAPPROVED || $post_data['post_visibility'] == ITEM_REAPPROVE) && $this->auth->acl_get('m_approve', $post_data['forum_id'])) ? true : false;
285
	}
286
287
	/**
288
	 * @param array $post_data
289
	 * @param array $topic_data
290
	 * @param string $cp_mode
291
	 * @return string
292
	 */
293
	protected function get_cp_param(array $post_data, array $topic_data, $cp_mode)
294
	{
295
		$cp_param = '';
296
		if ($topic_data['topic_first_post_id'] == $post_data['post_id'])
297
		{
298
			$cp_param = '&amp;cp=' . ((!$cp_mode) ? (($this->user_is_poster($post_data['poster_id'])) ? 'ucp' : 'mcp') : $cp_mode);
299
		}
300
		return $cp_param;
301
	}
302
303
	/**
304
	 * @param array $post_data
305
	 * @param array $topic_data
306
	 * @return bool
307
	 */
308
	protected function edit_allowed(array $post_data, array $topic_data)
309
	{
310
		return ($this->user->data['is_registered'] && ($this->auth->acl_get('m_edit', $post_data['forum_id']) || (
311
			!$this->cannot_edit($post_data) &&
312
			!$this->cannot_edit_time($post_data) &&
313
			!$this->cannot_edit_locked($post_data, $topic_data)
314
		)));
315
	}
316
317
	/**
318
	 * @param array $topic_data
319
	 * @return bool
320
	 */
321
	protected function quote_allowed(array $topic_data)
322
	{
323
		return $this->auth->acl_get('m_edit', $topic_data['forum_id']) || ($topic_data['topic_status'] != ITEM_LOCKED &&
324
			($this->user->data['user_id'] == ANONYMOUS || $this->auth->acl_get('f_reply', $topic_data['forum_id']))
325
		);
326
	}
327
328
	/**
329
	 * @param array $post_data
330
	 * @param array $topic_data
331
	 * @return bool
332
	 */
333
	protected function post_is_quotable(array $post_data, array $topic_data)
334
	{
335
		return ($post_data['post_visibility'] == ITEM_APPROVED && $topic_data['topic_first_post_id'] != $post_data['post_id']);
336
	}
337
338
	/**
339
	 * @param array $post_data
340
	 * @param array $topic_data
341
	 * @return bool
342
	 */
343
	protected function delete_allowed(array $post_data, array $topic_data)
344
	{
345
		return ($this->user->data['is_registered'] && (($this->auth->acl_get('m_delete', $post_data['forum_id']) || ($this->auth->acl_get('m_softdelete', $post_data['forum_id']) && $post_data['post_visibility'] != ITEM_DELETED)) || (
346
			!$this->cannot_delete($post_data) &&
347
			!$this->cannot_delete_lastpost($post_data, $topic_data) &&
348
			!$this->cannot_delete_time($post_data) &&
349
			!$this->cannot_delete_locked($post_data, $topic_data)
350
		)));
351
	}
352
353
	/**
354
	 * @param array $post_data
355
	 * @return bool
356
	 */
357
	protected function softdelete_allowed(array $post_data)
358
	{
359
		return ($this->auth->acl_get('m_softdelete', $post_data['forum_id']) ||
360
			($this->auth->acl_get('f_softdelete', $post_data['forum_id']) && $this->user->data['user_id'] == $post_data['poster_id'])) && ($post_data['post_visibility'] != ITEM_DELETED);
361
	}
362
363
	/**
364
	 * @param array $post_data
365
	 * @return bool
366
	 */
367
	protected function cannot_edit(array $post_data)
368
	{
369
		return (!$this->auth->acl_get('f_edit', $post_data['forum_id']) || $this->user->data['user_id'] != $post_data['poster_id']);
370
	}
371
372
	/**
373
	 * @param array $post_data
374
	 * @return bool
375
	 */
376
	protected function cannot_edit_time(array $post_data)
377
	{
378
		return ($this->config['edit_time'] && $post_data['post_time'] <= time() - ($this->config['edit_time'] * 60));
379
	}
380
381
	/**
382
	 * @param array $post_data
383
	 * @param array $topic_data
384
	 * @return bool
385
	 */
386
	protected function cannot_edit_locked(array $post_data, array $topic_data)
387
	{
388
		return ($topic_data['topic_status'] == ITEM_LOCKED || $post_data['post_edit_locked']);
389
	}
390
391
	/**
392
	 * @param array $post_data
393
	 * @return bool
394
	 */
395
	protected function cannot_delete(array $post_data)
396
	{
397
		return $this->user->data['user_id'] != $post_data['poster_id'] || (
398
			!$this->auth->acl_get('f_delete', $post_data['forum_id']) &&
399
			(!$this->auth->acl_get('f_softdelete', $post_data['forum_id']) || $post_data['post_visibility'] == ITEM_DELETED)
400
		);
401
	}
402
403
	/**
404
	 * @param array $post_data
405
	 * @param array $topic_data
406
	 * @return bool
407
	 */
408
	protected function cannot_delete_lastpost(array $post_data, array $topic_data)
409
	{
410
		return $topic_data['topic_last_post_id'] != $post_data['post_id'];
411
	}
412
413
	/**
414
	 * @param array $post_data
415
	 * @return bool
416
	 */
417
	protected function cannot_delete_time(array $post_data)
418
	{
419
		return $this->config['delete_time'] && $post_data['post_time'] <= time() - ($this->config['delete_time'] * 60);
420
	}
421
422
	/**
423
	 * we do not want to allow removal of the last post if a moderator locked it!
424
	 * @param array $post_data
425
	 * @param array $topic_data
426
	 * @return bool
427
	 */
428
	protected function cannot_delete_locked(array $post_data, array $topic_data)
429
	{
430
		return $topic_data['topic_status'] == ITEM_LOCKED || $post_data['post_edit_locked'];
431
	}
432
}
433