Completed
Push — master ( 3bdd73...3b96a7 )
by Ankit
02:28
created

Conversation::conversationLoad()   C

Complexity

Conditions 12
Paths 42

Size

Total Lines 76
Code Lines 43

Duplication

Lines 4
Ratio 5.26 %

Importance

Changes 0
Metric Value
cc 12
eloc 43
nc 42
nop 2
dl 4
loc 76
rs 5.2846
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 16 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 ChatApp\Time;
7
use ChatApp\User;
8
use Dotenv\Dotenv;
9
$dotenv = new Dotenv(dirname(__DIR__));
10
$dotenv->load();
11
12
13
/**
14
*
15
*/
16
class Conversation
17
{
18
    protected $connect;
19
    protected $array;
20
    protected $obTime;
21
    protected $obUser;
22
23 View Code Duplication
    public function __construct($sessionId)
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...
24
    {
25
        session_id($sessionId);
26
        @session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
27
        $this->connect = mysqli_connect(
28
            getenv('DB_HOST'),
29
            getenv('DB_USER'),
30
            getenv('DB_PASSWORD'),
31
            getenv('DB_NAME')
32
        );
33
        session_write_close();
34
        $this->obTime = new Time();
35
        $this->obUser = new User();
36
        $this->array = array();
37
    }
38
39
    public function conversationLoad($msg, $para)
40
    {
41
42
        $flag = 1;
0 ignored issues
show
Unused Code introduced by
$flag 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...
43
        if(Session::get('start') != null && isset($msg))
44
        {
45
            $add_load = 0;
46
            $userId = Session::get('start');
47
            $msg = json_decode($msg);
48
            $username = $msg->username;
49
            $load = $msg->load;
50
51
            $fetch = $this->obUser->userDetails($username, $para);
52
53
            if($fetch != NULL)
54
            {
55
                $login_id = (int)$fetch['login_id'];
56
57
                // Unique Identifier
58 View Code Duplication
                if($login_id > $userId)
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...
59
                    $identifier = $userId.':'.$login_id;
60
                else
61
                    $identifier = $login_id.':'.$userId;
62
63
                $query = "SELECT total_messages from total_message where identifier = '$identifier'";
64
                if($result = $this->connect->query($query))
65
                {
66
                    if($result->num_rows > 0)
67
                    {
68
                        $total = $result->fetch_assoc();
69
                        $total = $total['total_messages'];
70
                        if($total - $load > 0)
71
                            if($total - $load > 10)
72
                                $add_load = $load + 10;
73
                            else
74
                                $add_load = $total;
75
                    }
76
                }
77
78
                $query = "SELECT message, time, sent_by FROM messages WHERE identifier_message_number = '$identifier' ORDER BY id DESC limit ".$load;
79
                if($result = $this->connect->query($query))
80
                {
81
                    if($result->num_rows > 0)
82
                    {
83
                        while($row = $result->fetch_assoc())
84
                        {
85
                            $row['time'] = $this->obTime->timeConversion($row['time']);
86
                            $row = array_merge($row,['start' => $userId]);
87
                            $this->array = array_merge($this->array, [$row]);
88
                        }
89
90
                        $this->array = array_merge([['name' => $fetch['name'], 'username' => $fetch['username'], 'id' => $fetch['login_id'], 'load' => $add_load, 'login_status' => $fetch['login_status'], 'type' => 1]], $this->array);
91
                        return json_encode($this->array);
92
                    }
93
                    else
94
                    {
95
                        return json_encode([['name' => $fetch['name'], 'username' => $fetch['username'], 'id' => $fetch['login_id'], 'login_status' => $fetch['login_status'], 'type' => 0]]);
96
                    }
97
                }
98
                else
99
                {
100
                    echo "Query Failed";
101
                }
102
            }
103
            else
104
            {
105
                echo "Query Failed";
106
            }
107
        }
108
        else
109
        {
110
            header('Location:../login.php');
111
        }
112
113
        $this->connect->close();
114
    }
115
}
116