Completed
Push — master ( 53691c...15e6b6 )
by Marcel
01:44
created

Messages/PusherChannelProtocolMessage.php (3 issues)

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\WebSockets\Messages;
4
5
use stdClass;
6
use Ratchet\ConnectionInterface;
7
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
8
9
class PusherChannelProtocolMessage implements PusherMessage
10
{
11
    /** @var \stdClass */
12
    protected $payload;
13
14
    /** @var \React\Socket\ConnectionInterface */
15
    protected $connection;
16
17
    /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
18
    protected $channelManager;
19
20
    public function __construct(stdClass $payload, ConnectionInterface $connection, ChannelManager $channelManager)
21
    {
22
        $this->payload = $payload;
23
24
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type object<Ratchet\ConnectionInterface> is incompatible with the declared type object<React\Socket\ConnectionInterface> of property $connection.

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...
25
26
        $this->channelManager = $channelManager;
27
    }
28
29
    public function respond()
30
    {
31
        $eventName = camel_case(str_after($this->payload->event, ':'));
32
33
        if (method_exists($this, $eventName)) {
34
            call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass());
35
        }
36
    }
37
38
    /*
39
     * @link https://pusher.com/docs/pusher_protocol#ping-pong
40
     */
41
    protected function ping(ConnectionInterface $connection)
42
    {
43
        $connection->send(json_encode([
44
            'event' => 'pusher:pong',
45
        ]));
46
    }
47
48
    /*
49
     * @link https://pusher.com/docs/pusher_protocol#pusher-subscribe
50
     */
51
    protected function subscribe(ConnectionInterface $connection, stdClass $payload)
52
    {
53
        $channel = $this->channelManager->findOrCreate($connection->app->id, $payload->channel);
0 ignored issues
show
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...
54
55
        $channel->subscribe($connection, $payload);
56
    }
57
58
    public function unsubscribe(ConnectionInterface $connection, stdClass $payload)
59
    {
60
        $channel = $this->channelManager->findOrCreate($connection->app->id, $payload->channel);
0 ignored issues
show
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...
61
62
        $channel->unsubscribe($connection);
63
    }
64
}
65