TaskController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 144
Duplicated Lines 27.08 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 13
c 6
b 0
f 1
lcom 0
cbo 3
dl 39
loc 144
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 11 1
A create() 0 3 1
A store() 0 5 1
A show() 19 19 2
A edit() 0 3 1
A update() 20 20 2
A destroy() 0 3 1
A transformCollection() 0 3 1
A transform() 0 7 1
A saveTask() 0 6 1
A __construct() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Task;
6
use Illuminate\Http\Request;
7
8
use App\Http\Requests;
9
use App\Http\Controllers\Controller;
10
use Response;
11
12
class TaskController extends Controller {
13
    /**
14
     * TaskController constructor.
15
     */
16
    public function __construct(){
17
        //$this->middleware('auth:api');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
18
    }
19
20
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function index() {
27
        // 1. No es retorna: paginacion
28
        //return Task::all();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
29
30
        $task = Task::all();
31
32
        return Response::json([
33
            'data' => $this->transformCollection($task)
34
            //'data' => $task->toArray()
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
35
        ],200);
36
    }
37
38
    /**
39
     * Show the form for creating a new resource.
40
     *
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function create() {
44
        //
45
    }
46
47
    /**
48
     * Store a newly created resource in storage.
49
     *
50
     * @param  \Illuminate\Http\Request  $request
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function store(Request $request) {
54
        $task = new Task();
55
56
        $this->saveTask($request, $task);
57
    }
58
59
    /**
60
     * Display the specified resource.
61
     *
62
     * @param  int  $id
63
     * @return \Illuminate\Http\Response
64
     */
65 View Code Duplication
    public function show($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...
66
        //return $tag = Task::findOrFail($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
67
68
        $task = Task::find($id);
69
70
        if (!$task) {
71
            return Response::json([
72
                'error' => [
73
                    'message' => 'Task does not exist',
74
                    'code' => 195
75
                 ]
76
            ], 404);
77
        }
78
79
        return Response::json([
80
            'data' => $this->transform($task->toArray())
81
            //'data' => $task->toArray()
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
82
        ],200);
83
    }
84
85
    /**
86
     * Show the form for editing the specified resource.
87
     *
88
     * @param  int  $id
89
     * @return \Illuminate\Http\Response
90
     */
91
    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...
92
        //
93
    }
94
95
    /**
96
     * Update the specified resource in storage.
97
     *
98
     * @param  \Illuminate\Http\Request  $request
99
     * @param  int  $id
100
     * @return \Illuminate\Http\Response
101
     */
102 View Code Duplication
    public function update(Request $request, $id) {
0 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...
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...
103
        //$task = Task::findOrFail($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
104
105
        $task = Task::find($id);
106
107
        if (!$task) {
108
            return Response::json([
109
                'error' => [
110
                    'message' => 'Task does not exist',
111
                    'code' => 195
112
                ]
113
            ], 404);
114
        }
115
116
        return Response::json([
117
            'data' => $task->toArray()
118
        ],200);
119
120
        $this->saveTask($request, $task);
0 ignored issues
show
Unused Code introduced by
$this->saveTask($request, $task); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
121
    }
122
123
    /**
124
     * Remove the specified resource from storage.
125
     *
126
     * @param  int  $id
127
     * @return \Illuminate\Http\Response
128
     */
129
    public function destroy($id) {
130
        Task::destroy($id);
131
    }
132
133
    public function transformCollection($task) {
134
        return array_map([$this, 'transform'], $task->toArray());
135
    }
136
137
    private function transform($task){
138
        return [
139
            'name' => $task['name'],
140
            'priority' => (boolean) $task['priority'],
141
            'done' => $task['done']
142
        ];
143
    }
144
145
    /**
146
     * @param Request $request
147
     * @param $tag
148
     */
149
    public function saveTask(Request $request, $task) {
150
        $task->name = $request->name;
1 ignored issue
show
Bug introduced by
The property name does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
151
        $task->priority = $request->priority;
1 ignored issue
show
Bug introduced by
The property priority does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
152
        $task->done = $request->done;
1 ignored issue
show
Bug introduced by
The property done does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
153
        $task->save();
154
    }
155
}
156