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

Connection::processHandshake()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 15
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 12
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\Http\Request;
18
use Nekland\Woketo\Http\Response;
19
use Nekland\Woketo\Rfc6455\Handshake\ClientHandShake;
20
use Nekland\Woketo\Rfc6455\MessageProcessor;
21
use React\Promise\PromiseInterface;
22
use React\Stream\Stream;
23
24
class Connection extends AbstractConnection
25
{
26
    /**
27
     * @var Stream
28
     */
29
    private $stream;
30
31
    /**
32
     * @var bool
33
     */
34
    private $requestSent;
35
36
    /**
37
     * @var string
38
     */
39
    private $host;
40
41
    /**
42
     * @var string
43
     */
44
    private $buffer;
45
46
    public function __construct(string $uri, string $host, PromiseInterface $clientPromise, MessageProcessor $messageProcessor)
47
    {
48
        parent::__construct($messageProcessor, new ClientHandShake());
49
50
        $this->requestSent = false;
51
        $this->uri = $uri;
52
        $this->host = $host;
53
        $this->buffer = '';
54
55
        $clientPromise->then(function (Stream $stream) {
56
            $this->stream = $stream;
57
            $this->onConnection($stream);
58
        }, function (\Exception $error){
59
            $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...
60
        });
61
        $this->processHandshake('');
62
    }
63
64
    private function onConnection(Stream $stream)
65
    {
66
        $stream->on('message', function (string $data) {
67
            $this->onMessage($data);
68
        });
69
    }
70
71
    protected function processHandshake(string $data)
72
    {
73
        // Sending initialization request
74
        if (!$this->requestSent) {
75
            $request = Request::createClientRequest($this->uri, $this->host);
76
            $this->stream->write($request->getRequestAsString());
77
            return;
78
        }
79
80
        $this->buffer .= $data;
81
82
        try {
83
            $response = Response::create($data);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
        } catch (IncompleteHttpMessageException $e) {
85
            return;
86
        }
87
88
89
        // Receiving switching protocol response
90
91
92
        // Verifying response
93
94
95
        $this->handshakeDone = true;
96
    }
97
98
    protected function processMessage(string $data)
99
    {
100
        // TODO: Implement processMessage() method.
101
    }
102
103
    /**
104
     * @param \Exception|string $error
105
     */
106
    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...
107
    {
108
109
    }
110
}
111