TaskController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Task;
6
use Illuminate\Http\Request;
7
8
use App\Acme\Transformers\TaskTransformer;
9
use App\Http\Requests;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Support\Facades\Input;
12
use phpDocumentor\Reflection\DocBlock\Tag;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class TaskController extends Controller
16
{
17
    protected $taskTransformer;
18
    /**
19
     * TaskController constructor.
20
     */
21
    public function __construct(TaskTransformer $taskTransformer)
22
    {
23
        $this->taskTransformer = $taskTransformer;
24
        $this->middleware('auth.basic', ['only' => 'store']);
25
        $this->middleware("auth.api");
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function index()
34
    {
35
        //problema 1, no retorna: paginació
36
        $task = Task::all();
37
        return $this->respond([
0 ignored issues
show
Documentation Bug introduced by
The method respond does not exist on object<App\Http\Controllers\TaskController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
38
            'data' => $this->taskTransformer->transformCollection($task->all())
39
        ]);
40
    }
41
42
    /**
43
     * Show the form for creating a new resource.
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function create()
48
    {
49
        //
50
    }
51
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @param  \Illuminate\Http\Request  $request
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function store(Request $request)
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...
59
    {
60
        if (! Input::get('name') or ! Input::get('done') or ! Input::get('priority'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
61
        {
62
            return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY)
0 ignored issues
show
Documentation Bug introduced by
The method setStatusCode does not exist on object<App\Http\Controllers\TaskController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
                ->respondWithError('Parameters failed validation for a task.');
64
        }
65
        Task::create(Input::all());
66
        return $this->respondCreated('Task successfully created.');
0 ignored issues
show
Bug introduced by
The method respondCreated() does not exist on App\Http\Controllers\TaskController. Did you maybe mean create()?

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...
67
    }
68
69
    /**
70
     * Display the specified resource.
71
     *
72
     * @param  int  $id
73
     * @return \Illuminate\Http\Response
74
     */
75
    public function show($id)
76
    {
77
        $task = Task::find($id);
78
        if (!$task) {
79
            return $this->respondNotFound('La tasca no existeix!');
0 ignored issues
show
Documentation Bug introduced by
The method respondNotFound does not exist on object<App\Http\Controllers\TaskController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
        }
81
        return $this->respond([
0 ignored issues
show
Documentation Bug introduced by
The method respond does not exist on object<App\Http\Controllers\TaskController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
82
            'data' => $this->taskTransformer->transform($task)
83
        ]);
84
85
        //return $task = 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...
86
87
        //Es el mateix
88
        //$task = Tag::where('id', $id)->first();
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
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::findOrFail($id);
112
113
        $this->saveTask($request, $task);
114
    }
115
116
    /**
117
     * Remove the specified resource from storage.
118
     *
119
     * @param  int  $id
120
     * @return \Illuminate\Http\Response
121
     */
122
    public function destroy($id)
123
    {
124
        Task::destroy($id);
125
    }
126
127
    /**
128
     * @param Request $request
129
     * @param $task
130
     */
131
    public function saveTask(Request $request, $task)
132
    {
133
        $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...
134
        $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...
135
        $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...
136
        $task->save();
137
    }
138
139
    public function transform ($tasks){
140
        return arry_map(function ($task){
141
            return [
142
            'name' => $task['name'],
143
            'text' => $task['text'],
144
            'done' => $task['done'],
145
                ];
146
        }, $tasks->toArray());
147
    }
148
}
149