TaskController::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
use App\Http\Requests;
8
use App\Task;
9
use App\Http\Controllers\Controller;
10
use App\Http\Transformers\TaskTransformer;
11
12
class TaskController extends Controller
13
{
14
    protected $TaskTransformer;
15
    protected $tagTransformer;
16
17
    public function __construct(TaskTransformer $TaskTransformer)
18
    {
19
         $this->TaskTransformer = $TaskTransformer;
20
    }
21
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function index()
28
    {
29
        $transformer = new TaskTransformer();
0 ignored issues
show
Unused Code introduced by
$transformer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
31
        $tasks = Task::get();
32
33
        return Response::json(
34
            $this->tagTransformer->transformCollection($tasks),
35
            200
36
        );
37
//        return response() -> json([
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
//            "msg" => "Success",
39
//            "tasks" =>  $this->TaskTransformer->transformCollection($tasks)
40
//            ], 200
41
//        );
42
    }
43
44
45
    /**
46
     * Show the form for creating a new resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function create()
51
    {
52
        //
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  \Illuminate\Http\Request  $request
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function store(Request $request)
62
    {
63
        Task::create($request->all());
64
        return ['created' => true];
65
    }
66
67
    /**
68
     * Display the specified resource.
69
     *
70
     * @param  int  $id
71
     * @return \Illuminate\Http\Response
72
     */
73
    public function show($id, $task)
0 ignored issues
show
Unused Code introduced by
The parameter $task 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...
74
    {
75
        $task = Task::find($id);
76
        if(!$task){
77
            return response() -> json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...does not exist'), 404); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Http\Controllers\TaskController::show 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...
78
            "msg" => "does not exist"            
79
            ], 404
80
        );
81
        } else {
82
83
        return Response::json(
84
            $this->TaskTransformer->transform($task),
85
            200
86
        );
87
88
        }
89
    }
90
91
    /**
92
     * Show the form for editing the specified resource.
93
     *
94
     * @param  int  $id
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function edit($id)
0 ignored issues
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
    /**
103
     * Update the specified resource in storage.
104
     *
105
     * @param  \Illuminate\Http\Request  $request
106
     * @param  int  $id
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function update(Request $request, $id)
110
    {
111
        $task = Task::find($id);
112
        $task->update($request->all());
113
        return ['updated' => true];
114
115
    }
116
117
    /**
118
     * Remove the specified resource from storage.
119
     *
120
     * @param  int  $id
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function destroy($id)
124
    {
125
        Task::destroy($id);
126
        return ['deleted' => true];
127
    }
128
129
    public function transformCollection($task)
130
    {
131
     return array_map([$this, 'transform'],$task->toArray());
132
    }
133 View Code Duplication
    public function transform($task)
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...
134
    {
135
        return [
136
            'name' => $task['name'],
137
            'done' => $task['done'],
138
            'priority' => $task['priority']
139
        ];
140
            
141
142
    }
143
    
144
}
145