Completed
Pull Request — master (#100)
by Maxime
03:59
created

AbstractConnection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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\Core;
13
14
use Nekland\Woketo\Exception\NoHandlerException;
15
use Nekland\Woketo\Exception\WebsocketException;
16
use Nekland\Woketo\Message\MessageHandlerInterface;
17
use Nekland\Woketo\Rfc6455\Handshake\HandshakeInterface;
18
use Nekland\Woketo\Rfc6455\Message;
19
use Nekland\Woketo\Rfc6455\MessageProcessor;
20
use Psr\Log\LoggerAwareTrait;
21
use React\EventLoop\LoopInterface;
22
use React\EventLoop\Timer\TimerInterface;
23
use React\Socket\Connection;
24
25
abstract class AbstractConnection
26
{
27
    /**
28
     * 5 seconds
29
     */
30
    const DEFAULT_TIMEOUT = 5;
31
32
    use LoggerAwareTrait;
33
34
    /**
35
     * @var Connection
36
     */
37
    protected $stream;
38
39
    /**
40
     * @var MessageProcessor
41
     */
42
    protected $messageProcessor;
43
44
    /**
45
     * @var \Nekland\Woketo\Rfc6455\Handshake\ServerHandshake|\Nekland\Woketo\Rfc6455\Handshake\ClientHandshake
46
     */
47
    protected $handshake;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $handshakeDone;
53
54
    /**
55
     * @var string
56
     */
57
    protected $uri;
58
59
    /**
60
     * @var MessageHandlerInterface|\Closure
61
     */
62
    protected $handler;
63
64
    /**
65
     * @var TimerInterface
66
     */
67
    protected $timeout;
68
69
    /**
70
     * @var Message
71
     */
72
    protected $currentMessage;
73
74
    /**
75
     * @var LoopInterface
76
     */
77
    protected $loop;
78
79
    public function __construct(MessageProcessor $messageProcessor, HandshakeInterface $handshake = null)
80
    {
81
        $this->handshake = $handshake;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handshake can also be of type object<Nekland\Woketo\Rf...ake\HandshakeInterface>. However, the property $handshake is declared as type object<Nekland\Woketo\Rf...dshake\ClientHandshake>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
82
        $this->messageProcessor = $messageProcessor;
83
    }
84
85
    protected function onMessage(string $data)
86
    {
87
//        var_dump($data, 'hello');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
88
        try {
89
            if (!$this->handshakeDone) {
90
                $this->processHandshake($data);
91
            } else {
92
                $this->processMessage($data);
93
            }
94
95
            return;
96
        } catch (WebsocketException $e) {
97
            $this->messageProcessor->close($this->stream);
98
            $this->logger->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage());
99
            $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...
100
        }
101
    }
102
103
    protected abstract function processHandshake(string $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
104
    protected abstract function processMessage(string $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
105
106
    /**
107
     * @return string
108
     */
109
    public function getIp()
110
    {
111
        return $this->stream->getRemoteAddress();
112
    }
113
114
    /**
115
     * @return \Psr\Log\LoggerInterface
116
     */
117
    public function getLogger()
118
    {
119
        return $this->logger;
120
    }
121
122
    /**
123
     * @return MessageHandlerInterface
124
     * @throws NoHandlerException
125
     */
126
    protected function getHandler() : MessageHandlerInterface
127
    {
128
        if ($this->handler instanceof \Closure) {
129
            $handler = $this->handler;
130
            $handler = $handler($this->uri, $this);
131
132
            if (null === $handler) {
133
                throw new NoHandlerException(sprintf('No handler for request URI %s.', $this->uri));
134
            }
135
136
            $this->handler = $handler;
137
        }
138
139
        return $this->handler;
140
    }
141
}
142