Completed
Push — master ( f066c0...72d72a )
by Yaro
01:50 queued 10s
created

SortableHandlerTrait::init()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Exceptions\PermissionDenied;
7
use Yaro\Jarboe\Table\CRUD;
8
9
trait SortableHandlerTrait
10
{
11
    /**
12
     * Switch table order for making sortable table.
13
     *
14
     * @return \Illuminate\Http\RedirectResponse
15
     * @throws PermissionDenied
16
     */
17
    public function switchSortable()
18
    {
19
        $this->init();
20
        $this->bound();
21
22
        if (!$this->crud()->isSortableByWeight()) {
23
            throw new PermissionDenied();
24
        }
25
26
        $newState = !$this->crud()->isSortableByWeightActive();
27
        $this->crud()->setSortableOrderState($newState);
28
29
        return back();
30
    }
31
32
    /**
33
     * Change sort weight of dragged row.
34
     *
35
     * @param $id
36
     * @param Request $request
37
     * @return \Illuminate\Http\JsonResponse
38
     * @throws PermissionDenied
39
     */
40
    public function moveItem($id, Request $request)
41
    {
42
        $this->init();
43
        $this->bound();
44
45
        if (!$this->crud()->isSortableByWeight()) {
46
            throw new PermissionDenied();
47
        }
48
49
        $this->crud()->repo()->reorder($id, $request->get('prev'), $request->get('next'));
50
51
        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...
52
            'ok' => true,
53
        ]);
54
    }
55
56
    abstract protected function init();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
57
    abstract protected function bound();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
58
    abstract protected function crud(): CRUD;
59
    abstract protected function can($action): bool;
60
}
61