Test Failed
Push — master ( 8fcff1...262e91 )
by Marcel
04:31
created

CDCController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Game;
6
use Carbon\Carbon;
7
use Illuminate\Http\Request;
8
9
class CDCController extends Controller
10
{
11
    /**
12
     * Display a listing of the resource.
13
     *
14
     * @return \Illuminate\Http\Response
15
     */
16 View Code Duplication
    public function index()
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...
17
    {
18
        $cdc = \DB::table('games_coupdecoeur')
19
            ->leftJoin('games', 'games.id', '=', 'games_coupdecoeur.game_id')
20
            ->leftJoin('games_developer', 'games.id', '=', 'games_developer.game_id')
21
            ->leftJoin('developer', 'games_developer.developer_id', '=', 'developer.id')
22
            ->leftJoin('makers', 'makers.id', '=', 'games.maker_id')
23
            ->leftJoin('comments', function($join) {
24
                $join->on('comments.content_id', '=', 'games.id');
25
                $join->on('comments.content_type', '=', \DB::raw("'game'"));
26
            })
27
            ->leftJoin('games_files', 'games_files.game_id', '=', 'games.id')
28
            ->leftJoin('users', 'games_developer.user_id', '=', 'users.id')
29
            ->select([
30
                'games.id as gameid',
31
                'games.title as gametitle',
32
                'games.subtitle as gamesubtitle',
33
                'developer.name as developername',
34
                'developer.id as developerid',
35
                'developer.created_at as developerdate',
36
                'developer.user_id as developeruserid',
37
                'users.name as developerusername',
38
                'games.created_at as gamecreated_at',
39
                'makers.short as makershort',
40
                'makers.title as makertitle',
41
                'makers.id as makerid',
42
                'games_coupdecoeur.created_at as cdcdate',
43
            ])
44
            ->selectRaw('(SELECT COUNT(id) FROM comments WHERE content_id = games.id AND content_type = "game") as commentcount')
45
            ->selectRaw('(SELECT SUM(vote_up) FROM comments WHERE content_id = games.id AND content_type = "game") as voteup')
46
            ->selectRaw('(SELECT SUM(vote_down) FROM comments WHERE content_id = games.id AND content_type = "game") as votedown')
47
            ->selectRaw('MAX(games_files.release_type) as gametype')
48
            ->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")
49
            ->selectRaw('(SELECT COUNT(id) FROM games_coupdecoeur WHERE game_id = games.id) as cdccount')
50
            ->groupBy('games.id')
51
            ->orderBy('games_coupdecoeur.created_at', 'desc')
52
            ->get();
53
54
        $gametypes = \DB::table('games_files_types')
55
            ->select('id', 'title', 'short')
56
            ->get();
57
        $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...
58
        foreach ($gametypes as $gt) {
59
            $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...
60
            $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...
61
            $gtypes[$gt->id] = $t;
62
        }
63
64
        return view('cdc.index', [
65
            'cdcs' => $cdc,
66
            'gametypes' => $gtypes,
67
        ]);
68
    }
69
70
    /**
71
     * Show the form for creating a new resource.
72
     *
73
     * @return \Illuminate\Http\Response
74
     */
75
    public function create()
76
    {
77
        return view('cdc.create');
78
    }
79
80
    /**
81
     * Store a newly created resource in storage.
82
     *
83
     * @param  \Illuminate\Http\Request  $request
84
     * @return \Illuminate\Http\RedirectResponse
85
     */
86
    public function store(Request $request)
87
    {
88
        $this->validate($request, [
89
            'gamename' => 'required',
90
        ]);
91
92
        // Prüfen ob das Spiel auch wirklich existiert
93
        $title = explode(" -=- ", $request->get('gamename'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal -=- does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
94
        
95
        if (count($title) == 1) {
96
            $game = Game::whereTitle($title[0])
97
                ->first();
98
        } else {
99
            $game = Game::whereTitle($title[0])
1 ignored issue
show
Bug introduced by
The method orWhere does only exist in Illuminate\Database\Query\Builder, but not in App\Models\Game.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
                ->orWhere('subtitle', '=', $title[1])
101
                ->first();
102
        }
103
104
        \DB::table('games_coupdecoeur')->insert([
105
            'game_id' => $game->id,
106
            'user_id' => \Auth::id(),
107
            'created_at' => Carbon::now(),
108
        ]);
109
110
        return redirect()->action('MsgBoxController@cdc_add', [$game->id]);
111
    }
112
113
}
114