RCONSocket::checkCommandReceived()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Torralbodavid\DuckFunkCore\Http\RCON;
4
5
use Exception;
6
7
class RCONSocket
8
{
9
    protected $socket;
10
    protected $command;
11
12
    /**
13
     * RCONSocket constructor.
14
     * @param array $command
15
     */
16
    public function __construct(array $command)
17
    {
18
        try {
19
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
20
        } catch (Exception $exception) {
21
            return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
22
                [
23
                    'connection' => 'failed',
24
                    'error'   => $exception->getMessage(),
25
                ]
26
            );
27
        }
28
29
        if ($socket === false) {
30
            return response()->json(
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
31
                [
32
                    'connection' => 'failed',
33
                    'error'   => socket_strerror(socket_last_error()),
34
                ]
35
            );
36
        }
37
38
        try {
39
            $result = socket_connect($socket, config('duck-funk.host_rcon'), config('duck-funk.port_rcon'));
40
        } catch (Exception $exception) {
41
            return response()->json(
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
42
              [
43
                  'connection' => 'failed',
44
                  'error'   => $exception->getMessage(),
45
              ]
46
            );
47
        }
48
49
        if ($result === false) {
50
            return response()->json(
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
51
                [
52
                    'connection' => 'failed',
53
                    'error'   => 'The socket connection does not exist',
54
                ]
55
            );
56
        }
57
58
        $command['auth_key'] = config('duck-funk.auth_key_rcon');
59
        $this->command = json_encode($command);
60
        $this->socket = $socket;
61
    }
62
63
    public function run()
64
    {
65
        try {
66
            $this->sendCommand();
67
            $this->checkCommandReceived();
68
            $this->destruct();
69
        } catch (Exception $exception) {
70
            return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
71
                [
72
                    'connection' => 'failed',
73
                    'error'   => $exception->getMessage(),
74
                ]
75
            );
76
        }
77
78
        return response()->json(
79
            [
80
                'connection' => 'success',
81
            ]
82
        );
83
    }
84
85
    private function sendCommand()
86
    {
87
        if (socket_write($this->socket, $this->command, strlen($this->command)) === false) {
88
            echo socket_strerror(socket_last_error($this->socket));
89
90
            return false;
91
        } else {
92
            return true;
93
        }
94
    }
95
96
    private function checkCommandReceived()
97
    {
98
        return socket_read($this->socket, strlen($this->command));
99
    }
100
101
    private function destruct()
102
    {
103
        socket_close($this->socket);
104
    }
105
}
106