Reply::replyTo()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 9
nop 1
dl 0
loc 69
rs 7.743
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
2
/**
3
 * Reply 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
require_once dirname(__DIR__).'/vendor/autoload.php';
15
use mysqli;
16
use Dotenv\Dotenv;
17
$dotenv = new Dotenv(dirname(__DIR__));
18
$dotenv->load();
19
20
21
/**
22
 * Store Message in the Database
23
 *
24
 * @category PHP
25
 * @package  OpenChat
26
 * @author   Ankit Jain <[email protected]>
27
 * @license  The MIT License (MIT)
28
 * @link     https://github.com/ankitjain28may/openchat
29
 */
30
class Reply
31
{
32
    /*
33
    |--------------------------------------------------------------------------
34
    | Reply Class
35
    |--------------------------------------------------------------------------
36
    |
37
    | Store Message in the Database
38
    |
39
    */
40
41
    protected $connect;
42
43
    /**
44
     * Create a new class instance.
45
     *
46
     * @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...
47
     */
48 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...
49
    {
50
        $this->connect = new mysqli(
51
            getenv('DB_HOST'),
52
            getenv('DB_USER'),
53
            getenv('DB_PASSWORD'),
54
            getenv('DB_NAME')
55
        );
56
57
        date_default_timezone_set('Asia/Kolkata');
58
    }
59
60
    /**
61
     * Store Message in Db so as to send message to other members
62
     *
63
     * @param object $msg To store user id and massage
64
     *
65
     * @return string
66
     */
67
    public function replyTo($msg)
68
    {
69
        if (!empty($msg)) {
70
            // checks for the value send
71
            $userId = $msg->userId;
72
            // stores id of the person whom message is to be sent
73
            $receiverID = $msg->name;
74
            $identifier;
0 ignored issues
show
Bug introduced by
The variable $identifier 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...
75
76
            if ($receiverID > $userId) {
77
                // geneate specific unique code to store messages
78
                $user1 = $userId;
79
                $user2 = $receiverID;
80
                $identifier = $userId.":".$receiverID;
81
            } else {
82
                $user1 = $receiverID;
83
                $user2 = $userId;
84
                $identifier = $receiverID.":".$userId;
85
            }
86
87
            // stores the message sent by the user.
88
            $reply = addslashes(trim($msg->reply));
89
            // current time
90
            $time = date("D d M Y H:i:s");
91
            // echo $time;
92
            // to sort the array on the basis of time
93
            $time_id = date("YmdHis");
94
95
            // the sender id must not be equal to current session id
96
            if ($reply != "" && $receiverID != $userId) {
97
                // check whether the receiver is authorized or registered
98
                $query = "SELECT * from login where login_id = '$receiverID'";
99
100
                $result = $this->connect->query($query);
101
                if ($result->num_rows > 0) {
102
                    // check whether he is sending message
103
                    // for the first time or he has sent messages before
104
                    $query = "SELECT * from total_message where
105
                                identifier = '$identifier'";
106
                    $result = $this->connect->query($query);
107
                    if ($result->num_rows > 0) {
108
                        // if he has sent messages before Update Total_Message Table
109
                        $query = "UPDATE total_message SET
110
                                total_messages = total_messages + 1,
111
                                time = '$time', unread = 1,
112
                                id = '$time_id' WHERE identifier = '$identifier'";
113
114
                        return $this->updateMessages(
115
                            $query, $identifier, $reply, $userId, $time
116
                        );
117
118
                    } else {
119
                        // if he sends message for the first time
120
                        // insert Details in Total_Message Table
121
                        $query = "INSERT into total_message values(
122
                            '$identifier', 1, '$user1', '$user2', 1,
123
                            '$time', '$time_id'
124
                        )";
125
                        return $this->updateMessages(
126
                            $query, $identifier, $reply, $userId, $time
127
                        );
128
                    }
129
                }
130
                // if he is unauthorized echo message is failed
131
                return "Invalid Authentication";
132
            }
133
        }
134
        return "Failed";
135
    }
136
137
    /**
138
     * To Store Message in DB Based on Identifier
139
     *
140
     * @param string $query      To store the query performed
141
     * @param string $identifier To store unique id
142
     * @param string $reply      To store message
143
     * @param string $userId     To store userid
144
     * @param string $time       To store time
145
     *
146
     * @return string
147
     */
148
    public function updateMessages($query, $identifier, $reply, $userId, $time)
149
    {
150
        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...
151
            //insert message in db
152
            $query = "INSERT into messages values(
153
                '$identifier', '$reply', '$userId', '$time', null
154
            )";
155
            if ($this->connect->query($query)) {
156
                // if query is executed return true
157
                return "Messages is sent\n";
158
            }
159
            return "Message is failed\n";
160
        }
161
    }
162
163
164
}
165
166