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

WebSocketClient::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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
use Nekland\Woketo\Message\MessageHandlerInterface;
15
16
class WebSocketClient
17
{
18
    /**
19
     * @var int
20
     */
21
    private $port;
22
23
    /**
24
     * @var string
25
     */
26
    private $host;
27
28
    /**
29
     * @var array
30
     */
31
    private $config;
32
33
    /**
34
     * @var ConnectorFactoryInterface
35
     */
36
    private $connectorFactory;
37
38
    public function __construct(int $port, string $host, array $config = [], ConnectorFactoryInterface $connectorFactory = null)
39
    {
40
        $this->port = $port;
41
        $this->host = $host;
42
        $this->connectorFactory = $connectorFactory ?: new ConnectorFactory();
43
        $this->setConfig($config);
44
    }
45
46
    public function start(MessageHandlerInterface $handler)
0 ignored issues
show
Unused Code introduced by
The parameter $handler 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...
47
    {
48
        if ($this->config['prod'] && \extension_loaded('xdebug')) {
49
            throw new \Exception('xdebug is enabled, it\'s a performance issue. Disable that extension or specify "prod" option to false.');
50
        }
51
52
        $connection = new Connection($this->connectorFactory->createConnector($this->host, $this->port));
0 ignored issues
show
Unused Code introduced by
$connection 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...
Bug introduced by
The call to Connection::__construct() misses some required arguments starting with $host.
Loading history...
Documentation introduced by
$this->connectorFactory-...his->host, $this->port) is of type object<React\Promise\PromiseInterface>, but the function expects a string.

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...
53
    }
54
55
    /**
56
     * @param array $config
57
     * @return self
58
     */
59
    public function setConfig(array $config = [])
60
    {
61
        $this->config = array_merge([
62
            'prod' => true
63
        ], $config);
64
65
        return $this;
66
    }
67
}