Completed
Push — master ( be02a5...a1ec95 )
by Nick
22:04 queued 02:39
created

GenericController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace EloquentJs\Controllerless;
4
5
use EloquentJs\Model\AcceptsEloquentJsQueries;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
9
class GenericController extends Controller
10
{
11
    /**
12
     * @var AcceptsEloquentJsQueries
13
     */
14
    protected $model;
15
16
    /**
17
     * Create a new GenericController instance.
18
     *
19
     * @param AcceptsEloquentJsQueries $model
20
     */
21
    public function __construct(AcceptsEloquentJsQueries $model)
22
    {
23
        $this->model = $model;
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index()
32
    {
33
        return $this->model->eloquentJs()->get();
0 ignored issues
show
Bug introduced by
The method eloquentJs() does not exist on EloquentJs\Model\AcceptsEloquentJsQueries. Did you maybe mean scopeEloquentJs()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
34
    }
35
36
    /**
37
     * Store a newly created resource in storage.
38
     *
39
     * @param  \Illuminate\Http\Request  $request
40
     * @return Model
41
     */
42
    public function store(Request $request)
43
    {
44
        return $this->model->create($request->all());
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<EloquentJs\Model\AcceptsEloquentJsQueries>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
    }
46
47
    /**
48
     * Display the specified resource.
49
     *
50
     * @param  int  $id
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function show($id)
54
    {
55
        return $this->model->eloquentJs()->find($id);
0 ignored issues
show
Bug introduced by
The method eloquentJs() does not exist on EloquentJs\Model\AcceptsEloquentJsQueries. Did you maybe mean scopeEloquentJs()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
    }
57
58
    /**
59
     * Update the specified resource in storage.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     * @param  int  $id
63
     * @return \Illuminate\Http\Response
64
     */
65
    public function update(Request $request, $id)
66
    {
67
        if ($id === '*') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $id (integer) and '*' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
68
            return $this->model->newQuery()->eloquentJs()->update($request->all());
0 ignored issues
show
Bug introduced by
The method newQuery() does not seem to exist on object<EloquentJs\Model\AcceptsEloquentJsQueries>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        }
70
71
        $resource = $this->model->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail() does not seem to exist on object<EloquentJs\Model\AcceptsEloquentJsQueries>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
73
        $resource->update($request->all());
74
75
        return $resource;
76
    }
77
78
    /**
79
     * Remove the specified resource from storage.
80
     *
81
     * @param  int  $id
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function destroy($id)
85
    {
86
        if ($id === '*') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $id (integer) and '*' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
87
            return $this->model->newQuery()->eloquentJs()->delete();
0 ignored issues
show
Bug introduced by
The method newQuery() does not seem to exist on object<EloquentJs\Model\AcceptsEloquentJsQueries>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        }
89
90
        $resource = $this->model->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail() does not seem to exist on object<EloquentJs\Model\AcceptsEloquentJsQueries>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
92
        return ['success' => $resource->delete()];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('success' => $resource->delete()); (array) is incompatible with the return type documented by EloquentJs\Controllerles...ericController::destroy of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
93
    }
94
}
95