Completed
Pull Request — master (#75)
by Alex
01:36
created

TriggerBatchedEventsController::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\Concerns\BroadcastsEvents;
8
9
class TriggerBatchedEventsController extends Controller
10
{
11
    use BroadcastsEvents;
12
13
    public function __invoke(Request $request)
14
    {
15
        $this->ensureValidSignature($request);
16
17
        $validator = validator($request->json()->all(), [
18
            'batch.*.name' => 'required',
19
            'batch.*.data' => 'required',
20
            'batch.*.channel' => 'required',
21
        ], [
22
            'required' => 'Missing required parameter: :attribute',
23
        ]);
24
25
        if ($validator->fails()) {
0 ignored issues
show
Bug introduced by
The method fails does only exist in Illuminate\Contracts\Validation\Validator, but not in Illuminate\Contracts\Validation\Factory.

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...
26
            return new JsonResponse([
27
                'body' => 'Failed input validation.',
28
                'status' => 400,
29
                'validation_errors' => $validator->errors()->toArray(),
0 ignored issues
show
Bug introduced by
The method errors does only exist in Illuminate\Contracts\Validation\Validator, but not in Illuminate\Contracts\Validation\Factory.

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...
30
            ], 400);
31
        }
32
33
        foreach ($request->json()->get('batch', []) as $event) {
34
            $this->broadcastEventForAppToChannel(
35
                $request->appId,
36
                $event['channel'],
37
                $event['name'],
38
                $event['data'],
39
                $event['socket_id'] ?? null
40
            );
41
        }
42
43
        return $request->json()->all();
44
    }
45
}
46