TagController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 13
c 5
b 0
f 4
lcom 2
cbo 4
dl 12
loc 124
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 8 1
A create() 0 4 1
A store() 0 12 2
A show() 12 12 2
A edit() 0 4 1
A update() 0 10 2
A destroy() 0 4 1
A getTags() 0 4 2

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 Acme\Transformers\TagTransformer;
6
use App\Tag;
7
use App\Task;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\Response as IlluminateResponse;
10
use Illuminate\Support\Facades\Input;
11
12
/**
13
 * Class TagController
14
 * @package App\Http\Controllers
15
 */
16
class TagController extends ApiController
17
{
18
    /**
19
     * @var TagTransformer
20
     */
21
    protected $tagTransformer;
22
    /**
23
     * TagController constructor.
24
     * @param $tagTransformer
25
     */
26
    public function __construct(TagTransformer $tagTransformer)
27
    {
28
        $this->tagTransformer = $tagTransformer;
29
    }
30
31
    /**
32
     * Display a listing of the resource.
33
     * @param null $taskId
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function index($taskId = null)
37
    {
38
        //1. No és retorna: paginació
39
        //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...
40
41
        $tag = $this->getTags($taskId);
42
        return $this->respond($this->tagTransformer->transformCollection($tag->all()));
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
     *  @return \Illuminate\Http\Response
58
     */
59
    public function store()
60
    {
61
        if (!Input::get('title'))
62
        {
63
            return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY)
64
                ->respondWithError('Parameters failed validation for a task.');
65
        }
66
67
        Tag::create(Input::all());
68
69
        return $this->respondCreated('Task successfully created.');
70
    }
71
72
    /**
73
     * Display the specified resource.
74
     *
75
     * @param  int $id
76
     * @return \Illuminate\Http\Response
77
     */
78 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...
79
    {
80
        $tag = Tag::find($id);
81
82
        if (!$tag) {
83
            return $this->respondNotFound('Tag does not exsist');
84
        }
85
86
        return $this->respond([
87
            'data' => $this->tagTransformer->transform($tag)
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
        $tag = Tag::find($id);
112
        if (!$tag)
113
        {
114
            return $this->respondNotFound('Tag does not exist!');
115
        }
116
        $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...
117
        $tag->save();
118
    }
119
120
    /**
121
     * Remove the specified resource from storage.
122
     *
123
     * @param  int $id
124
     * @return \Illuminate\Http\Response
125
     */
126
    public function destroy($id)
127
    {
128
        Tag::destroy($id);
129
    }
130
131
    /**
132
    * @param $taskId
133
    * @return \Illuminate\Database\Eloquent\Collection|static[]
134
    */
135
    public function getTags($taskId)
136
     {
137
         return $taskId ? Task::findOrFail($taskId)->tags : Tag::all();
138
     }
139
}