Connection::write()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 1
dl 0
loc 28
rs 9.1608
c 0
b 0
f 0
1
<?php
2
3
namespace RedMoon\TinkerServer;
4
5
class Connection
6
{
7
    /** @var int */
8
    protected $socket;
9
10
    /** @var string */
11
    protected $host;
12
13
    /**
14
     * Connection constructor.
15
     *
16
     * @param string $host
17
     */
18
    public function __construct(string $host)
19
    {
20
        $this->host = $host;
21
    }
22
23
    /**
24
     * Write.
25
     *
26
     * @param  array  $namedParameters
27
     *
28
     * @return bool
29
     */
30
    public function write(array $namedParameters): bool
31
    {
32
        if (! $this->socket = $this->socket ?: $this->createSocket()) {
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->socket ?: $this->createSocket() can also be of type resource. However, the property $socket is declared as type integer. 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...
33
            return false;
34
        }
35
36
        set_error_handler([self::class, 'nullErrorHandler']);
37
38
        try {
39
            $encodedPayload = base64_encode(serialize($namedParameters))."\n";
40
41
            if (-1 !== stream_socket_sendto($this->socket, $encodedPayload)) {
42
                return true;
43
            }
44
45
            stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
46
            fclose($this->socket);
47
            $this->socket = $this->createSocket();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createSocket() of type resource is incompatible with the declared type integer of property $socket.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
49
            if (-1 !== stream_socket_sendto($this->socket, $encodedPayload)) {
50
                return true;
51
            }
52
        } finally {
53
            restore_error_handler();
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * nullErrorHandler.
61
     *
62
     * @param  $t
63
     * @param  $m
64
     *
65
     * @return void
66
     */
67
    private static function nullErrorHandler($t, $m)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $t 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...
Unused Code introduced by
The parameter $m 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...
68
    {
69
        // no-op
70
    }
71
72
    /**
73
     * Create Socket.
74
     *
75
     * @return resource
76
     */
77
    private function createSocket()
78
    {
79
        set_error_handler([self::class, 'nullErrorHandler']);
80
        try {
81
            return stream_socket_client($this->host, $errno, $errstr, 3, STREAM_CLIENT_CONNECT);
82
        } finally {
83
            restore_error_handler();
84
        }
85
    }
86
}
87