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

Search   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 18.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 1
C searchItem() 0 52 10

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
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 14 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\Time;
6
use ChatApp\Session;
7
use Dotenv\Dotenv;
8
$dotenv = new Dotenv(dirname(__DIR__));
9
$dotenv->load();
10
11
/**
12
* Search for the user
13
*/
14
class Search
15
{
16
17
    protected $connect;
18
    protected $array;
19
    protected $obTime;
20
21 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...
22
    {
23
        session_id($sessionId);
24
        @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...
25
        $this->connect = mysqli_connect(
26
            getenv('DB_HOST'),
27
            getenv('DB_USER'),
28
            getenv('DB_PASSWORD'),
29
            getenv('DB_NAME')
30
        );
31
        session_write_close();
32
        $this->obTime = new Time();
33
        $this->array = array();
34
    }
35
36
    public function searchItem($suggestion)
37
    {
38
        $suggestion = $suggestion->value;
39
        $flag = 0;
40
        $userId = Session::get('start');
41
        if($userId != null && isset($suggestion))
42
        {
43
            $suggestion = trim($suggestion);
44
            if($suggestion != "")
45
            {
46
                $query = "SELECT * FROM login where login_id != '$userId' and name like '$suggestion%' ORDER BY name ASC";
47
                if($result = $this->connect->query($query))
48
                {
49
                    if($result->num_rows > 0)
50
                    {
51
                        while($row = $result->fetch_assoc())
52
                        {
53
                            $check_id = $row["login_id"];
54
                            $query = "SELECT * from total_message where (user1 = '$check_id' and user2 = '$userId') or (user2 = '$check_id' and user1 = '$userId')";
55
                            if($result1 = $this->connect->query($query))
56
                            {
57
                                if($result1->num_rows > 0)
58
                                {
59
                                    $fetch = $result1->fetch_assoc();
60
                                    $fetch['time'] = $this->obTime->timeConversion($fetch['time']);
61
62
                                    $this->array = array_merge($this->array, [['time' => $fetch['time'], 'username' => $row['username'], 'name' => $row['name']]]);
63
                                    $flag = 1;
64
                                }
65
                            }
66
                        }
67
                    }
68
                }
69
                if($flag != 0)
70
                {
71
                    $this->array = array_merge([], ["Search" => $this->array]);
72
                    return json_encode($this->array);
73
                }
74
                else
75
                    return json_encode(["Search" => "Not Found"]);
76
            }
77
            else
78
            {
79
                return json_encode(["Search" => "Not Found"]);
80
            }
81
        }
82
        else
83
        {
84
            return json_encode(["Search" => "Not Found"]);
85
        }
86
87
    }
88
}
89