TagController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 33.9 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 20
loc 59
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A create() 0 4 1
A store() 10 10 1
A show() 0 10 1
A edit() 0 4 1
A update() 10 10 1
A destroy() 0 6 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 Retosteffen\LaravelBlog\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Retosteffen\LaravelBlog\Models\LaravelBlog;
7
use Spatie\Tags\Tag;
8
9
class TagController
10
{
11
    public function index(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...
12
    {
13
        $tags = Tag::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE);
14
15
        return view('laravel-blog::tag.index', compact('tags'));
16
    }
17
18
    public function create(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...
19
    {
20
        return view('laravel-blog::tag.create');
21
    }
22
23 View Code Duplication
    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...
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...
24
    {
25
        $attributes = request()->validate([
26
            'name'=>['required'],
27
        ]);
28
29
        $tag = Tag::findOrCreate($attributes['name']);
0 ignored issues
show
Unused Code introduced by
$tag 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
        return redirect()->route('tags');
32
    }
33
34
    public function show($slug)
35
    {
36
        $tag = Tag::findFromSlug($slug);
37
38
        $item = $tag;
39
40
        $posts = LaravelBlog::where('published', '=', true)->withAnyTags([$tag])->get();
41
42
        return view('laravel-blog::archive', compact('item', 'posts'));
43
    }
44
45
    public function edit(Tag $tag)
46
    {
47
        return view('laravel-blog::tag.edit', compact('tag'));
48
    }
49
50 View Code Duplication
    public function update(Request $request, Tag $tag)
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...
51
    {
52
        $attributes = request()->validate([
53
            'name'=>['required'],
54
        ]);
55
56
        $tag->update($attributes);
57
58
        return redirect()->route('tags');
59
    }
60
61
    public function destroy(Tag $tag)
62
    {
63
        $tag->delete();
64
65
        return redirect()->route('tags');
66
    }
67
}
68