Completed
Push — master ( 54ddf1...ef6d8d )
by Julien
02:51
created

TreeController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
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\Fight;
14
use Xoco70\KendoTournaments\Models\FightersGroup;
15
use Xoco70\KendoTournaments\Models\Team;
16
use Xoco70\KendoTournaments\Models\Tournament;
17
18
class TreeController extends Controller
19
{
20
    /**
21
     * Display a listing of trees.
22
     *
23
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
24
     */
25
    public function index()
26
    {
27
        $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...
28
            'competitors',
29
            'championshipSettings',
30
            'championships.settings',
31
            'championships.category')->first();
32
33
        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...
34
            ->with('tournament', $tournament)
35
            ->with('championship', $tournament->championships[0])
36
            ->with('settings', $tournament->championships[0]->setting);
37
    }
38
39
    /**
40
     * Build Tree.
41
     *
42
     * @param Request $request
43
     *
44
     * @return \Illuminate\Http\Response|string
45
     */
46
    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...
47
    {
48
        $this->deleteEverything();
49
        $numFighters = $request->numFighters;
50
        $isTeam = $request->isTeam ?? 0;
51
        $championship = $this->provisionObjects($request, $isTeam, $numFighters);
52
        $generation = $championship->chooseGenerationStrategy();
53
        try {
54
            $generation->run();
55
        } catch (TreeGenerationException $e) {
56
            redirect()->back()
57
                ->withErrors([$numFighters . "-" . $e->getMessage()]);
58
        }
59
60
        $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...
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...
61
            'competitors',
62
            'championshipSettings',
63
            'championships.settings',
64
            'championships.category')->first();
65
66
        return back();
67
68
    }
69
70
    private function deleteEverything()
71
    {
72
        DB::table('fight')->delete();
73
        DB::table('fighters_groups')->delete();
74
        DB::table('fighters_group_competitor')->delete();
75
        DB::table('fighters_group_team')->delete();
76
        DB::table('competitor')->delete();
77
        DB::table('team')->delete();
78
        DB::table('users')->where('id', '<>', 1)->delete();
79
    }
80
81
    /**
82
     * @param Request $request
83
     * @param $isTeam
84
     * @param $numFighters
85
     * @return Championship
86
     */
87
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
88
    {
89
        if ($isTeam) {
90
            $championship = Championship::find(2);
91
            factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
92
        } else {
93
            $championship = Championship::find(1);
94
            $users = factory(User::class, (int)$numFighters)->create();
95
            foreach ($users as $user) {
96
                factory(Competitor::class)->create(
97
                    ['championship_id' => $championship->id,
98
                        'user_id' => $user->id,
99
                        'confirmed' => 1,
100
                        'short_id' => $user->id
101
                    ]
102
                );
103
            }
104
        }
105
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
106
        return $championship;
107
    }
108
109
110
    /**
111
     * @param Request $request
112
     * @return \Illuminate\Http\RedirectResponse
113
     */
114
    public function update(Request $request, Championship $championship)
115
    {
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
        if ($championship->hasPreliminary()){
122
            $query = $query->where('round','>',1);
123
        }
124
        $groups = $query->get();
125
        $fights = $request->fights;
126
127
        foreach ($groups as $group) {
128
            foreach ($group->fights as $fight) {
129
                // Find the fight in array, and update order
130
                $fight->c1 = $fights[$numFight++];
131
                $fight->c2 = $fights[$numFight++];
132
                $fight->save();
133
            }
134
        }
135
136
        return back();
137
    }
138
}
139