Completed
Push — master ( 3fea40...9fe881 )
by Tobias
08:17 queued 01:29
created

listener   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 428
Duplicated Lines 3.97 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 33
Bugs 11 Features 0
Metric Value
wmc 36
c 33
b 11
f 0
lcom 2
cbo 1
dl 17
loc 428
rs 8.8

20 Methods

Rating   Name   Duplication   Size   Complexity  
A append_sid() 0 14 4
B display_forums_modify_template_vars() 0 28 2
A display_forums_modify_category_template_vars() 0 7 1
A generate_forum_nav() 0 15 2
A make_jumpbox_modify_tpl_ary() 0 11 2
A display_forums_modify_forum_rows() 0 11 2
A __construct() 0 11 1
A getSubscribedEvents() 0 23 1
A display_forums_modify_sql() 10 10 1
A memberlist_view_profile() 0 8 1
C pagination_generate_page_link() 0 24 10
A search_modify_tpl_ary() 0 13 1
A viewforum_modify_topicrow() 0 21 1
A viewforum_get_shadowtopic_data() 7 7 1
A viewforum_get_topic_data() 0 14 1
A viewtopic_get_post_data() 0 9 1
A viewtopic_assign_template_vars_before() 0 10 1
A viewtopic_before_f_read_check() 0 4 1
A viewtopic_modify_page_title() 0 8 1
A viewtopic_modify_post_row() 0 8 1

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
 * @package phpBB Extension - tas2580 SEO URLs
