Completed
Pull Request — master (#100)
by Maxime
02:23
created

Connection::onError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is a part of Woketo package.
5
 *
6
 * (c) Nekland <[email protected]>
7
 *
8
 * For the full license, take a look to the LICENSE file
9
 * on the root directory of this project
10
 */
11
12
namespace Nekland\Woketo\Client;
13
14
15
use Nekland\Woketo\Core\AbstractConnection;
16
use Nekland\Woketo\Exception\Http\IncompleteHttpMessageException;
17
use Nekland\Woketo\Exception\WebsocketException;
18
use Nekland\Woketo\Http\Request;
19
use Nekland\Woketo\Http\Response;
20
use Nekland\Woketo\Rfc6455\Handshake\ClientHandShake;
21
use Nekland\Woketo\Rfc6455\MessageProcessor;
22
use React\Promise\PromiseInterface;
23
use React\Stream\Stream;
24
25
class Connection extends AbstractConnection
26
{
27
    /**
28
     * @var Stream
29
     */
30
    private $stream;
31
32
    /**
33
     * @var bool
34
     */
35
    private $requestSent;
36
37
    /**
38
     * @var string
39
     */
40
    private $host;
41
42
    /**
43
     * @var string
44
     */
45
    private $buffer;
46
47
    public function __construct(string $uri, string $host, PromiseInterface $clientPromise, MessageProcessor $messageProcessor)
48
    {
49
        parent::__construct($messageProcessor, new ClientHandShake());
50
51
        $this->requestSent = false;
52
        $this->uri = $uri;
53
        $this->host = $host;
54
        $this->buffer = '';
55
56
        $clientPromise->then(function (Stream $stream) {
57
            $this->stream = $stream;
58
            $this->onConnection($stream);
59
        }, function (\Exception $error){
60
            $this->onError($error);
0 ignored issues
show
Unused Code introduced by
The call to the method Nekland\Woketo\Client\Connection::onError() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
61
        });
62
        $this->processHandshake('');
63
    }
64
65
    private function onConnection(Stream $stream)
66
    {
67
        $stream->on('message', function (string $data) {
68
            $this->onMessage($data);
69
        });
70
    }
71
72
    protected function processHandshake(string $data)
73
    {
74
        // Sending initialization request
75
        if (!$this->requestSent) {
76
            $request = Request::createClientRequest($this->uri, $this->host);
77
            $this->stream->write($request->getRequestAsString());
78
            return;
79
        }
80
81
        $this->buffer .= $data;
82
83
        try {
84
            $response = Response::create($data);
85
        } catch (IncompleteHttpMessageException $e) {
86
            return;
87
        }
88
89
        $this->handshake->verify($response);
0 ignored issues
show
Documentation introduced by
$response is of type object<Nekland\Woketo\Http\Response>, but the function expects a object<Nekland\Woketo\Http\Request>.

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...
90
91
92
        // Receiving switching protocol response
93
94
95
        // Verifying response
96
97
98
        $this->handshakeDone = true;
99
    }
100
101
    protected function processMessage(string $data)
102
    {
103
        // TODO: Implement processMessage() method.
104
    }
105
106
    /**
107
     * @param \Exception|string $error
108
     */
109
    private function onError($error)
0 ignored issues
show
Unused Code introduced by
The parameter $error 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...
110
    {
111
112
    }
113
}
114