Completed
Push — master ( edcf20...3ec6c0 )
by Marcel
03:05 queued 01:22
created

src/Server/Logger/WebsocketsLogger.php (1 issue)

property access on interfaces.

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Logger;
4
5
use Exception;
6
use Ratchet\ConnectionInterface;
7
use Ratchet\RFC6455\Messaging\MessageInterface;
8
use Ratchet\WebSocket\MessageComponentInterface;
9
use BeyondCode\LaravelWebSockets\QueryParameters;
10
11
class WebsocketsLogger extends Logger implements MessageComponentInterface
12
{
13
    /** @var \Ratchet\Http\HttpServerInterface */
14
    protected $app;
15
16
    public static function decorate(MessageComponentInterface $app): self
17
    {
18
        $logger = app(self::class);
19
20
        return $logger->setApp($app);
21
    }
22
23
    public function setApp(MessageComponentInterface $app)
24
    {
25
        $this->app = $app;
26
27
        return $this;
28
    }
29
30
    public function onOpen(ConnectionInterface $connection)
31
    {
32
        $appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
33
34
        $this->warn("New connection opened for app key {$appKey}.");
35
36
        $this->app->onOpen(ConnectionLogger::decorate($connection));
37
    }
38
39
    public function onMessage(ConnectionInterface $connection, MessageInterface $message)
40
    {
41
        $this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
42
43
        $this->app->onMessage(ConnectionLogger::decorate($connection), $message);
44
    }
45
46
    public function onClose(ConnectionInterface $connection)
47
    {
48
        $socketId = $connection->socketId ?? null;
0 ignored issues
show
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...
49
50
        $this->warn("Connection id {$socketId} closed.");
51
52
        $this->app->onClose(ConnectionLogger::decorate($connection));
53
    }
54
55
    public function onError(ConnectionInterface $connection, Exception $exception)
56
    {
57
        $exceptionClass = get_class($exception);
58
59
        $appId = $connection->app->id ?? 'Unknown app id';
60
61
        $message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`.";
62
63
        if ($this->verbose) {
64
            $message .= $exception->getTraceAsString();
65
        }
66
67
        $this->error($message);
68
69
        $this->app->onError(ConnectionLogger::decorate($connection), $exception);
70
    }
71
}
72