Completed
Pull Request — master (#447)
by Alexandru
01:22
created

ConnectionLogger::decorate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
4
5
use Ratchet\ConnectionInterface;
6
7
class ConnectionLogger extends Logger implements ConnectionInterface
8
{
9
    /**
10
     * The connection to watch.
11
     *
12
     * @var \Ratchet\ConnectionInterface
13
     */
14
    protected $connection;
15
16
    /**
17
     * Create a new instance and add a connection to watch.
18
     *
19
     * @param  \Ratchet\ConnectionInterface  $connection
0 ignored issues
show
Bug introduced by
There is no parameter named $connection. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
20
     * @return self
21
     */
22
    public static function decorate(ConnectionInterface $app): self
23
    {
24
        $logger = app(self::class);
25
26
        return $logger->setConnection($app);
27
    }
28
29
    /**
30
     * Set a new connection to watch.
31
     *
32
     * @param  \Ratchet\ConnectionInterface  $connection
33
     * @return $this
34
     */
35
    public function setConnection(ConnectionInterface $connection)
36
    {
37
        $this->connection = $connection;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Send data through the connection.
44
     *
45
     * @param  string  $data
46
     * @return void
47
     */
48
    public function send($data)
49
    {
50
        $socketId = $this->connection->socketId ?? null;
0 ignored issues
show
Bug introduced by
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51
52
        $this->info("Connection id {$socketId} sending message {$data}");
53
54
        $this->connection->send($data);
55
    }
56
57
    /**
58
     * Close the connection.
59
     *
60
     * @return void
61
     */
62
    public function close()
63
    {
64
        $this->warn("Connection id {$this->connection->socketId} closing.");
0 ignored issues
show
Bug introduced by
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
65
66
        $this->connection->close();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function __set($name, $value)
73
    {
74
        return $this->connection->$name = $value;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function __get($name)
81
    {
82
        return $this->connection->$name;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function __isset($name)
89
    {
90
        return isset($this->connection->$name);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function __unset($name)
97
    {
98
        unset($this->connection->$name);
99
    }
100
}
101