Completed
Push — master ( 1b67ee...4e860c )
by Julien
02:07
created

TreeController::store()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 19
nc 2
nop 2
1
<?php
2
3
namespace Xoco70\KendoTournaments;
4
5
use Illuminate\Foundation\Auth\User;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Support\Facades\DB;
9
use Xoco70\KendoTournaments\Exceptions\TreeGenerationException;
10
use Xoco70\KendoTournaments\Models\Championship;
11
use Xoco70\KendoTournaments\Models\ChampionshipSettings;
12
use Xoco70\KendoTournaments\Models\Competitor;
13
use Xoco70\KendoTournaments\Models\FightersGroup;
14
use Xoco70\KendoTournaments\Models\Team;
15
use Xoco70\KendoTournaments\Models\Tournament;
16
17
class TreeController extends Controller
18
{
19
    /**
20
     * Display a listing of trees.
21
     *
22
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
23
     */
24
    public function index()
25
    {
26
        $tournament = Tournament::with(
0 ignored issues
show
Bug introduced by
The method first does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
27
            'competitors',
28
            'championshipSettings',
29
            'championships.settings',
30
            'championships.category')->first();
31
32
        return view('kendo-tournaments::tree.index')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
33
            ->with('tournament', $tournament)
34
            ->with('championship', $tournament->championships[0])
35
            ->with('settings', $tournament->championships[0]->setting);
36
    }
37
38
    /**
39
     * Build Tree.
40
     *
41
     * @param Request $request
42
     *
43
     * @return \Illuminate\Http\Response|string
44
     */
45
    public function store(Request $request, $championshipId)
0 ignored issues
show
Unused Code introduced by
The parameter $championshipId 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...
46
    {
47
        $this->deleteEverything();
48
        $numFighters = $request->numFighters;
49
        $isTeam = $request->isTeam ?? 0;
50
        $championship = $this->provisionObjects($request, $isTeam, $numFighters);
51
        $generation = $championship->chooseGenerationStrategy();
52
        try {
53
            $generation->run();
54
        } catch (TreeGenerationException $e) {
55
            redirect()->back()
56
                ->withErrors([$numFighters . "-" . $e->getMessage()]);
57
        }
58
59
        $tournament = Tournament::with(
0 ignored issues
show
Unused Code introduced by
$tournament 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...
Bug introduced by
The method first does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
60
            'competitors',
61
            'championshipSettings',
62
            'championships.settings',
63
            'championships.category')->first();
64
65
        return back()
66
            ->with('numFighters',$numFighters)
67
            ->with('isTeam',$isTeam);
68
69
    }
70
71
    private function deleteEverything()
72
    {
73
        DB::table('fight')->delete();
74
        DB::table('fighters_groups')->delete();
75
        DB::table('fighters_group_competitor')->delete();
76
        DB::table('fighters_group_team')->delete();
77
        DB::table('competitor')->delete();
78
        DB::table('team')->delete();
79
        DB::table('users')->where('id', '<>', 1)->delete();
80
    }
81
82
    /**
83
     * @param Request $request
84
     * @param $isTeam
85
     * @param $numFighters
86
     * @return Championship
87
     */
88
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
89
    {
90
        if ($isTeam) {
91
            $championship = Championship::find(2);
92
            factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
93
        } else {
94
            $championship = Championship::find(1);
95
            $users = factory(User::class, (int)$numFighters)->create();
96
            foreach ($users as $user) {
97
                factory(Competitor::class)->create(
98
                    ['championship_id' => $championship->id,
99
                        'user_id' => $user->id,
100
                        'confirmed' => 1,
101
                        'short_id' => $user->id
102
                    ]
103
                );
104
            }
105
        }
106
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
107
        return $championship;
108
    }
109
110
111
    /**
112
     * @param Request $request
113
     * @return \Illuminate\Http\RedirectResponse
114
     */
115
    public function update(Request $request, Championship $championship)
116
    {
117
        $numFight = 0;
118
        $query = FightersGroup::with('fights')
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
119
            ->where('championship_id', $championship->id);
120
121
        $fighters = $request->directElimination_fighters;
122
        if ($championship->hasPreliminary()) {
123
            $query = $query->where('round', '>', 1);
124
            $fighters = $request->preliminary_fighters;
125
        }
126
        $groups = $query->get();
127
128
        foreach ($groups as $group) {
129
            foreach ($group->fights as $fight) {
130
                // Find the fight in array, and update order
131
                $fight->c1 = $fighters[$numFight++];
132
                $fight->c2 = $fighters[$numFight++];
133
                $fight->save();
134
            }
135
        }
136
137
        return back();
138
    }
139
}
140