Passed
Push — master ( 3dfec8...77351e )
by Julien
06:18
created

TreeController::update()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 30
rs 8.439
cc 5
eloc 21
nc 8
nop 2
1
<?php
2
3
namespace Xoco70\LaravelTournaments;
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\LaravelTournaments\Exceptions\TreeGenerationException;
10
use Xoco70\LaravelTournaments\Models\Championship;
11
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
12
use Xoco70\LaravelTournaments\Models\Competitor;
13
use Xoco70\LaravelTournaments\Models\FightersGroup;
14
use Xoco70\LaravelTournaments\Models\Team;
15
use Xoco70\LaravelTournaments\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
            'championships.settings',
29
            'championships.category')->first();
30
31
        return view('laravel-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...
32
            ->with('tournament', $tournament);
33
    }
34
35
    /**
36
     * Build Tree.
37
     *
38
     * @param Request $request
39
     *
40
     * @return \Illuminate\Http\Response|string
41
     */
42
    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...
43
    {
44
        $this->deleteEverything();
45
        $numFighters = $request->numFighters;
46
        $isTeam = $request->isTeam ?? 0;
47
        $championship = $this->provisionObjects($request, $isTeam, $numFighters);
48
        $generation = $championship->chooseGenerationStrategy();
49
50
        try {
51
            $generation->run();
52
        } catch (TreeGenerationException $e) {
53
            redirect()->back()
54
                ->withErrors($e->getMessage());
55
        }
56
57
        return back()
58
            ->with('numFighters', $numFighters)
59
            ->with('isTeam', $isTeam);
60
    }
61
62
    private function deleteEverything()
63
    {
64
        DB::table('fight')->delete();
65
        DB::table('fighters_groups')->delete();
66
        DB::table('fighters_group_competitor')->delete();
67
        DB::table('fighters_group_team')->delete();
68
        DB::table('competitor')->delete();
69
        DB::table('team')->delete();
70
        DB::table('users')->where('id', '<>', 1)->delete();
71
    }
72
73
    /**
74
     * @param Request $request
75
     * @param $isTeam
76
     * @param $numFighters
77
     *
78
     * @return Championship
79
     */
80
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
81
    {
82
        if ($isTeam) {
83
            $championship = Championship::find(2);
84
            factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
85
        } else {
86
            $championship = Championship::find(1);
87
            $users = factory(User::class, (int)$numFighters)->create();
88 View Code Duplication
            foreach ($users as $user) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
                factory(Competitor::class)->create(
90
                    ['championship_id' => $championship->id,
91
                        'user_id' => $user->id,
92
                        'confirmed' => 1,
93
                        'short_id' => $user->id,
94
                    ]
95
                );
96
            }
97
        }
98
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
99
100
        return $championship;
101
    }
102
103
    /**
104
     * @param Request $request
105
     *
106
     * @return \Illuminate\Http\RedirectResponse
107
     */
108
    public function update(Request $request, Championship $championship)
109
    {
110
        $numFighter = 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->singleElimination_fighters;
115
        $scores = $request->score;
116
        if ($championship->hasPreliminary()) {
117
            $query = $query->where('round', '>', 1);
118
            $fighters = $request->preliminary_fighters;
119
        }
120
        $groups = $query->get();
121
122
        foreach ($groups as $group) {
123
            foreach ($group->fights as $fight) {
124
                $fight->c1 = $fighters[$numFighter];
125
                $fight->winner_id = $this->getWinnerId($fighters, $scores, $numFighter);
126
                $numFighter++;
127
128
                $fight->c2 = $fighters[$numFighter];
129
                if ($fight->winner_id == null) {
130
                    $fight->winner_id = $this->getWinnerId($fighters, $scores, $numFighter);
131
                }
132
                $numFighter++;
133
                $fight->save();
134
            }
135
        }
136
        return back();
137
    }
138
139
140
    function getWinnerId($fighters, $scores, $numFighter)
141
    {
142
        return $scores[$numFighter] != null ? $fighters[$numFighter] : null;
143
    }
144
}
145