Test Setup Failed
Push — master ( a87e27...976b69 )
by Ankit
02:24
created

Chat::onClose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace ChatApp;
3
use Ratchet\MessageComponentInterface;
4
use Ratchet\ConnectionInterface;
5
use ChatApp\Models\Message;
6
use ChatApp\Reply;
7
use ChatApp\Conversation;
8
use ChatApp\Receiver;
9
use ChatApp\SideBar;
10
use ChatApp\Search;
11
use ChatApp\Compose;
12
use ChatApp\Online;
13
14
class Chat implements MessageComponentInterface {
15
    protected $clients;
16
    protected $online;
17
18
    public function __construct() {
19
        $this->clients = new \SplObjectStorage;
20
        $this->result = '';
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $this->online = 0;
22
    }
23
24
    public function onOpen(ConnectionInterface $conn) {
25
        $conn = $this->setID($conn);
26
        $this->clients->attach($conn);
27
        echo "New connection! ({$conn->resourceId})\n";
28
        Online::setOnlineStatus($conn->userId);
29
    }
30
31
    public function setID($conn)
0 ignored issues
show
Coding Style introduced by
setID uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
32
    {
33
        var_dump($conn->WebSocket->request->getCookies()['PHPSESSID']);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($conn->WebSocke...ookies()['PHPSESSID']); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
34
        session_id($conn->WebSocket->request->getCookies()['PHPSESSID']);
35
        @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...
36
        $conn->userId = $_SESSION['start'];
37
        session_write_close();
38
        return $conn;
39
    }
40
41
    public function onMessage(ConnectionInterface $from, $msg) {
42
        $sessionId = $from->WebSocket->request->getCookies()['PHPSESSID'];
0 ignored issues
show
Bug introduced by
Accessing WebSocket on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
43
        if($msg == 'OpenChat initiated..!')
44
        {
45
            @$initial->initial = json_decode($this->onSidebar($from->userId));
0 ignored issues
show
Bug introduced by
The variable $initial does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
Accessing userId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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...
46
47
            @$initial->conversation = json_decode(
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...
48
                $this->onConversation(
49
                    json_encode([
50
                        "username" => $initial->initial[0]->login_id,
51
                        "load" => 10
52
                    ]), True, $sessionId
53
                )
54
            );
55
56
            @$initial->conversation[0]->login_status = $this->online;
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...
57
            $from->send(json_encode($initial));
58
        }
59
        elseif ($msg == 'Load Sidebar')
60
        {
61
            @$initial->initial = json_decode($this->onSidebar($from->userId));
0 ignored issues
show
Bug introduced by
Accessing userId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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...
62
            $from->send(json_encode($initial));
63
        }
64
        elseif (@json_decode($msg)->newConversation == 'Initiated')
65
        {
66
            @$result->conversation = json_decode($this->onConversation($msg, False, $sessionId));
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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...
67
            $from->send(json_encode($result));
68
        }
69
        elseif (@json_decode($msg)->search == 'search')
70
        {
71
            $searchResult = $this->onSearch($msg, $sessionId);
72
            $from->send($searchResult);
73
        }
74
        elseif (@json_decode($msg)->Compose == 'Compose')
75
        {
76
            $composeResult = $this->onCompose($msg, $sessionId);
77
            $from->send($composeResult);
78
        }
79
        else
80
        {
81
            $this->onReply($msg, $sessionId);
82
83
            $msg = json_decode($msg);
84
            // $msg->from = $from->userId;
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
85
86
            foreach ($this->clients as $client)
87
            {
88
                if ($client->userId == $msg->name)
89
                {
90
                    @$result->sidebar = json_decode($this->onSidebar($client->userId));
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...
91
92
                    @$result->conversation = json_decode(
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...
93
                        $this->onReceiver(
94
                            json_encode([
95
                                "username" => $client->userId,
96
                                "load" => 10
97
                            ]), True, $sessionId
98
                        )
99
                    );
100
101
                    $client->send(json_encode($result));
102
                    $this->online = 1;
103
                }
104
                elseif($client == $from)
105
                {
106
                    @$result->sidebar = json_decode($this->onSidebar($client->userId));
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...
107
108
                    @$result->conversation = json_decode(
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...
109
                        $this->onConversation(
110
                            json_encode([
111
                                "username" => $msg->name,
112
                                "load" => 10
113
                            ]), True, $sessionId
114
                        )
115
                    );
116
117
                    $result->conversation[0]->login_status = $this->online;
118
                    $client->send(json_encode($result));
119
                    $this->online = 0;
120
                }
121
            }
122
123
        }
124
    }
125
126
    public function onSidebar($data)
127
    {
128
        $obSidebar = new Sidebar();
129
        return $obSidebar->LoadSideBar($data);
130
    }
131
132
    public function onConversation($data, $para, $sessionId)
133
    {
134
        $obConversation = new Conversation($sessionId);
135
        return $obConversation->ConversationLoad($data, $para);
136
    }
137
138
    public function onReceiver($data, $para, $sessionId)
0 ignored issues
show
Unused Code introduced by
The parameter $para is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        $obReceiver = new Receiver($sessionId);
141
        return $obReceiver->ReceiverLoad($data, True);
0 ignored issues
show
Unused Code introduced by
The call to Receiver::ReceiverLoad() has too many arguments starting with True.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
142
    }
143
144
    public function onSearch($data, $sessionId)
145
    {
146
        $obSearch = new Search($sessionId);
147
        return $obSearch->SearchItem(json_decode($data));
148
    }
149
150
    public function onCompose($data, $sessionId)
151
    {
152
        $obCompose = new Compose($sessionId);
153
        return $obCompose->SelectUser(json_decode($data));
154
    }
155
156
    public function onReply($data, $sessionId)
157
    {
158
        $obReply = new Reply($sessionId);
159
        $obReply->replyTo($data);
160
    }
161
162
    public function onClose(ConnectionInterface $conn) {
163
        Online::removeOnlineStatus($conn->userId);
0 ignored issues
show
Bug introduced by
Accessing userId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
164
        $this->clients->detach($conn);
165
        echo "Connection {$conn->resourceId} has disconnected\n";
0 ignored issues
show
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
166
    }
167
    public function onError(ConnectionInterface $conn, \Exception $e) {
168
        echo "An error has occurred: {$e->getMessage()}\n";
169
        $conn->close();
170
    }
171
172
173
}