Completed
Pull Request — master (#132)
by Matt
01:25
created

idea_controller   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 265
Duplicated Lines 24.15 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 2
dl 64
loc 265
rs 9.1199
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B idea() 0 35 7
A delete() 0 37 3
A duplicate() 10 10 3
A removevote() 12 18 5
A rfc() 10 10 4
A ticket() 10 10 4
A implemented() 10 10 3
A status() 0 12 4
A vote() 12 20 5
A get_hash() 0 4 1
A is_mod() 0 4 1
A is_own() 0 4 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like idea_controller 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 idea_controller, and based on these observations, apply Extract Interface, too.

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\controller;
12
13
use phpbb\exception\http_exception;
14
use phpbb\ideas\ext;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
18
class idea_controller extends base
19
{
20
	/** @var array of idea data */
21
	protected $data;
22
23
	/**
24
	 * Controller for /idea/{idea_id}
25
	 *
26
	 * @param $idea_id int The ID of the requested idea, maybe?
27
	 * @throws http_exception
28
	 * @return JsonResponse|RedirectResponse
29
	 */
30
	public function idea($idea_id)
31
	{
32
		if (!$this->is_available())
33
		{
34
			throw new http_exception(404, 'IDEAS_NOT_AVAILABLE');
35
		}
36
37
		$this->data = $this->ideas->get_idea($idea_id);
0 ignored issues
show
Bug introduced by
The method get_idea does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
38
		if (!$this->data)
39
		{
40
			throw new http_exception(404, 'IDEA_NOT_FOUND');
41
		}
42
43
		$mode = $this->request->variable('mode', '');
44
		if (!empty($mode) && $this->request->is_ajax())
45
		{
46
			$result = $this->$mode();
47
48
			return new JsonResponse($result);
49
		}
50
51
		$params = array(
52
			'f' => $this->config['ideas_forum_id'],
53
			't' => $this->data['topic_id']
54
		);
55
56
		if ($unread = ($this->request->variable('view', '') === 'unread'))
57
		{
58
			$params = array_merge($params, array('view' => 'unread'));
59
		}
60
61
		$url = append_sid(generate_board_url() . "/viewtopic.{$this->php_ext}", $params, false) . ($unread ? '#unread' : '');
62
63
		return new RedirectResponse($url);
64
	}
65
66
	/**
67
	 * Delete action (deletes an idea via confirm dialog)
68
	 *
69
	 * @throws http_exception
70
	 * @return void
71
	 * @access public
72
	 */
73
	public function delete()
74
	{
75
		if (!$this->is_mod())
76
		{
77
			throw new http_exception(403, 'NO_AUTH_OPERATION');
78
		}
79
80
		if (confirm_box(true))
81
		{
82
			include $this->root_path . 'includes/functions_admin.' . $this->php_ext;
83
			$this->ideas->delete($this->data['idea_id'], $this->data['topic_id']);
0 ignored issues
show
Bug introduced by
The method delete does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
85
			$redirect = $this->helper->route('phpbb_ideas_index_controller');
86
			$message = $this->language->lang('IDEA_DELETED') . '<br /><br />' . $this->language->lang('RETURN_IDEAS', '<a href="' . $redirect . '">', '</a>');
87
			meta_refresh(3, $redirect);
88
			trigger_error($message); // trigger error needed for data-ajax
89
		}
90
		else
91
		{
92
			confirm_box(
93
				false,
94
				$this->language->lang('CONFIRM_DELETE'),
95
				build_hidden_fields(array(
96
					'idea_id' => $this->data['idea_id'],
97
					'mode' => 'delete',
98
				)),
99
				'confirm_body.html',
100
				$this->helper->route(
101
					'phpbb_ideas_idea_controller',
102
					array(
103
						'idea_id' => $this->data['idea_id'],
104
						'mode'    => 'delete',
105
					)
106
				)
107
			);
108
		}
109
	}
110
111
	/**
112
	 * Duplicate action (sets an idea's duplicate link)
113
	 *
114
	 * @return bool True if set, false if not
115
	 * @access public
116
	 */
117 View Code Duplication
	public function 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...
118
	{
119
		if ($this->is_mod() && check_link_hash($this->get_hash(), "duplicate_{$this->data['idea_id']}"))
120
		{
121
			$duplicate = $this->request->variable('duplicate', 0);
122
			return $this->ideas->set_duplicate($this->data['idea_id'], $duplicate);
0 ignored issues
show
Bug introduced by
The method set_duplicate does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
		}
124
125
		return false;
126
	}
127
128
	/**
129
	 * Remove vote action (remove a user's vote from an idea)
130
	 *
131
	 * @return mixed Array of vote data, an error message, or false if failed
132
	 * @access public
133
	 */
134
	public function removevote()
135
	{
136 View Code Duplication
		if ($this->data['idea_status'] === ext::$statuses['IMPLEMENTED'] || $this->data['idea_status'] === ext::$statuses['DUPLICATE'] || !check_link_hash($this->get_hash(), "removevote_{$this->data['idea_id']}"))
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...
137
		{
138
			return false;
139
		}
140
141 View Code Duplication
		if ($this->auth->acl_get('f_vote', (int) $this->config['ideas_forum_id']))
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...
142
		{
143
			$result = $this->ideas->remove_vote($this->data, $this->user->data['user_id']);
0 ignored issues
show
Bug introduced by
The method remove_vote does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
144
		}
145
		else
146
		{
147
			$result = $this->language->lang('NO_AUTH_OPERATION');
148
		}
149
150
		return $result;
151
	}
152
153
	/**
154
	 * RFC action (sets an idea's rfc link)
155
	 *
156
	 * @return bool True if set, false if not
157
	 * @access public
158
	 */
159 View Code Duplication
	public function 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...
160
	{
161
		if (($this->is_own() || $this->is_mod()) && check_link_hash($this->get_hash(), "rfc_{$this->data['idea_id']}"))
162
		{
163
			$rfc = $this->request->variable('rfc', '');
164
			return $this->ideas->set_rfc($this->data['idea_id'], $rfc);
0 ignored issues
show
Bug introduced by
The method set_rfc does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
165
		}
166
167
		return false;
168
	}
169
170
	/**
171
	 * Status action (sets an idea's status)
172
	 *
173
	 * @return bool True if set, false if not
174
	 * @access public
175
	 */
176
	public function status()
177
	{
178
		$status = $this->request->variable('status', 0);
179
180
		if ($status && $this->is_mod() && check_link_hash($this->get_hash(), "status_{$this->data['idea_id']}"))
181
		{
182
			$this->ideas->set_status($this->data['idea_id'], $status);
0 ignored issues
show
Bug introduced by
The method set_status does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
183
			return true;
184
		}
185
186
		return false;
187
	}
188
189
	/**
190
	 * Ticket action (sets an idea's ticket link)
191
	 *
192
	 * @return bool True if set, false if not
193
	 * @access public
194
	 */
195 View Code Duplication
	public function 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...
196
	{
197
		if (($this->is_own() || $this->is_mod()) && check_link_hash($this->get_hash(), "ticket_{$this->data['idea_id']}"))
198
		{
199
			$ticket = $this->request->variable('ticket', 0);
200
			return $this->ideas->set_ticket($this->data['idea_id'], $ticket);
0 ignored issues
show
Bug introduced by
The method set_ticket does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
201
		}
202
203
		return false;
204
	}
205
206
	/**
207
	 * Implemented action (sets an idea's implemented phpBB version)
208
	 *
209
	 * @return bool True if set, false if not
210
	 * @access public
211
	 */
212 View Code Duplication
	public function implemented()
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...
213
	{
214
		if ($this->is_mod() && check_link_hash($this->get_hash(), "implemented_{$this->data['idea_id']}"))
215
		{
216
			$version = $this->request->variable('implemented', '');
217
			return $this->ideas->set_implemented($this->data['idea_id'], $version);
0 ignored issues
show
Bug introduced by
The method set_implemented does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
218
		}
219
220
		return false;
221
	}
222
223
	/**
224
	 * Vote action (sets an idea's vote)
225
	 *
226
	 * @return mixed Array of vote data, an error message, or false if failed
227
	 * @access public
228
	 */
229
	public function vote()
230
	{
231
		$vote = $this->request->variable('v', 1);
232
233 View Code Duplication
		if ($this->data['idea_status'] === ext::$statuses['IMPLEMENTED'] || $this->data['idea_status'] === ext::$statuses['DUPLICATE'] || !check_link_hash($this->get_hash(), "vote_{$this->data['idea_id']}"))
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...
234
		{
235
			return false;
236
		}
237
238 View Code Duplication
		if ($this->auth->acl_get('f_vote', (int) $this->config['ideas_forum_id']))
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...
239
		{
240
			$result = $this->ideas->vote($this->data, $this->user->data['user_id'], $vote);
0 ignored issues
show
Bug introduced by
The method vote does only exist in phpbb\ideas\factory\idea, but not in phpbb\ideas\factory\ideas.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
241
		}
242
		else
243
		{
244
			$result = $this->language->lang('NO_AUTH_OPERATION');
245
		}
246
247
		return $result;
248
	}
249
250
	/**
251
	 * Get a hash query parameter
252
	 *
253
	 * @return string The hash
254
	 * @access protected
255
	 */
256
	protected function get_hash()
257
	{
258
		return $this->request->variable('hash', '');
259
	}
260
261
	/**
262
	 * Does the user have moderator privileges?
263
	 *
264
	 * @return bool
265
	 * @access protected
266
	 */
267
	protected function is_mod()
268
	{
269
		return $this->auth->acl_get('m_', (int) $this->config['ideas_forum_id']);
270
	}
271
272
	/**
273
	 * Is the user the author of the idea?
274
	 *
275
	 * @return bool
276
	 * @access protected
277
	 */
278
	protected function is_own()
279
	{
280
		return $this->data['idea_author'] === $this->user->data['user_id'];
281
	}
282
}
283