Completed
Pull Request — master (#447)
by Marcel
02:29 queued 59s
created

SendMessage   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 36 3
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
4
5
use BeyondCode\LaravelWebSockets\Concerns\PushesToPusher;
6
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
7
use Exception;
8
use Illuminate\Http\Request;
9
10
class SendMessage
11
{
12
    use PushesToPusher;
13
14
    /**
15
     * Send the message to the requested channel.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function __invoke(Request $request)
21
    {
22
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
23
            'appId' => ['required', new AppId],
24
            'key' => 'required|string',
25
            'secret' => 'required|string',
26
            'channel' => 'required|string',
27
            'event' => 'required|string',
28
            'data' => 'required|json',
29
        ]);
30
31
        $broadcaster = $this->getPusherBroadcaster([
32
            'key' => $request->key,
33
            'secret' => $request->secret,
34
            'id' => $request->appId,
35
        ]);
36
37
        try {
38
            $decodedData = @json_decode($request->data, true);
39
40
            $broadcaster->broadcast(
41
                [$request->channel],
42
                $request->event,
43
                $decodedData ?: []
44
            );
45
        } catch (Exception $e) {
46
            return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
                'ok' => false,
48
                'exception' => $e->getMessage(),
49
            ]);
50
        }
51
52
        return response()->json([
53
            'ok' => true,
54
        ]);
55
    }
56
}
57