Completed
Pull Request — master (#2254)
by
unknown
11:30
created

ModelForm::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Controllers;
4
5
trait ModelForm
6
{
7
    /**
8
     * Display the specified resource.
9
     *
10
     * @param int $id
11
     *
12
     * @return \Illuminate\Http\Response
13
     */
14
    public function show($id)
15
    {
16
        return $this->edit($id);
0 ignored issues
show
Bug introduced by
It seems like edit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
17
    }
18
19
    /**
20
     * Update the specified resource in storage.
21
     *
22
     * @param int $id
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function update($id)
27
    {
28
        return $this->form()->update($id);
0 ignored issues
show
Bug introduced by
It seems like form() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
    }
30
31
    /**
32
     * Remove the specified resource from storage.
33
     *
34
     * @param int $id
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function destroy($id)
39
    {
40
        if ($this->form()->destroy($id)) {
0 ignored issues
show
Bug introduced by
It seems like form() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
            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...
42
                'status'  => true,
43
                'message' => trans('admin.delete_succeeded'),
44
            ]);
45
        } else {
46
            return response()->json([
47
                'status'  => false,
48
                'message' => trans('admin.delete_failed'),
49
            ]);
50
        }
51
    }
52
53
    /**
54
     * Store a newly created resource in storage.
55
     *
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function store()
59
    {
60
        return $this->form()->store();
0 ignored issues
show
Bug introduced by
It seems like form() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
    }
62
}
63