Register   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 163
Duplicated Lines 17.18 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 28
loc 163
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
C authRegister() 12 75 10
A onError() 8 8 1
A emptyValue() 8 19 3

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
 * Register Class Doc Comment
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  OpenChat
9
 * @author   Ankit Jain <[email protected]>
10
 * @license  The MIT License (MIT)
11
 * @link     https://github.com/ankitjain28may/openchat
12
 */
13
namespace ChatApp;
14
15
require_once dirname(__DIR__).'/vendor/autoload.php';
16
use ChatApp\Validate;
17
use ChatApp\Session;
18
use Dotenv\Dotenv;
19
use mysqli;
20
$dotenv = new Dotenv(dirname(__DIR__));
21
$dotenv->load();
22
23
/**
24
 * Register the User
25
 *
26
 * @category PHP
27
 * @package  OpenChat
28
 * @author   Ankit Jain <[email protected]>
29
 * @license  The MIT License (MIT)
30
 * @link     https://github.com/ankitjain28may/openchat
31
 */
32
class Register
33
{
34
    /*
35
    |--------------------------------------------------------------------------
36
    | Register Class
37
    |--------------------------------------------------------------------------
38
    |
39
    | Save User Details in DB.
40
    |
41
    */
42
43
    protected $error;
44
    protected $flag;
45
    protected $obValidate;
46
    protected $connect;
47
48
    /**
49
     * Create a new class instance.
50
     *
51
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
52
     */
53
    public function __construct()
54
    {
55
        $this->error = array();
56
        $this->flag = 0;
57
        $this->connect = new mysqli(
58
            getenv('DB_HOST'),
59
            getenv('DB_USER'),
60
            getenv('DB_PASSWORD'),
61
            getenv('DB_NAME')
62
        );
63
        $this->obValidate = new Validate();
64
65
    }
66
67
    /**
68
     * Store Message in Db so as to send message to other members
69
     *
70
     * @param object $data To store user details
71
     *
72
     * @return string
73
     */
74
    public function authRegister($data)
75
    {
76
        $data = $this->emptyValue($data);
0 ignored issues
show
Documentation introduced by
$data is of type object, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        $name = $data["name"];
78
        $email = $data["email"];
79
        $username = $data["username"];
80
        $mob = $data["mob"];
81
        $password = $data["passRegister"];
82
        $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...
83
84
        if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
85
            $this->onError("email", " *Enter correct Email address");
86
        } else if ($this->obValidate->validateEmailInDb($email) === 1) {
87
            $this->onError("email", " *Email is already registered");
88
        }
89
90
        if ($this->obValidate->validateUsernameInDb($username) === 1) {
91
            $this->onError("username", " *Username is already registered");
92
        }
93
94
        if (!preg_match("/^[0-9]{10}$/", $data["mob"])) {
95
            $this->onError("mob", " *Enter correct Mobile Number");
96
        }
97
98
        if ($this->flag == 1) {
99
            return json_encode($this->error);
100
        }
101
102
        $password = md5($password);
103
104
        $query = "INSERT INTO register VALUES(
105
            null, '$email', '$username', '$password'
106
        )";
107 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...
108
            return json_encode(
109
                [
110
                "Error" => "You are not registered, ".$this->connect->error
111
                ]
112
            );
113
        }
114
        $query = "SELECT id FROM register WHERE email = '$email'";
115
        if ($result = $this->connect->query($query)) {
116
            $row = $result->fetch_assoc();
117
            $userId = $row['id'];
118
            $query = "INSERT INTO login VALUES(
119
                '$userId', '$name', '$email', '$username', '$mob', 0
120
            )";
121
122 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...
123
                return json_encode(
124
                    [
125
                    "Error" => "You are not registered, ".$this->connect->error
126
                    ]
127
                );
128
            }
129
130
            $query = "INSERT INTO profile VALUES(
131
                '$userId', 'Joined OpenChat', 'Joined OpenChat', ''
132
            )";
133 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...
134
                return json_encode(
135
                    [
136
                    "Error" => "You are not registered, ".$this->connect->error
137
                    ]
138
                );
139
            }
140
141
            Session::put('start', $userId);
142
            return json_encode(
143
                [
144
                "location" => getenv('APP_URL')."/views/account.php"
145
                ]
146
            );
147
        }
148
    }
149
150
151
    /**
152
     * For Adding Error statements
153
     *
154
     * @param string $key   To store Key
155
     * @param string $value To store Key
156
     *
157
     * @return void
158
     */
159 View Code Duplication
    public function onError($key, $value)
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
        $this->flag = 1;
162
        $this->error = array_merge(
163
            $this->error,
164
            [["key" => $key, "value" => $value]]
165
        );
166
    }
167
168
    /**
169
     * For Traversing data to check for error
170
     *
171
     * @param array $data To store Data
172
     *
173
     * @return array
174
     */
175
    public function emptyValue($data)
176
    {
177
        $errorCode = array(
178
            "name" => " *Enter the name",
179
            "email" => " *Enter the email address",
180
            "username" => " *Enter the username",
181
            "passRegister" => " *Enter the password",
182
            "mob" => " *Enter the Mobile Number"
183
        );
184
185 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...
186
            $data[$key] = trim($data[$key]);
187
            $value = trim($value);
188
            if (empty($value)) {
189
                $this->onError($key, $errorCode[$key]);
190
            }
191
        }
192
        return $data;
193
    }
194
}
195