Compose   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 17.19 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
B selectUser() 0 25 6

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
 * Compose 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 mysqli;
17
use Dotenv\Dotenv;
18
$dotenv = new Dotenv(dirname(__DIR__));
19
$dotenv->load();
20
21
/**
22
 * To Compose New Message
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 Compose
31
{
32
    /*
33
    |--------------------------------------------------------------------------
34
    | Compose Class
35
    |--------------------------------------------------------------------------
36
    |
37
    | To Compose New Message.
38
    |
39
    */
40
41
    protected $connect;
42
    protected $array;
43
44
    /**
45
     * Create a new class instance.
46
     *
47
     * @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...
48
     */
49 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...
50
    {
51
        $this->connect = new mysqli(
52
            getenv('DB_HOST'),
53
            getenv('DB_USER'),
54
            getenv('DB_PASSWORD'),
55
            getenv('DB_NAME')
56
        );
57
58
        $this->array = array();
59
    }
60
61
    /**
62
     * Fetch User from the DB
63
     *
64
     * @param object $msg To store user id and suggestion value
65
     *
66
     * @return string
67
     */
68
    public function selectUser($msg)
69
    {
70
        $userId = $msg->userId;
71
        $suggestion = $msg->value;
72
        $suggestion = trim($suggestion);
73
        if (!empty($userId) && !empty($suggestion)) {
74
            $query = "SELECT * FROM login where login_id != '$userId' and
75
                        name like '$suggestion%' ORDER BY name DESC";
76
            if ($result = $this->connect->query($query)) {
77
                if ($result->num_rows > 0) {
78
                    while ($row = $result->fetch_assoc()) {
79
                        $row["login_id"] = bin2hex(
80
                            convert_uuencode($row["login_id"])
81
                        );
82
                        $this->array = array_merge($this->array, [$row]);
83
                    }
84
                    $this->array = array_merge([], ["Compose" => $this->array]);
85
                    return json_encode($this->array);
86
                }
87
                return json_encode(["Compose" => "Not Found"]);
88
            }
89
            return json_encode(["Compose" => "Query Failed"]);
90
        }
91
        return json_encode(["Compose" => "Not Found"]);
92
    }
93
}
94
95