Completed
Push — develop ( e1ba06...9ac8df )
by
unknown
14:59 queued 06:41
created

PortController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\User;
6
use App\Port;
7
use Illuminate\Http\Request;
8
9
class PortController extends Controller
10
{
11
    public function __construct() {
12
13
    }
14
15
    /**
16
     * Display a listing of the resource.
17
     *
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function index(Request $request)
21
    {
22
        if ($request->user()->level >= 10 || $request->user()->level == 5) {
23
            return Port::all();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \App\Port::all(); (Illuminate\Database\Eloquent\Collection) is incompatible with the return type documented by App\Api\Controllers\PortController::index 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...
24
        }
25
        else {
26
            return User::find($request->user()->user_id)->ports()->get();
27
        }
28
    }
29
30
    /**
31
     * Show the form for creating a new resource.
32
     *
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function create()
36
    {
37
        //
38
    }
39
40
    /**
41
     * Store a newly created resource in storage.
42
     *
43
     * @param  \Illuminate\Http\Request  $request
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function store(Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        //
49
    }
50
51
    /**
52
     * Display the specified resource.
53
     *
54
     * @param  int  $id
55
     * @return \Illuminate\Http\Response
56
     */
57 View Code Duplication
    public function show(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        if ($request->user()->level >= 10 || $request->user()->level == 5) {
60
            return Port::find($id);
61
        }
62
        else {
63
            $user = User::find($request->user()->user_id);
64
            return $user->ports()->find($id);
65
        }
66
    }
67
68
    /**
69
     * Show the form for editing the specified resource.
70
     *
71
     * @param  int  $id
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function edit($id)
1 ignored issue
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        //
77
    }
78
79
    /**
80
     * Update the specified resource in storage.
81
     *
82
     * @param  \Illuminate\Http\Request  $request
83
     * @param  int  $id
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function update(Request $request, $id)
2 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        //
89
    }
90
91
    /**
92
     * Remove the specified resource from storage.
93
     *
94
     * @param  int  $id
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function destroy($id)
1 ignored issue
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        //
100
    }
101
}
102