Test Failed
Pull Request — master (#30)
by
unknown
02:21
created

Chat::setID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/**
3
 * Chat 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
use Ratchet\MessageComponentInterface;
16
use Ratchet\ConnectionInterface;
17
// use ChatApp\Models\Message;
18
use ChatApp\Reply;
19
use ChatApp\Conversation;
20
use ChatApp\Receiver;
21
use ChatApp\SideBar;
22
use ChatApp\Search;
23
use ChatApp\Compose;
24
use ChatApp\Online;
25
26
/**
27
 * This Class handles the all the main functionalities for this ChatApp.
28
 *
29
 * @category PHP
30
 * @package  OpenChat
31
 * @author   Ankit Jain <[email protected]>
32
 * @license  The MIT License (MIT)
33
 * @link     https://github.com/ankitjain28may/openchat
34
 */
35
36
class Chat implements MessageComponentInterface
37
{
38
    /*
39
    |--------------------------------------------------------------------------
40
    | Chat Class
41
    |--------------------------------------------------------------------------
42
    |
43
    | This Class handles the all the main functionalities for this ChatApp.
44
    |
45
    */
46
47
    protected $clients;
48
49
    /**
50
     * Create a new class instance.
51
     *
52
     * @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...
53
     */
54
    public function __construct()
55
    {
56
        $this->clients = new \SplObjectStorage;
57
    }
58
59
    /**
60
     * Open the Socket Connection and get client connection
61
     *
62
     * @param ConnectionInterface $conn To store client details
63
     *
64
     * @return void
65
     */
66
    public function onOpen(ConnectionInterface $conn)
67
    {
68
        $conn = $this->setID($conn);
69
        $this->clients->attach($conn);
70
        echo "New connection! ({$conn->resourceId})\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...
71
        Online::setOnlineStatus($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...
72
    }
73
74
    /**
75
     * Set Session Id in Connection object
76
     *
77
     * @param ConnectionInterface $conn To store client details
78
     *
79
     * @return $conn
0 ignored issues
show
Documentation introduced by
The doc-type $conn could not be parsed: Unknown type name "$conn" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81
    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...
82
    {
83
        session_id($conn->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...
84
        @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...
85
        $conn->userId = $_SESSION['start'];
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...
86
        session_write_close();
87
        return $conn;
88
    }
89
90
    /**
91
     * Send Messages to Clients
92
     *
93
     * @param ConnectionInterface $from To store client details
94
     * @param string              $msg  To store message
95
     *
96
     * @return void
97
     */
98
    public function onMessage(ConnectionInterface $from, $msg)
99
    {
100
        $msg = (object)json_decode($msg);
101
        if ($msg->type == 'OpenChat initiated..!') {
102
            $initial = (object)array();
103
            $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...
104
105
            if ($initial->initial != null) {
106
                $initial->conversation = json_decode(
107
                    $this->onConversation(
108
                        json_encode(
109
                            [
110
                            "details" => $initial->initial[0]->login_id,
111
                            "load" => 20,
112
                            "userId" => $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...
113
                            ]
114
                        ), true
115
                    )
116
                );
117
            }
118
            $from->send(json_encode($initial));
119
        } else if ($msg->type == 'Load Sidebar') {
120
            $sidebar = (object)array();
121
            $sidebar->sidebar = 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...
122
            $from->send(json_encode($sidebar));
123
        } else if ($msg->type == 'Initiated') {
124
            $msg->userId = $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...
125
            $result = (object)array();
126
            $result->conversation = json_decode(
127
                $this->onConversation(json_encode($msg), false)
128
            );
129
            $from->send(json_encode($result));
130
        } else if ($msg->type == 'Search') {
131
            $msg->userId = $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...
132
            $searchResult = $this->onSearch($msg);
133
            $from->send($searchResult);
134
        } else if ($msg->type == 'Compose') {
135
            $msg->userId = $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...
136
            $composeResult = $this->onCompose($msg);
137
            $from->send($composeResult);
138
        } else if ($msg->type == 'typing') {
139
            $msg->userId = $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...
140
            $msg->name = convert_uudecode(hex2bin($msg->name));
141
            foreach ($this->clients as $client) {
142
                if ($client->userId == $msg->name) {
143
                    $client->send(json_encode(array('typing' => 'typing')));
144
                }
145
            }
146
        } else {
147
            $msg->userId = $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...
148
            $msg->name = convert_uudecode(hex2bin($msg->name));
149
150
            $getReturn = $this->onReply($msg);
151
            echo $getReturn;
152
153
            $receiveResult = (object)array();
154
            $sentResult = (object)array();
155
            foreach ($this->clients as $client) {
156
                if ($client->userId == $msg->name) {
157
                    $receiveResult->sidebar = json_decode(
158
                        $this->onSidebar($client->userId)
159
                    );
160
161
                    $receiveResult->reply = json_decode(
162
                        $this->onReceiver(
163
                            json_encode(
164
                                [
165
                                "details" => $client->userId,
166
                                "load" => 20,
167
                                "userId" => $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...
168
                                ]
169
                            ), true
170
                        )
171
                    );
172
173
                    $client->send(json_encode($receiveResult));
174
                } else if ($client == $from) {
175
                    $sentResult->sidebar = json_decode(
176
                        $this->onSidebar($client->userId)
177
                    );
178
179
                    $sentResult->conversation = json_decode(
180
                        $this->onConversation(
181
                            json_encode(
182
                                [
183
                                "details" => bin2hex(convert_uuencode($msg->name)),
184
                                "load" => 20,
185
                                "userId" => $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...
186
                                ]
187
                            ), true
188
                        )
189
                    );
190
                    $client->send(json_encode($sentResult));
191
                }
192
            }
193
194
        }
195
    }
196
197
    /**
198
     * To Call SideBar Class
199
     *
200
     * @param string $data To store data
201
     *
202
     * @return string
203
     */
204
    public function onSidebar($data)
205
    {
206
        $obSidebar = new SideBar();
207
        return $obSidebar->loadSideBar($data);
208
    }
209
210
    /**
211
     * To Call Conversation Class
212
     *
213
     * @param string  $data to store data
214
     * @param boolean $para to store True/False
215
     *
216
     * @return string
217
     */
218
    public function onConversation($data, $para)
219
    {
220
        $obConversation = new Conversation();
221
        return $obConversation->conversationLoad($data, $para);
222
    }
223
224
    /**
225
     * To Call Receiver Class
226
     *
227
     * @param string  $data to store data
228
     * @param boolean $para to store True/False
229
     *
230
     * @return string
231
     */
232
    public function onReceiver($data, $para)
233
    {
234
        $obReceiver = new Receiver();
235
        return $obReceiver->receiverLoad($data, $para);
236
    }
237
238
    /**
239
     * To Call Search Class
240
     *
241
     * @param string $data to store data
242
     *
243
     * @return string
244
     */
245
    public function onSearch($data)
246
    {
247
        $obSearch = new Search();
248
        return $obSearch->searchItem($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
    }
250
251
    /**
252
     * To Call Compose Class
253
     *
254
     * @param string $data to store data
255
     *
256
     * @return string
257
     */
258
    public function onCompose($data)
259
    {
260
        $obCompose = new Compose();
261
        return $obCompose->selectUser($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
262
    }
263
264
    /**
265
     * To Call Reply Class
266
     *
267
     * @param string $data to store data
268
     *
269
     * @return string
270
     */
271
    public function onReply($data)
272
    {
273
        $obReply = new Reply();
274
        return $obReply->replyTo($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
275
    }
276
277
    /**
278
     * To Call Online Class
279
     *
280
     * @param ConnectionInterface $conn To store client details
281
     *
282
     * @return void
283
     */
284
    public function onClose(ConnectionInterface $conn)
285
    {
286
        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...
287
        $this->clients->detach($conn);
288
        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...
289
    }
290
291
    /**
292
     * To Show error due to any problem occured
293
     *
294
     * @param ConnectionInterface $conn To store client details
295
     * @param \Exception          $e    To store exception
296
     *
297
     * @return void
298
     */
299
    public function onError(ConnectionInterface $conn, \Exception $e)
300
    {
301
        echo "An error has occurred: {$e->getMessage()}\n";
302
        $conn->close();
303
    }
304
305
}
306