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

Search::searchItem()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 8
nop 1
dl 0
loc 52
rs 6.2553
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 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