Test Failed
Branch master (46da36)
by Julien
03:07
created

TreeController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 10
Bugs 2 Features 1
Metric Value
wmc 11
c 10
b 2
f 1
lcom 2
cbo 10
dl 0
loc 116
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 1
A store() 0 18 2
A deleteEverything() 0 10 1
A provisionObjects() 0 21 3
B update() 0 24 4
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
        return back()
59
            ->with('numFighters', $numFighters)
60
            ->with('isTeam', $isTeam);
61
62
    }
63
64
    private function deleteEverything()
65
    {
66
        DB::table('fight')->delete();
67
        DB::table('fighters_groups')->delete();
68
        DB::table('fighters_group_competitor')->delete();
69
        DB::table('fighters_group_team')->delete();
70
        DB::table('competitor')->delete();
71
        DB::table('team')->delete();
72
        DB::table('users')->where('id', '<>', 1)->delete();
73
    }
74
75
    /**
76
     * @param Request $request
77
     * @param $isTeam
78
     * @param $numFighters
79
     * @return Championship
80
     */
81
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
82
    {
83
        if ($isTeam) {
84
            $championship = Championship::find(2);
85
            factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
86
        } else {
87
            $championship = Championship::find(1);
88
            $users = factory(User::class, (int)$numFighters)->create();
89
            foreach ($users as $user) {
90
                factory(Competitor::class)->create(
91
                    ['championship_id' => $championship->id,
92
                        'user_id' => $user->id,
93
                        'confirmed' => 1,
94
                        'short_id' => $user->id
95
                    ]
96
                );
97
            }
98
        }
99
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
100
        return $championship;
101
    }
102
103
104
    /**
105
     * @param Request $request
106
     * @return \Illuminate\Http\RedirectResponse
107
     */
108
    public function update(Request $request, Championship $championship)
109
    {
110
        $numFight = 0;
111
        $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...
112
            ->where('championship_id', $championship->id);
113
114
        $fighters = $request->directElimination_fighters;
115
        if ($championship->hasPreliminary()) {
116
            $query = $query->where('round', '>', 1);
117
            $fighters = $request->preliminary_fighters;
118
        }
119
        $groups = $query->get();
120
121
        foreach ($groups as $group) {
122
            foreach ($group->fights as $fight) {
123
                // Find the fight in array, and update order
124
                $fight->c1 = $fighters[$numFight++];
125
                $fight->c2 = $fighters[$numFight++];
126
                $fight->save();
127
            }
128
        }
129
130
        return back();
131
    }
132
}
133