Test Setup Failed
Push — master ( acfc24...9b7451 )
by Marcel
03:57
created

TaggingController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Tag;
6
use App\Models\TagRelation;
7
use Carbon\Carbon;
8
use Illuminate\Http\Request;
9
10
class TaggingController extends Controller
11
{
12
    public function index($orderby = 'tag', $direction = 'asc'){
13
        $tags = Tag::all()->sortBy('title');
14
15
        return view('tags.index', [
16
            'tags' => $tags,
17
            'orderby' => $orderby,
18
            'direction' => $direction,
19
        ]);
20
    }
21
22 View Code Duplication
    public function showGames($tagid) {
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...
23
        $games = \DB::table('games')
24
            ->leftJoin('games_developer', 'games.id', '=', 'games_developer.game_id')
25
            ->leftJoin('developer', 'games_developer.developer_id', '=', 'developer.id')
26
            ->leftJoin('makers', 'makers.id', '=', 'games.maker_id')
27
            ->leftJoin('tag_relations', function($join) {
28
                $join->on('tag_relations.content_id', '=', 'games.id');
29
                $join->on('tag_relations.content_type', '=', \DB::raw("'game'"));
30
            })
31
            ->leftJoin('tags', 'tag_relations.tag_id', '=', 'tags.id')
32
            ->leftJoin('games_files', 'games_files.game_id', '=', 'games.id')
33
            ->leftJoin('users', 'games_developer.user_id', '=', 'users.id')
34
            ->select([
35
                'games.id as gameid',
36
                'games.title as gametitle',
37
                'games.subtitle as gamesubtitle',
38
                'developer.name as developername',
39
                'developer.id as developerid',
40
                'developer.created_at as developerdate',
41
                'developer.user_id as developeruserid',
42
                'users.name as developerusername',
43
                'games.created_at as gamecreated_at',
44
                'makers.short as makershort',
45
                'makers.title as makertitle',
46
                'makers.id as makerid',
47
                'games.views as views',
48
                'tags.title as tag',
49
            ])
50
            ->selectRaw('(SELECT COUNT(id) FROM comments WHERE content_id = games.id AND content_type = "game") as commentcount')
51
            ->selectRaw('(SELECT SUM(vote_up) FROM comments WHERE content_id = games.id AND content_type = "game") as voteup')
52
            ->selectRaw('(SELECT SUM(vote_down) FROM comments WHERE content_id = games.id AND content_type = "game") as votedown')
53
            ->selectRaw('MAX(games_files.release_type) as gametype')
54
            ->selectRaw("(SELECT STR_TO_DATE(CONCAT(release_year,'-',release_month,'-',release_day ), '%Y-%m-%d') FROM games_files WHERE game_id = games.id ORDER BY release_year DESC, release_month DESC, release_day DESC LIMIT 1) as releasedate")
55
            ->selectRaw('(SELECT COUNT(id) FROM games_coupdecoeur WHERE game_id = games.id) as cdccount')
56
            ->where('tags.id', '=', $tagid)
57
            ->orderBy('games.title')
58
            ->groupBy('games.id')
59
            ->get();
60
61
        $gametypes = \DB::table('games_files_types')
62
            ->select('id', 'title', 'short')
63
            ->get();
64
        $gtypes = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
65
        foreach ($gametypes as $gt) {
66
            $t['title'] = $gt->title;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$t was never initialized. Although not strictly required by PHP, it is generally a good practice to add $t = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
67
            $t['short'] = $gt->short;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
            $gtypes[$gt->id] = $t;
69
        }
70
71
        return view('tags.show', [
72
            'games' => $games,
73
            'gametypes' => $gtypes,
74
        ]);
75
    }
76
77
    public function store(Request $request) {
78
        $this->validate($request, [
79
            'title' => 'required',
80
            'content_id' => 'required',
81
            'content_type' => 'required',
82
        ]);
83
84
        $tag = Tag::firstOrCreate(['title' => $request->get('title')]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
85
        $tagid = $tag->id;
86
87
        $tagrel = TagRelation::firstOrCreate([
0 ignored issues
show
Unused Code introduced by
$tagrel 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...
88
            'tag_id' => $tagid,
89
            'user_id' => \Auth::id(),
90
            'content_id' => $request->get('content_id'),
91
            'content_type' => $request->get('content_type'),
92
        ]);
93
94
        if ($request->get('content_type') == 'news') {
95
            return \Redirect::action('NewsController@show', $request->get('content_id'));
96
        }elseif ($request->get('content_type') == 'game') {
97
            return \Redirect::action('GameController@show', $request->get('content_id'));
98
        }elseif ($request->get('content_type') == 'resource') {
99
            return \Redirect::action('ResourceController@show', $request->get('content_id'));
100
        }
101
102
        return \Redirect::action('IndexController@index');
103
    }
104
}
105