Completed
Push — master ( b5a957...040c1d )
by Paul
03:38
created

main_controller::createError()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 56
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 56
rs 6.7093
cc 12
eloc 26
nc 12
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
*
4
* 2FA extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2015 Paul Sohier
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace paul999\tfa\controller;
12
13
use paul999\tfa\helper\session_helper_interface;
14
use paul999\tfa\modules\module_interface;
15
use phpbb\config\config;
16
use phpbb\controller\helper;
17
use phpbb\db\driver\driver_interface;
18
use phpbb\request\request_interface;
19
use phpbb\template\template;
20
use phpbb\user;
21
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
22
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
23
24
/**
25
 * Controller
26
 */
27
class main_controller
28
{
29
	/**
30
	 * @var helper
31
	 */
32
	private $controller_helper;
33
34
	/**
35
	 * @var template
36
	 */
37
	private $template;
38
39
	/**
40
	 * @var driver_interface
41
	 */
42
	private $db;
43
44
	/**
45
	 * @var user
46
	 */
47
	private $user;
48
49
	/**
50
	 * @var request_interface
51
	 */
52
	private $request;
53
54
	/**
55
	 * @var config
56
	 */
57
	private $config;
58
59
	/**
60
	 * @var session_helper_interface
61
	 */
62
	private $session_helper;
63
64
	/**
65
	 * @var string
66
	 */
67
	private $root_path;
68
69
	/**
70
	 * @var string
71
	 */
72
	private $php_ext;
73
74
	/**
75
	 * Constructor
76
	 *
77
	 * @access public
78
	 * @param helper $controller_helper
79
	 * @param driver_interface $db
80
	 * @param template $template
81
	 * @param user $user
82
	 * @param request_interface $request
83
	 * @param config $config
84
	 * @param session_helper_interface $session_helper
85
	 * @param string $root_path
86
	 * @param string $php_ext
87
	 */
88 View Code Duplication
	public function __construct(helper $controller_helper, driver_interface $db, template $template, user $user, request_interface $request, config $config, session_helper_interface $session_helper, $root_path, $php_ext)
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...
89
	{
90
		$this->controller_helper 	= $controller_helper;
91
		$this->template 			= $template;
92
		$this->db					= $db;
93
		$this->user					= $user;
94
		$this->request				= $request;
95
		$this->config				= $config;
96
		$this->session_helper		= $session_helper;
97
		$this->root_path			= $root_path;
98
		$this->php_ext				= $php_ext;
99
100
	}
101
102
	/**
103
	 * @param int $user_id
104
	 * @param bool $admin
105
	 * @param bool $auto_login
106
	 * @param bool $viewonline
107
	 * @param string $class
108
	 * @return \Symfony\Component\HttpFoundation\Response
109
	 */
110
	public function display($user_id, $admin, $auto_login, $viewonline, $class)
111
	{
112
		$this->user->add_lang_ext('paul999/tfa', 'common');
113
114
		if ($this->config['tfa_mode'] == session_helper_interface::MODE_DISABLED)
115
		{
116
			throw new AccessDeniedHttpException('TFA_DISABLED');
117
		}
118
		if (($this->user->data['user_id'] != ANONYMOUS && !$admin) || $user_id == ANONYMOUS || ($user_id != $this->user->data['user_id'] && $admin))
119
		{
120
			throw new AccessDeniedHttpException('TFA_NO_ACCESS');
121
		}
122
		$modules = $this->session_helper->getModules();
123
124
		/**
125
		 * @var module_interface $module
126
		 */
127
		if (!empty($default))
0 ignored issues
show
Bug introduced by
The variable $default seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
128
		{
129
			$module = $this->session_helper->findModule($class);
130
		}
131
		else
132
		{
133
			foreach ($modules as $row)
134
			{
135
				if ($module->is_usable($user_id))
0 ignored issues
show
Bug introduced by
The variable $module seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
136
				{
137
					$module = $row;
138
				}
139
				break;
140
			}
141
		}
142
		if ($module == null)
0 ignored issues
show
Bug introduced by
The variable $module does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
143
		{
144
			throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG'));
145
		}
146
147
		/**
148
		 * @var module_interface $row
149
		 */
150
		foreach ($modules as $row)
151
		{
152
			if ($row->is_usable($user_id))
153
			{
154
				$this->template->assign_block_vars('', array(
155
					'U_CHANGE_CLASS'	=> $this->controller_helper->route('paul999_tfa_read_controller', array(
156
						'user_id'		=> $user_id,
157
						'admin'			=> $admin,
158
						'auto_login'	=> $auto_login,
159
						'viewonline'	=> $viewonline,
160
						'class'			=> get_class($row),
161
					)),
162
				));
163
			}
164
		}
165
		$module->login_start($user_id);
166
167
168
		$this->template->assign_vars(array(
169
			'REDIRECT'		=> $this->request->variable('redirect', ''),
170
			'U_SUBMIT_AUTH'	=> $this->controller_helper->route('paul999_tfa_read_controller_submit', array(
171
				'user_id'		=> $user_id,
172
				'admin'			=> $admin,
173
				'auto_login'	=> $auto_login,
174
				'viewonline'	=> $viewonline,
175
				'class'			=> get_class($module),
176
			)),
177
		));
178
179
		return $this->controller_helper->render('@paul999_tfa/authenticate_main.html');
180
	}
181
182
	/**
183
	 * @param int $user_id
184
	 * @param bool $admin
185
	 * @param bool $auto_login
186
	 * @param bool $viewonline
187
	 * @return \Symfony\Component\HttpFoundation\Response
188
	 * @throws AccessDeniedHttpException
189
	 */
190
	public function submit($user_id, $admin, $auto_login, $viewonline, $class)
191
	{
192
		$this->user->add_lang_ext('paul999/tfa', 'common');
193
194
		if (empty($class))
195
		{
196
			throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG'));
197
		}
198
199
		$module = $this->session_helper->findModule($class);
200
201
		if ($module == null)
202
		{
203
			throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG'));
204
		}
205
		$module->login($user_id);
206
207
		$old_session_id = $this->user->session_id;
208
209
		if ($admin)
210
		{
211
			$cookie_expire = time() - 31536000;
212
			$this->user->set_cookie('u', '', $cookie_expire);
213
			$this->user->set_cookie('sid', '', $cookie_expire);
214
		}
215
216
		$result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline);
217
218
		// Successful session creation
219
		if ($result === true)
220
		{
221
			// If admin re-authentication we remove the old session entry because a new one has been created...
222
			if ($admin)
223
			{
224
				// the login array is used because the user ids do not differ for re-authentication
225
				$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
226
						WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "'
227
						AND session_user_id = {$user_id}";
228
				$this->db->sql_query($sql);
229
230
				redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id']));
231
			}
232
			$redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}");
233
			redirect(append_sid($redirect, false, true, $this->user->data['session_id']));
234
		}
235
		throw new BadRequestHttpException($this->user->lang('TFA_SOMETHING_WENT_WRONG'));
236
	}
237
}
238