TagController::show()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 11
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Acme\Transformers\TagTransformer;
6
use App\Tag;
7
use App\Task;
8
use Illuminate\Http\Request;
9
10
use App\Http\Requests;
11
use App\Http\Controllers\Controller;
12
use Input;
13
14
class TagController extends ApiController
15
{
16
    protected $tagTranformer;
17
18
    function __construct(TagTransformer $tagTransformer)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        $this->tagTranformer = $tagTransformer;
21
22
        // TODO: Post test not working with auth middlerare
23
        //$this->middleware('auth.basic', ['only' => 'store']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index($taskId = null)
32
    {
33
        // 1. No es retorna tot: paginació
34
        //return Tag::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...
35
36
        $tags = $this->getTags($taskId);
37
38
        return $this->respond($this->tagTranformer->transformCollection($tags))->setStatusCode(200);
39
    }
40
41
    /**
42
     * Show the form for creating a new resource.
43
     *
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function create()
47
    {
48
49
    }
50
51
    /**
52
     * Store a newly created resource in storage.
53
     *
54
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     * @return \Illuminate\Http\Response
56
     */
57
    public function store()
58
    {
59
        if (!Input::get('title'))
60
        {
61
            return $this->setStatusCode(422)->respondWithError('Parameters failed validation for a task');
62
        }
63
64
        Tag::create(Input::all());
65
66
        return $this->respondCreated('Tag successfully created.');
67
    }
68
69
    /**
70
     * Display the specified resource.
71
     *
72
     * @param  int  $id
73
     * @return \Illuminate\Http\Response
74
     */
75 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...
76
    {
77
        $tag = Tag::find($id);
78
79
        if (!$tag)
80
        {
81
            return $this->respondNotFound('Tag does not exist');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->respondNot...('Tag does not exist'); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Http\Controllers\TagController::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...
82
        }
83
84
        return $this->respond($this->tagTranformer->transform($tag))->setStatusCode(200);
85
    }
86
87
    /**
88
     * Show the form for editing the specified resource.
89
     *
90
     * @param  int  $id
91
     * @return \Illuminate\Http\Response
92
     */
93
    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...
94
    {
95
        //
96
    }
97
98
    /**
99
     * Update the specified resource in storage.
100
     *
101
     * @param  \Illuminate\Http\Request  $request
102
     * @param  int  $id
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function update(Request $request, $id)
106
    {
107
        $tag = Tag::find($id);
108
109
        if (!$tag)
110
        {
111
            return $this->respondNotFound('Tag does not exist!!');
112
        }
113
114
        $tag->title = $request->title;
1 ignored issue
show
Bug introduced by
The property title 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...
115
        $tag->save();
116
    }
117
118
    /**
119
     * Remove the specified resource from storage.
120
     *
121
     * @param  int  $id
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function destroy($id)
125
    {
126
        Tag::destroy($id);
127
    }
128
129
    /**
130
     * @param $taskId
131
     * @return \Illuminate\Database\Eloquent\Collection|static[]
132
     */
133
    public function getTags($taskId)
134
    {
135
        return $taskId ? Task::findOrFail($taskId)->tags : Tag::all();
136
    }
137
}
138