Completed
Push — master ( 5d5455...35e057 )
by Ankit
02:15
created

Register::authRegister()   C

Complexity

Conditions 9
Paths 60

Size

Total Lines 58
Code Lines 32

Duplication

Lines 8
Ratio 13.79 %

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 60
nop 1
dl 8
loc 58
rs 6.9928
c 0
b 0
f 0

How to fix   Long Method   

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