Conversation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 9.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 12
loc 127
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
C conversationLoad() 0 84 12

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
 * Conversation 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\Time;
17
use ChatApp\User;
18
use mysqli;
19
use Dotenv\Dotenv;
20
$dotenv = new Dotenv(dirname(__DIR__));
21
$dotenv->load();
22
23
/**
24
 * To Return the Conversation Data between users
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 Conversation
33
{
34
    /*
35
    |--------------------------------------------------------------------------
36
    | Conversation Class
37
    |--------------------------------------------------------------------------
38
    |
39
    | To Return the Conversation Data between users.
40
    |
41
    */
42
43
    protected $connect;
44
    protected $array;
45
    protected $obTime;
46
    protected $obUser;
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 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...
54
    {
55
        $this->connect = new mysqli(
56
            getenv('DB_HOST'),
57
            getenv('DB_USER'),
58
            getenv('DB_PASSWORD'),
59
            getenv('DB_NAME')
60
        );
61
        $this->obTime = new Time();
62
        $this->obUser = new User();
63
        $this->array = array();
64
    }
65
66
    /**
67
     * Fetch data from DB and show to user.
68
     *
69
     * @param string  $msg  To store message
70
     * @param boolean $para To store True/False
71
     *
72
     * @return string
73
     */
74
    public function conversationLoad($msg, $para)
75
    {
76
        $msg = json_decode($msg);
77
        if (!empty($msg)) {
78
            $userId = $msg->userId;
79
            $add_load = 0;
80
            $details = $msg->details;
81
            $load = $msg->load;
82
83
            if ($para == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
84
                $details = convert_uudecode(hex2bin($details));
85
            }
86
            $fetch = $this->obUser->userDetails($details, $para);
0 ignored issues
show
Documentation introduced by
$para is of type boolean, but the function expects a object<ChatApp\boollen>.

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...
87
88
            if ($fetch != null) {
89
                $login_id = (int)$fetch['login_id'];
90
91
                // Unique Identifier
92
                if ($login_id > $userId) {
93
                    $identifier = $userId.':'.$login_id;
94
                } else {
95
                    $identifier = $login_id.':'.$userId;
96
                }
97
98
                $query = "SELECT total_messages from total_message
99
                            where identifier = '$identifier'";
100
                if ($result = $this->connect->query($query)) {
101
                    if ($result->num_rows > 0) {
102
                        $total = $result->fetch_assoc();
103
                        $total = $total['total_messages'];
104
                        if ($total - $load > 0) {
105
                            if ($total - $load > 10) {
106
                                $add_load = $load + 10;
107
                            } else {
108
                                $add_load = $total;
109
                            }
110
                        }
111
                    }
112
                }
113
114
                $query = "SELECT message, time, sent_by FROM messages WHERE
115
                            identifier_message_number = '$identifier'
116
                            ORDER BY id DESC limit ".$load;
117
118
                if ($result = $this->connect->query($query)) {
119
                    if ($result->num_rows > 0) {
120
                        while ($row = $result->fetch_assoc()) {
121
                            $row['time'] = $this->obTime->timeConversion(
122
                                $row['time']
123
                            );
124
                            $row = array_merge($row, ['start' => $userId]);
125
                            $this->array = array_merge($this->array, [$row]);
126
                        }
127
128
                        $this->array = array_merge(
129
                            [[
130
                            'name' => $fetch['name'],
131
                            'username' => $fetch['username'],
132
                            'id' => bin2hex(convert_uuencode($fetch['login_id'])),
133
                            'load' => $add_load,
134
                            'login_status' => $fetch['login_status'],
135
                            'type' => 1
136
                            ]],
137
                            $this->array
138
                        );
139
                        return json_encode($this->array);
140
                    } else {
141
                        return json_encode(
142
                            [[
143
                            'name' => $fetch['name'],
144
                            'username' => $fetch['username'],
145
                            'id' => bin2hex(convert_uuencode($fetch['login_id'])),
146
                            'login_status' => $fetch['login_status'],
147
                            'type' => 0
148
                            ]]
149
                        );
150
                    }
151
                }
152
                return "Query Failed";
153
            }
154
            return "Query Failed";
155
        }
156
        return "Empty";
157
    }
158
}
159