search_listener   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 138
Duplicated Lines 7.25 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 1
dl 10
loc 138
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getSubscribedEvents() 0 24 1
A search_assign_topic_attributes() 10 10 2
A search_add_url_parameter() 0 7 2
A search_by_author_modify_search_key() 0 7 2
A search_author_query_before() 0 7 2
A search_keywords_main_query_before() 0 19 3
A search_modify_keyword_search() 0 19 4
A search_alter_show_result() 0 11 2
A search_select_attributes() 0 4 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 Quick Title Edition Extension
5
 * @copyright (c) 2015 ABDev
6
 * @copyright (c) 2015 PastisD
7
 * @copyright (c) 2015 Geolim4 <http://geolim4.com>
8
 * @copyright (c) 2015 Zoddo <[email protected]>
9
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
10
 *
11
 */
12
13
namespace ernadoo\qte\event;
14
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
class search_listener implements EventSubscriberInterface
18
{
19
	/** @var \phpbb\request\request */
20
	protected $request;
21
22
	/** @var \ernadoo\qte\qte */
23
	protected $qte;
24
25
	/** @var bool */
26
	private $searc_attr = false;
27
28
	/** @var int */
29
	private $searc_attr_id;
30
31
	public function __construct(\phpbb\request\request $request, \ernadoo\qte\qte $qte, \ernadoo\qte\search\fulltext_attribute $qte_search)
32
	{
33
		$this->request		= $request;
34
		$this->qte			= $qte;
35
		$this->qte_search	= $qte_search;
36
	}
37
38
	static public function getSubscribedEvents()
39
	{
40
		return array(
41
			// search
42
			'core.search_modify_forum_select_list'			=> 'search_select_attributes',
43
			'core.search_modify_submit_parameters'			=> 'search_alter_show_result',
44
			'core.search_modify_url_parameters'				=> 'search_add_url_parameter',
45
			'core.search_modify_tpl_ary'					=> 'search_assign_topic_attributes',
46
47
			'core.search_backend_search_after'				=> 'search_modify_keyword_search',
48
49
			'core.search_mysql_author_query_before'				=> 'search_author_query_before',
50
			'core.search_mysql_by_author_modify_search_key'		=> 'search_by_author_modify_search_key',
51
			'core.search_mysql_keywords_main_query_before'		=> 'search_keywords_main_query_before',
52
53
			'core.search_native_author_count_query_before'		=> 'search_author_query_before',
54
			'core.search_native_by_author_modify_search_key'	=> 'search_by_author_modify_search_key',
55
			'core.search_native_keywords_count_query_before'	=> 'search_keywords_main_query_before',
56
57
			'core.search_postgres_author_count_query_before'	=> 'search_by_author_modify_search_key',
58
			'core.search_postgres_by_author_modify_search_key'	=> 'search_by_author_modify_search_key',
59
			'core.search_postgres_keywords_main_query_before'	=> 'search_keywords_main_query_before',
60
		);
61
	}
62
63 View Code Duplication
	public function search_assign_topic_attributes($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...
64
	{
65
		if (!empty($event['row']['topic_attr_id']))
66
		{
67
			$this->qte->get_users_by_topic_id((array) $event['row']['topic_id']);
68
			$tpl_ary = $event['tpl_ary'];
69
			$tpl_ary['TOPIC_ATTRIBUTE'] = $this->qte->attr_display($event['row']['topic_attr_id'], $event['row']['topic_attr_user'], $event['row']['topic_attr_time']);
70
			$event['tpl_ary'] = $tpl_ary;
71
		}
72
	}
73
74
	public function search_add_url_parameter($event)
75
	{
76
		if ($this->searc_attr)
77
		{
78
			$event['u_search'] .= '&amp;attr_id=' . $this->searc_attr_id;
79
		}
80
	}
81
82
	public function search_by_author_modify_search_key($event)
83
	{
84
		if ($this->searc_attr)
85
		{
86
			$event['firstpost_only'] = true;
87
		}
88
	}
89
90
	public function search_author_query_before($event)
91
	{
92
		if ($this->searc_attr)
93
		{
94
			$event['sql_author'] .= ' AND t.topic_attr_id = ' . $this->searc_attr_id;
95
		}
96
	}
97
98
	public function search_keywords_main_query_before($event)
99
	{
100
		if ($this->searc_attr)
101
		{
102
			// Fulltext_native
103
			if (isset($event['sql_where']))
104
			{
105
				$event['left_join_topics'] = true;
106
				$sql_where = $event['sql_where'];
107
				$sql_where[] = 't.topic_attr_id = ' . (int) $this->searc_attr_id;
108
				$event['sql_where'] = $sql_where;
109
			}
110
			else
111
			{
112
				$event['join_topic'] = true;
113
				$event['sql_match_where'] = ' AND t.topic_attr_id = ' . (int) $this->searc_attr_id;
114
			}
115
		}
116
	}
117
118
	public function search_modify_keyword_search($event)
119
	{
120
		if ($this->searc_attr)
121
		{
122
			$keywords	= utf8_normalize_nfc($this->request->variable('keywords', '', true));
123
			$author		= $this->request->variable('author', '', true);
124
125
			if (!$keywords && !$author)
126
			{
127
				$id_ary = $event['id_ary'];
128
				$start = $event['start'];
129
				$total_match_count = $event['total_match_count'];
0 ignored issues
show
Unused Code introduced by
$total_match_count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
				$total_match_count = $this->qte_search->attribute_search($this->searc_attr_id, $event['show_results'], $event['search_terms'], $event['sort_by_sql'], $event['sort_key'], $event['sort_dir'], $event['sort_days'], $event['ex_fid_ary'], $event['m_approve_posts_fid_sql'], $event['topic_id'], $event['author_id_ary'], $event['sql_author_match'], $id_ary, $start, $event['per_page']);
131
				$event['total_match_count'] = $total_match_count;
132
				$event['start'] = $start;
133
				$event['id_ary'] = $id_ary;
134
			}
135
		}
136
	}
137
138
	public function search_alter_show_result($event)
139
	{
140
		$topic_attribute = $this->request->variable('attr_id', 0, false, \phpbb\request\request_interface::GET);
141
142
		if ($topic_attribute)
143
		{
144
			$this->searc_attr = true;
145
			$this->searc_attr_id = $topic_attribute;
146
			$event['submit'] = true;
147
		}
148
	}
149
150
	public function search_select_attributes($event)
151
	{
152
		$this->qte->attr_search();
153
	}
154
}
155