Completed
Pull Request — master (#413)
by
unknown
01:22
created

WebsocketsLogger::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Logger;
4
5
use BeyondCode\LaravelWebSockets\QueryParameters;
6
use Exception;
7
use Ratchet\ConnectionInterface;
8
use Ratchet\RFC6455\Messaging\MessageInterface;
9
use Ratchet\WebSocket\MessageComponentInterface;
10
11
class WebsocketsLogger extends Logger implements MessageComponentInterface
12
{
13
    /** @var \Ratchet\Http\HttpServerInterface */
14
    protected $app;
15
16
    /**
17
     * WebsocketsLogger polymorphic constructor for Logger Singleton instanciation and WSServer instanciation.
18
     *
19
     * @param MessageComponentInterface|OutputInterface $app
20
     */
21
    public function __construct($app)
22
    {
23
        if ($app instanceof OutputInterface) {
0 ignored issues
show
Bug introduced by
The class BeyondCode\LaravelWebSoc...\Logger\OutputInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
24
            parent::__construct($app);
25
        } else {
26
            $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type object<Ratchet\WebSocket...sageComponentInterface> is incompatible with the declared type object<Ratchet\Http\HttpServerInterface> of property $app.

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...
27
        }
28
    }
29
30
    public static function decorate(MessageComponentInterface $app): self
31
    {
32
        $logger = app(self::class);
33
34
        return $logger->setApp($app);
35
    }
36
37
    public function setApp(MessageComponentInterface $app)
38
    {
39
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type object<Ratchet\WebSocket...sageComponentInterface> is incompatible with the declared type object<Ratchet\Http\HttpServerInterface> of property $app.

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...
40
41
        return $this;
42
    }
43
44
    public function onOpen(ConnectionInterface $connection)
45
    {
46
        $appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
0 ignored issues
show
Bug introduced by
Accessing httpRequest 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...
47
48
        $this->warn("New connection opened for app key {$appKey}.");
49
50
        $this->app->onOpen(ConnectionLogger::decorate($connection));
51
    }
52
53
    public function onMessage(ConnectionInterface $connection, MessageInterface $message)
54
    {
55
        $this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
0 ignored issues
show
Bug introduced by
Accessing app 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...
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...
56
57
        $this->app->onMessage(ConnectionLogger::decorate($connection), $message);
58
    }
59
60
    public function onClose(ConnectionInterface $connection)
61
    {
62
        $socketId = $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...
63
64
        $this->warn("Connection id {$socketId} closed.");
65
66
        $this->app->onClose(ConnectionLogger::decorate($connection));
67
    }
68
69
    public function onError(ConnectionInterface $connection, Exception $exception)
70
    {
71
        $exceptionClass = get_class($exception);
72
73
        $appId = $connection->app->id ?? 'Unknown app id';
0 ignored issues
show
Bug introduced by
Accessing app 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...
74
75
        $message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`.";
76
77
        if ($this->verbose) {
78
            $message .= $exception->getTraceAsString();
79
        }
80
81
        $this->error($message);
82
83
        $this->app->onError(ConnectionLogger::decorate($connection), $exception);
84
    }
85
}
86