SideBar   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 88
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
B loadSideBar() 0 24 6
A data() 0 14 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
 * Sidebar 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 mysqli;
18
use Dotenv\Dotenv;
19
$dotenv = new Dotenv(dirname(__DIR__));
20
$dotenv->load();
21
22
/**
23
 * Fetching the sidebar results
24
 *
25
 * @category PHP
26
 * @package  OpenChat
27
 * @author   Ankit Jain <[email protected]>
28
 * @license  The MIT License (MIT)
29
 * @link     https://github.com/ankitjain28may/openchat
30
 */
31
class SideBar
32
{
33
    /*
34
    |--------------------------------------------------------------------------
35
    | Time Class
36
    |--------------------------------------------------------------------------
37
    |
38
    | For Fetching the sidebar results.
39
    |
40
    */
41
42
    protected $obTime;
43
    protected $array;
44
    protected $connect;
45
46
    /**
47
     * Create a new class instance.
48
     *
49
     * @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...
50
     */
51 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...
52
    {
53
        $this->connect = new mysqli(
54
            getenv('DB_HOST'),
55
            getenv('DB_USER'),
56
            getenv('DB_PASSWORD'),
57
            getenv('DB_NAME')
58
        );
59
        $this->obTime = new Time();
60
        $this->array = array();
61
    }
62
63
    /**
64
     * Load Sidebar for the user containing all the names of the people
65
     *
66
     * @param int $userId To store session id of the user
67
     *
68
     * @return string
69
     */
70
    public function loadSideBar($userId)
71
    {
72
        if (!empty($userId)) {
73
            $query = "SELECT * FROM total_message WHERE user1 = '$userId'
74
                        or user2 = '$userId'  ORDER BY id DESC";
75
            if ($result = $this->connect->query($query)) {
76
                if ($result->num_rows > 0) {
77
                    while ($row = $result->fetch_assoc()) {
78
                        $identifier = $row['identifier'];
79
                        $substring = explode(":", $identifier);
80
                        if ($substring[0] != $userId) {
81
                            $this->data($substring[0], $row);
82
                        } else {
83
                            $this->data($substring[1], $row);
84
                        }
85
                    }
86
                    return json_encode($this->array);
87
                }
88
                return json_encode(null);
89
            }
90
            return "Query Failed";
91
        }
92
        return "Invalid Authentication";
93
    }
94
95
    /**
96
     * Fetch data form the DB for the Sidebar
97
     *
98
     * @param int   $userId To store session id of the user
99
     * @param array $row    To store data
100
     *
101
     * @return void
102
     */
103
    public function data($userId, $row)
104
    {
105
        $query = "SELECT username, name, login_status, login_id
106
                    from login where login_id = '$userId'";
107
        if ($result = $this->connect->query($query)) {
108
            if ($result->num_rows > 0) {
109
                $fetch = $result->fetch_assoc();
110
                $fetch['login_id'] = bin2hex(convert_uuencode($fetch['login_id']));
111
                $row['time'] = $this->obTime->timeConversion($row['time']);
112
                $fetch = array_merge($fetch, ['time' => $row['time']]);
113
                $this->array = array_merge($this->array, [$fetch]);
114
            }
115
        }
116
    }
117
118
}
119