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

AbstractConnection::processHandshake()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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\Core;
13
14
use Nekland\Woketo\Exception\WebsocketException;
15
use Nekland\Woketo\Message\MessageHandlerInterface;
16
use Nekland\Woketo\Rfc6455\Handshake\HandshakeInterface;
17
use Nekland\Woketo\Rfc6455\MessageProcessor;
18
use Psr\Log\LoggerAwareTrait;
19
use React\Socket\Connection;
20
21
abstract class AbstractConnection
22
{
23
    use LoggerAwareTrait;
24
25
    /**
26
     * @var Connection
27
     */
28
    protected $stream;
29
30
    /**
31
     * @var MessageProcessor
32
     */
33
    protected $messageProcessor;
34
35
    /**
36
     * @var HandshakeInterface
37
     */
38
    protected $handshake;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $handshakeDone;
44
45
    /**
46
     * @var string
47
     */
48
    protected $uri;
49
50
    /**
51
     * @var MessageHandlerInterface|\Closure
52
     */
53
    protected $handler;
54
55 5
    public function __construct(MessageProcessor $messageProcessor, HandshakeInterface $handshake = null)
56
    {
57 5
        $this->handshake = $handshake;
58 5
        $this->messageProcessor = $messageProcessor;
59 5
    }
60
61
    protected function onMessage(string $data)
62
    {
63
        try {
64
            if (!$this->handshakeDone) {
65
                $this->processHandshake($data);
66
            } else {
67
                $this->processMessage($data);
68
            }
69
70
            return;
71
        } catch (WebsocketException $e) {
72
            $this->messageProcessor->close($this->stream);
73
            $this->logger->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage());
74
            $this->handler->onError($e, $this);
0 ignored issues
show
Bug introduced by
The method onError does only exist in Nekland\Woketo\Message\MessageHandlerInterface, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
        }
76
    }
77
78
    protected abstract function processHandshake(string $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
79
    protected abstract function processMessage(string $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
80
81
    /**
82
     * @return string
83
     */
84
    public function getIp()
85
    {
86
        return $this->stream->getRemoteAddress();
87
    }
88
89
    /**
90
     * @return \Psr\Log\LoggerInterface
91
     */
92 1
    public function getLogger()
93
    {
94 1
        return $this->logger;
95
    }
96
}
97