Completed
Push — master ( 9edb5e...d40969 )
by Ankit
03:51
created

Reply   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 4.35 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 4
loc 92
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B replyTo() 4 61 8
A updateMessages() 0 13 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
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 14 and the first side effect is on line 4.

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
require_once (dirname(__DIR__) . '/vendor/autoload.php');
5
use ChatApp\Session;
6
use Dotenv\Dotenv;
7
$dotenv = new Dotenv(dirname(__DIR__));
8
$dotenv->load();
9
10
11
/**
12
* Store Message in the Database
13
*/
14
class Reply
15
{
16
    protected $connect;
17
18
    public function __construct()
19
    {
20
        $this->connect = mysqli_connect(
21
            getenv('DB_HOST'),
22
            getenv('DB_USER'),
23
            getenv('DB_PASSWORD'),
24
            getenv('DB_NAME')
25
        );
26
    }
27
28
    public function replyTo($msg)
29
    {
30
        if(!empty($msg))  //checks for the value send
31
        {
32
            $userId = $msg->userId;
33
            $identifier = $msg->name;
34
            $receiverID = $identifier;  //stores id of the person whom message is to be sent
35
36 View Code Duplication
            if($identifier > $userId)    // geneate specific unique code to store messages
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...
37
                $identifier = $userId . ":" . $identifier;
38
            else
39
                $identifier = $identifier . ":" . $userId;
40
41
            $reply = addslashes(trim($msg->reply[0])); // stores the message sent by the user.
42
43
            $time = date("D d M Y H:i:s", time() + 16200);  // current time
44
            $time_id = date("YmdHis", time() + 16200); //to sort the array on the basis of time
45
46
            //the sender id must not be equal to current session id
47
            if($reply != "" && $receiverID != $userId)
48
            {
49
                // check whether the receiver is authorized or registered
50
                $query = "SELECT * from login where login_id = '$receiverID'";
51
52
                $result = $this->connect->query($query);
53
                if($result->num_rows > 0)     // if true
54
                {
55
                    //check whether he is sending message for thr first time or he has sent messages before
56
                    $query = "SELECT * from total_message where identifier = '$identifier'";
57
                    $result = $this->connect->query($query);
58
                    if($result->num_rows>0)               // if he has sent messages before
59
                    {
60
                        // Update Total_Message Table
61
                        $query = "UPDATE total_message SET total_messages = total_messages+1, time = '$time', unread = 1, id = '$time_id' WHERE identifier = '$identifier'";
62
                        return $this->updateMessages($query, $identifier, $reply, $userId, $time);
63
64
                    }
65
                    else    // if he sends message for the first time
66
                    {
67
                        $length = strlen($userId);
68
                        if(substr($identifier, 0, $length) == $userId) // generate specific unique code
69
                        {
70
                            $user2 = substr($identifier, $length+1);
71
                            $user1 = $userId;
72
                        }
73
                        else
74
                        {
75
                            $user2 = $userId;
76
                            $length = strlen($identifier) - $length-1;
77
                            $user1 = substr($identifier, 0, $length);
78
                        }
79
                        // insert Details in Total_Message Table
80
                        $query = "INSERT into total_message values('$identifier', 1, '$user1', '$user2', 1, '$time', '$time_id')";
81
                        return $this->updateMessages($query, $identifier, $reply, $userId, $time);
82
                    }
83
                }
84
                return "Invalid Authentication";  // if he is unauthorized echo message is failed
85
            }
86
        }
87
        return "Failed";
88
    }
89
90
    public function updateMessages($query, $identifier, $reply, $userId, $time)
91
    {
92
        if($result = $this->connect->query($query))
0 ignored issues
show
Unused Code introduced by
$result 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...
93
        {
94
            //insert message in db
95
            $query = "INSERT into messages values('$identifier', '$reply', '$userId', '$time', null)";
96
            if($this->connect->query($query))
97
            {
98
                return "Messages is sent";    // if query is executed return true
99
            }
100
            return "Message is failed";
101
        }
102
    }
103
104
105
}
106
107