Test Setup Failed
Push — master ( a87e27...976b69 )
by Ankit
02:24
created

Register   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 7.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 9
loc 115
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
F authRegister() 0 90 15
A onError() 0 5 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace ChatApp;
4
use ChatApp\Validate;
5
use ChatApp\Session;
6
require_once (dirname(__DIR__) . '/config/database.php');
7
8
class Register
9
{
10
	protected $error;
11
	protected $key;
12
	protected $obValidate;
13
	protected $connect;
14
	protected $session;
15
16 View Code Duplication
	public function __construct()
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...
17
	{
18
		$this->error = array();
19
		$this->key = 0;
20
		$this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
21
		$this->session = new Session();
22
		$this->obValidate = new Validate();
23
24
	}
25
26
	public function authRegister($name, $email, $username, $password, $mob)
27
	{
28
		$name = trim($name);
29
		$email = trim($email);
30
		$username = trim($username);
31
		$password = trim($password);
32
		$mob = trim($mob);
33
		$userId = '';
34
35
		if (empty($name)) {
36
			$this->onError(["name" => " *Enter the name"]);
37
		}
38
39
		if(empty($email)) {
40
			$this->onError(["email" => " *Enter the email address"]);
41
		}
42
		elseif(filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
43
			$this->onError(["email" => " *Enter correct Email address"]);
44
		}
45
		else
46
		{
47
			if($this->obValidate->validateEmailInDb($email) === 1)
48
			{
49
50
				$this->onError(["email" => " *Email is already registered"]);
51
			}
52
		}
53
54
		if(empty($username)) {
55
			$this->onError(["username" => " *Enter the username"]);
56
		}
57
		else
58
		{
59
			if($this->obValidate->validateUsernameInDb($username) === 1)
60
			{
61
62
				$this->onError(["username" => " *Username is already registered"]);
63
			}
64
		}
65
66
		if(empty($password)) {
67
			$this->onError(["password" => " *Enter the password"]);
68
		}
69
70
		if(empty($mob)) {
71
			$this->onError(["mob" => " *Enter the Mobile Number"]);
72
		}
73
		elseif (!preg_match("/^[0-9]{10}$/", $mob)) {
74
			$this->onError(["mob" => " *Enter correct Mobile Number"]);
75
		}
76
77
		if($this->key == 1)
78
		{
79
			return json_encode($this->error);
80
		}
81
		else
82
		{
83
			$this->key = 0;
84
			$pass = md5($password);
85
			$query = "INSERT INTO register VALUES(null, '$email', '$username', '$pass')";
86
			if(!$this->connect->query($query)) {
87
				$this->key = 1;
88
				echo "You are not registered || Error in registration2";
89
			}
90
			else
91
			{
92
				$query = "SELECT id FROM register WHERE email = '$email'";
93
				if($result = $this->connect->query($query)) {
94
					$row = $result->fetch_assoc();
95
					$userId = $row['id'];
96
97
					$query = "INSERT INTO login VALUES('$userId', '$name', '$email', '$username', '$mob', 0)";
98
					if(!$this->connect->query($query)) {
99
						$this->key = 1;
100
						echo "You are not registered || Error in registration1";
101
					}
102
				}
103
			}
104
		}
105
		if ($this->key == 0) {
106
			$this->session->put('start', $userId);
107
			return json_encode([
108
				"location" => URL."/account.php"
109
			]);
110
		}
111
		else
112
		{
113
			return json_encode($this->error);
114
		}
115
	}
116
117
	public function onError($value)
118
	{
119
		$this->key = 1;
120
		$this->error = array_merge($this->error, $value);
121
	}
122
}
123