5
 * @copyright (c) 2016 tas2580 (https://tas2580.net)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace tas2580\seourls\event;
11
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Event listener
16
 */
17
class listener implements EventSubscriberInterface
18
{
19
	/** @var \tas2580\seourls\event\base */
20
	protected $base;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\request\request */
26
	protected $request;
27
28
	/** @var \phpbb\path_helper */
29
	protected $path_helper;
30
31
	/** @var string phpbb_root_path */
32
	protected $phpbb_root_path;
33
34
	/** @var string php_ext */
35
	protected $php_ext;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \tas2580\seourls\event\base	$base
41
	 * @param \phpbb\template\template		$template				Template object
42
	 * @param \phpbb\request\request		$request				Request object
43
	 * @param \phpbb\path_helper			$path_helper			Controller helper object
44
	 * @param string						$phpbb_root_path		phpbb_root_path
45
	 * @param string						$php_ext				php_ext
46
	 * @access public
47
	 */
48
	public function __construct(\tas2580\seourls\event\base $base, \phpbb\template\template $template, \phpbb\request\request $request, \phpbb\path_helper $path_helper, $phpbb_root_path, $php_ext)
49
	{
50
		$this->base = $base;
51
		$this->template = $template;
52
		$this->request = $request;
53
		$this->path_helper = $path_helper;
54
		$this->phpbb_root_path = $phpbb_root_path;
55
		$this->php_ext = $php_ext;
56
57
		$this->in_viewtopic = false;
58
	}
59
60
	/**
61
	 * Assign functions defined in this class to event listeners in the core
62
	 *
63
	 * @return array
64
	 * @static
65
	 * @access public
66
	 */
67
	public static function getSubscribedEvents()
68
	{
69
		return array(
70
			'core.append_sid'										=> 'append_sid',
71
			'core.display_forums_modify_sql'						=> 'display_forums_modify_sql',
72
			'core.display_forums_modify_template_vars'				=> 'display_forums_modify_template_vars',
73
			'core.display_forums_modify_forum_rows'					=> 'display_forums_modify_forum_rows',
74
			'core.display_forums_modify_category_template_vars'		=> 'display_forums_modify_category_template_vars',
75
			'core.generate_forum_nav'								=> 'generate_forum_nav',
76
			'core.make_jumpbox_modify_tpl_ary'						=> 'make_jumpbox_modify_tpl_ary',				// Not in phpBB
77
			'core.memberlist_view_profile'							=> 'memberlist_view_profile',
78
			'core.pagination_generate_page_link'					=> 'pagination_generate_page_link',
79
			'core.search_modify_tpl_ary'							=> 'search_modify_tpl_ary',
80
			'core.viewforum_modify_topicrow'						=> 'viewforum_modify_topicrow',
81
			'core.viewforum_get_topic_data'							=> 'viewforum_get_topic_data',
82
			'core.viewforum_get_shadowtopic_data'					=> 'viewforum_get_shadowtopic_data',
83
			'core.viewtopic_assign_template_vars_before'			=> 'viewtopic_assign_template_vars_before',
84
			'core.viewtopic_before_f_read_check'					=> 'viewtopic_before_f_read_check',
85
			'core.viewtopic_modify_page_title'						=> 'viewtopic_modify_page_title',
86
			'core.viewtopic_modify_post_row'						=> 'viewtopic_modify_post_row',
87
			'core.viewtopic_get_post_data'							=> 'viewtopic_get_post_data',
88
		);
89
	}
90
91
	/**
92
	 * Correct the path of $viewtopic_url
93
	 *
94
	 * @param	object	$event	The event object
95
	 * @return	null
96
	 * @access	public
97
	 */
98
	public function append_sid($event)
99
	{
100
		if ($this->in_viewtopic && preg_match('#./../viewtopic.' . $this->php_ext  . '#', $event['url']))
101
		{
102
			$url = '../viewtopic.' . $this->php_ext ;
103
			$event['url'] = $url;
104
		}
105
		if (isset($event['params']['redirect']))
106
		{
107
			$params = $event['params'];
108
			$params['redirect'] = str_replace('..', '.', $event['params']['redirect']);
109
			$event['params'] = $params;
110
		}
111
	}
112
113
	/**
114
	 * Get informations for the last post from Database
115
	 *
116
	 * @param	object	$event	The event object
117
	 * @return	null
118
	 * @access	public
119
	 */
120 View Code Duplication
	public function display_forums_modify_sql($event)
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...
121
	{
122
		$sql_array = $event['sql_ary'];
123
		$sql_array['LEFT_JOIN'][] = array(
124
			'FROM' => array(TOPICS_TABLE => 't'),
125
			'ON' => "f.forum_last_post_id = t.topic_last_post_id"
126
		);
127
		$sql_array['SELECT'] .= ', t.topic_title, t.topic_id, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted';
128
		$event['sql_ary'] = $sql_array;
129
	}
130
131
	/**
132
	 * Store informations for the last post in forum_rows array
133
	 *
134
	 * @param	object	$event	The event object
135
	 * @return	null
136
	 * @access	public
137
	 */
138
	public function display_forums_modify_forum_rows($event)
139
	{
140
		$forum_rows = $event['forum_rows'];
141
		if ($event['row']['forum_last_post_time'] == $forum_rows[$event['parent_id']]['forum_last_post_time'])
142
		{
143
			$forum_rows[$event['parent_id']]['forum_name_last_post'] =$event['row']['forum_name'];
144
			$forum_rows[$event['parent_id']]['topic_id_last_post'] =$event['row']['topic_id'];
145
			$forum_rows[$event['parent_id']]['topic_title_last_post'] =$event['row']['topic_title'];
146
			$event['forum_rows'] = $forum_rows;
147
		}
148
	}
149
150
	/**
151
	 * Rewrite links to forums and subforums in forum index
152
	 * also correct the path of the forum images if we are in a forum
153
	 *
154
	 * @param	object	$event	The event object
155
	 * @return	null
156
	 * @access	public
157
	 */
158
	public function display_forums_modify_template_vars($event)
159
	{
160
		$subforums_row = $event['subforums_row'];
161
		$forum_row = $event['forum_row'];
162
163
		// Rewrite URLs of sub forums
164
		foreach ($subforums_row as $i => $subforum)
165
		{
166
			// A little bit a dirty way, but there is no better solution
167
			$query = str_replace('&amp;', '&', parse_url($subforum['U_SUBFORUM'], PHP_URL_QUERY));
168
			parse_str($query, $id);
169
			$subforums_row[$i]['U_SUBFORUM'] = append_sid($this->base->generate_forum_link($id['f'], $subforum['SUBFORUM_NAME']));
170
		}
171
172
		// Update the image source in forums
173
		$img = $this->path_helper->update_web_root_path($forum_row['FORUM_IMAGE_SRC']);
174
		$forum_row['FORUM_IMAGE'] = preg_replace('#img src=\"(.*)\" alt#', 'img src="' . $img . '" alt', $forum_row['FORUM_IMAGE']);
175
176
		// Rewrite links to topics, posts and forums
177
		$replies = $this->base->get_count('topic_posts', $event['row'], $event['row']['forum_id']) - 1;
178
		$url = $this->base->generate_topic_link($event['row']['forum_id_last_post'], $event['row']['forum_name_last_post'], $event['row']['topic_id_last_post'], $event['row']['topic_title_last_post']);
179
		$forum_row['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($replies, $url) . '#p' . $event['row']['forum_last_post_id']);
180
		$forum_row['U_VIEWFORUM'] = append_sid($this->base->generate_forum_link($forum_row['FORUM_ID'], $forum_row['FORUM_NAME']));
181
		$forum_row['U_NEWEST_POST'] = $url . '?view=unread#unread';
182
183
		$event['subforums_row'] = $subforums_row;
184
		$event['forum_row'] = $forum_row;
185
	}
186
187
	/**
188
	 * Rewrite the categorie links
189
	 *
190
	 * @param	object	$event	The event object
191
	 * @return	null
192
	 * @access	public
193
	 */
194
	public function display_forums_modify_category_template_vars($event)
195
	{
196
		$cat_row = $event['cat_row'];
197
		$row = $event['row'];
198
		$cat_row['U_VIEWFORUM'] = append_sid($this->base->generate_forum_link($row['forum_id'], $row['forum_name']));
199
		$event['cat_row'] = $cat_row;
200
	}
201
202
	/**
203
	 * Rewrite links in breadcrumbs
204
	 *
205
	 * @param	object	$event	The event object
206
	 * @return	null
207
	 * @access	public
208
	 */
209
	public function generate_forum_nav($event)
210
	{
211
		$forum_data = $event['forum_data'];
212
		$navlinks = $event['navlinks'];
213
		$navlinks_parents = $event['navlinks_parents'];
214
215
		foreach ($navlinks_parents as $id => $data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
216
		{
217
			$navlinks_parents[$id]['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($data['FORUM_ID'] , $data['FORUM_NAME']));
218
		}
219
220
		$navlinks['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($forum_data['forum_id'], $forum_data['forum_name']));
221
		$event['navlinks'] = $navlinks;
222
		$event['navlinks_parents'] = $navlinks_parents;
223
	}
224
225
	// Not in phpBB
226
	public function make_jumpbox_modify_tpl_ary($event)
227
	{
228
		$tpl_ary = $event['tpl_ary'];
229
		$row = $event['row'];
230
		foreach ($tpl_ary as $id => $data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
231
		{
232
			$tpl_ary[$id]['LINK']	 = append_sid($this->base->generate_forum_link($row['forum_id'], $row['forum_name']));
233
		}
234
235
		$event['tpl_ary'] = $tpl_ary;
236
	}
237
238
	/**
239
	 * Rewrite links to most active forum and topic on profile page
240
	 *
241
	 * @param	object	$event	The event object
242
	 * @return	null
243
	 * @access	public
244
	 */
245
	public function memberlist_view_profile($event)
246
	{
247
		$data = $event['member'];
248
		$this->template->assign_vars(array(
249
			'U_ACTIVE_FORUM' => $this->base->generate_forum_link($data['active_f_row']['forum_id'], $data['active_f_row']['forum_name'], 0, true),
250
			'U_ACTIVE_TOPIC' => $this->base->generate_topic_link($data['active_f_row']['forum_id'], $data['active_f_row']['forum_name'], $data['active_t_row']['topic_id'], $data['active_t_row']['topic_title'], 0, true),
251
		));
252
	}
253
254
	/**
255
	 * Rewrite pagination links
256
	 *
257
	 * @param	object	$event	The event object
258
	 * @return	null
259
	 * @access	public
260
	 */
261
	public function pagination_generate_page_link($event)
262
	{
263
		if(!is_string($event['base_url']))
264
		{
265
			return;
266
		}
267
		// If we have a sort key we do not rewrite the URL
268
		$query = str_replace('&amp;', '&', parse_url($event['base_url'], PHP_URL_QUERY));
269
		parse_str($query, $param);
270
		if (isset($param['sd']) || isset($param['sk']) || isset($param['st']))
271
		{
272
			return;
273
		}
274
275
		$start = (($event['on_page'] - 1) * $event['per_page']);
276
		if (!empty($this->topic_data) && isset($param['f']) && isset($param['t']))
277
		{
278
			$event['generate_page_link_override'] = append_sid($this->base->generate_topic_link($this->topic_data['forum_id'], $this->topic_data['forum_name'], $this->topic_data['topic_id'], $this->topic_data['topic_title'], $start));
279
		}
280
		else if (!empty($this->forum_data) && isset($param['f']))
281
		{
282
			$event['generate_page_link_override'] = append_sid($this->base->generate_forum_link($this->forum_data['forum_id'], $this->forum_data['forum_name'], $start));
283
		}
284
	}
285
286
	/**
287
	 * Rewrite links in the search result
288
	 *
289
	 * @param	object	$event	The event object
290
	 * @return	null
291
	 * @access	public
292
	 */
293
	public function search_modify_tpl_ary($event)
294
	{
295
		$replies = $this->base->get_count('topic_posts', $event['row'], $event['row']['forum_id']) - 1;
296
		$u_view_topic = $this->base->generate_topic_link($event['row']['forum_id'], $event['row']['forum_name'], $event['row']['topic_id'], $event['row']['topic_title']);
297
298
		$tpl_ary = $event['tpl_ary'];
299
		$tpl_ary['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($replies, $u_view_topic) . '#p' . $event['row']['topic_last_post_id']);
300
		$tpl_ary['U_VIEW_TOPIC'] = append_sid($u_view_topic);
301
		$tpl_ary['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($event['row']['forum_id'], $event['row']['forum_name']));
302
		$tpl_ary['U_NEWEST_POST'] = $u_view_topic . '?view=unread#unread';
303
304
		$event['tpl_ary'] = $tpl_ary;
305
	}
306
307
	/**
308
	 * Rewrite links to topics in forum view
309
	 *
310
	 * @param	object	$event	The event object
311
	 * @return	null
312
	 * @access	public
313
	 */
314
	public function viewforum_modify_topicrow($event)
315
	{
316
		$topic_row = $event['topic_row'];
317
		$row = $event['row'];
318
319
		// assign to be used in pagination_generate_page_link
320
		$this->topic_data = array(
321
			'forum_id' => $row['forum_id'],
322
			'forum_name' => $topic_row['FORUM_NAME'],
323
			'topic_id' => $row['topic_id'],
324
			'topic_title' => $row['topic_title']
325
		);
326
327
		$u_view_topic = $this->base->generate_topic_link($row['forum_id'], $topic_row['FORUM_NAME'], $row['topic_id'], $row['topic_title']);
328
		$topic_row['U_VIEW_TOPIC'] = append_sid($u_view_topic);
329
		$topic_row['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($row['forum_id'], $topic_row['FORUM_NAME']));
330
		$topic_row['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($event['topic_row']['REPLIES'], $u_view_topic) . '#p' . $row['topic_last_post_id']);
331
		$topic_row['U_NEWEST_POST'] = $u_view_topic . '?view=unread#unread';
332
333
		$event['topic_row'] = $topic_row;
334
	}
335
336
	/**
337
	 * Get forum_name for shaddow topics
338
	 *
339
	 * @param	object	$event	The event object
340
	 * @return	null
341
	 * @access	public
342
	 */
343 View Code Duplication
	public function viewforum_get_shadowtopic_data($event)
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...
344
	{
345
		$sql_array = $event['sql_array'];
346
		$sql_array['SELECT'] .= ', f.forum_name';
347
		$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TABLE => 'f'), 'ON' => 'f.forum_id = t.forum_id');
348
		$event['sql_array'] = $sql_array;
349
	}
350
351
	/**
352
	 * Rewrite the canonical URL on viewforum.php
353
	 *
354
	 * @param	object	$event	The event object
355
	 * @return	null
356
	 * @access	public
357
	 */
358
	public function viewforum_get_topic_data($event)
359
	{
360
		// assign to be used in pagination_generate_page_link
361
		$this->forum_data = array(
362
			'forum_id' => $event['forum_data']['forum_id'],
363
			'forum_name' => $event['forum_data']['forum_name']
364
		);
365
366
		$start = $this->request->variable('start', 0);
367
		$this->template->assign_vars(array(
368
			'U_VIEW_FORUM'		=> append_sid($this->base->generate_forum_link($event['forum_data']['forum_id'], $event['forum_data']['forum_name'], $start)),
369
			'U_CANONICAL'		=> $this->base->generate_forum_link($event['forum_data']['forum_id'], $event['forum_data']['forum_name'], $start, true),
370
		));
371
	}
372
373
	/**
374
	 * Rewrite the topic URL for the headline of the topic page and the link back to forum
375
	 *
376
	 * @param	object	$event	The event object
377
	 * @return	null
378
	 * @access	public
379
	 */
380
	public function viewtopic_get_post_data($event)
381
	{
382
		$data = $event['topic_data'];
383
		$this->template->assign_vars(array(
384
			'U_VIEW_TOPIC'		=> append_sid($this->base->generate_topic_link($event['forum_id'] , $data['forum_name'], $event['topic_id'], $data['topic_title'], $event['start'])),
385
			'U_VIEW_FORUM'		=> append_sid($this->base->generate_forum_link($event['forum_id'] , $data['forum_name'])),
386
			'S_POLL_ACTION'		=> append_sid($this->base->generate_topic_link($event['forum_id'] , $data['forum_name'], $event['topic_id'], $data['topic_title'], $event['start'])),
387
		));
388
	}
389
390
	/**
391
	 * Assign topic data to global variables for pagination
392
	 *
393
	 * @param	object	$event	The event object
394
	 * @return	null
395
	 * @access	public
396
	 */
397
	public function viewtopic_assign_template_vars_before($event)
398
	{
399
		// assign to be used in pagination_generate_page_link
400
		$this->topic_data = array(
401
			'forum_id' => $event['topic_data']['forum_id'],
402
			'forum_name' => $event['topic_data']['forum_name'],
403
			'topic_id' => $event['topic_data']['topic_id'],
404
			'topic_title' => $event['topic_data']['topic_title']
405
		);
406
	}
407
408
	public function viewtopic_before_f_read_check()
409
	{
410
		$this->in_viewtopic = true;
411
	}
412
413
	/**
414
	 * Rewrite the canonical URL on viewtopic.php
415
	 *
416
	 * @param	object	$event	The event object
417
	 * @return	null
418
	 * @access	public
419
	 */
420
	public function viewtopic_modify_page_title($event)
421
	{
422
		$start = $this->request->variable('start', 0);
423
		$data = $event['topic_data'];
424
		$this->template->assign_vars(array(
425
			'U_CANONICAL'		=> $this->base->generate_topic_link($data['forum_id'], $data['forum_name'], $data['topic_id'], $data['topic_title'], $start, true),
426
		));
427
	}
428
429
	/**
430
	 * Rewrite mini post img link
431
	 *
432
	 * @param	object	$event	The event object
433
	 * @return	null
434
	 * @access	public
435
	 */
436
	public function viewtopic_modify_post_row($event)
437
	{
438
		$row = $event['post_row'];
439
		$start = $this->request->variable('start', 0);
440
		$data = $event['topic_data'];
441
		$row['U_MINI_POST'] = append_sid($this->base->generate_topic_link($data['forum_id'], $data['forum_name'], $data['topic_id'], $data['topic_title'], $start) . '#p' . $event['row']['post_id']);
442
		$event['post_row'] = $row;
443
	}
444
}
445