Completed
Push — master ( c4c906...4cafd2 )
by Julien
02:54
created

TreeController::store()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 18
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\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...
61
            'competitors',
62
            'championshipSettings',
63
            'championships.settings',
64
            'championships.category')->first();
65
66
        return view('kendo-tournaments::tree.index',
67
            compact('tournament', 'championship', 'numFighters', '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, $championshipId)
116
    {
117
        $numFight = 0;
118
//        $championshipId = $request->championshipId;
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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