Completed
Push — master ( d9c485...ab74b7 )
by Mr
03:31
created

SocketTrait::closeSocket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\ClientException;
6
7
trait SocketTrait
8
{
9
    /**
10
     * Initiate socket session
11
     *
12
     * @throws  \RouterOS\Exceptions\ClientException
13
     * @throws  \RouterOS\Exceptions\ConfigException
14
     */
15 7
    private function openSocket()
16
    {
17
        // Default: Context for ssl
18 7
        $context = stream_context_create([
19 7
            'ssl' => [
20
                'ciphers'          => 'ADH:ALL',
21
                'verify_peer'      => false,
22
                'verify_peer_name' => false
23
            ]
24
        ]);
25
26
        // Default: Proto tcp:// but for ssl we need ssl://
27 7
        $proto = $this->config('ssl') ? 'ssl://' : '';
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
29
        // Initiate socket client
30 7
        $socket = @stream_socket_client(
31 7
            $proto . $this->config('host') . ':' . $this->config('port'),
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32 7
            $this->_socket_err_num,
0 ignored issues
show
Bug introduced by
The property _socket_err_num does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33 7
            $this->_socket_err_str,
0 ignored issues
show
Bug introduced by
The property _socket_err_str does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34 7
            $this->config('timeout'),
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35 7
            STREAM_CLIENT_CONNECT,
36 7
            $context
37
        );
38
39
        // Throw error is socket is not initiated
40 7
        if (!$socket) {
41 1
            throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str);
42
        }
43
44
        // Save socket to static variable
45 6
        return $this->setSocket($socket);
0 ignored issues
show
Bug introduced by
It seems like setSocket() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
    }
47
48
    /**
49
     * Close socket session
50
     *
51
     * @return bool
52
     */
53 1
    private function closeSocket(): bool
54
    {
55 1
        return fclose($this->_socket);
0 ignored issues
show
Bug introduced by
The property _socket does not seem to exist. Did you mean _socket_err_num?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
56
    }
57
}
58