Passed
Branch develop (f993a2)
by Julien
06:23
created

TreeController::update()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
dl 0
loc 81
c 4
b 1
f 2
rs 8.8076
cc 1
eloc 5
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Xoco70\LaravelTournaments;
4
5
use Illuminate\Database\QueryException;
6
use Illuminate\Foundation\Auth\User;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Support\Facades\DB;
10
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException;
11
use Xoco70\LaravelTournaments\Models\Championship;
12
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
13
use Xoco70\LaravelTournaments\Models\Competitor;
14
use Xoco70\LaravelTournaments\Models\FighterGroupCompetitor;
15
use Xoco70\LaravelTournaments\Models\FighterGroupTeam;
16
use Xoco70\LaravelTournaments\Models\FightersGroup;
17
use Xoco70\LaravelTournaments\Models\Team;
18
use Xoco70\LaravelTournaments\Models\Tournament;
19
20
class TreeController extends Controller
21
{
22
    /**
23
     * Display a listing of trees.
24
     *
25
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
26
     */
27
    public function index()
28
    {
29
        $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...
30
            'competitors',
31
            'championships.settings',
32
            'championships.category')->first();
33
34
        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...
35
            ->with('tournament', $tournament);
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
53
        try {
54
            $generation->run();
55
        } catch (TreeGenerationException $e) {
56
            redirect()->back()
57
                ->withErrors($e->getMessage());
58
        }
59
60
        return back()
61
            ->with('numFighters', $numFighters)
62
            ->with('isTeam', $isTeam);
63
    }
64
65
    private function deleteEverything()
66
    {
67
        DB::table('fight')->delete();
68
        DB::table('fighters_groups')->delete();
69
        DB::table('fighters_group_competitor')->delete();
70
        DB::table('fighters_group_team')->delete();
71
        DB::table('competitor')->delete();
72
        DB::table('team')->delete();
73
        DB::table('users')->where('id', '<>', 1)->delete();
74
    }
75
76
    /**
77
     * @param Request $request
78
     * @param $isTeam
79
     * @param $numFighters
80
     *
81
     * @return Championship
82
     */
83
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
84
    {
85
        if ($isTeam) {
86
            $championship = Championship::find(2);
87
            factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
88
        } else {
89
            $championship = Championship::find(1);
90
            $users = factory(User::class, (int)$numFighters)->create();
91 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...
92
                factory(Competitor::class)->create(
93
                    ['championship_id' => $championship->id,
94
                        'user_id' => $user->id,
95
                        'confirmed' => 1,
96
                        'short_id' => $user->id,
97
                    ]
98
                );
99
            }
100
        }
101
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
102
103
        return $championship;
104
    }
105
106
    /**
107
     * @param Request $request
108
     *
109
     * @return \Illuminate\Http\RedirectResponse
110
     */
111
    public function update(Request $request, Championship $championship)
112
    {
113
        $tree = $championship->chooseGenerationStrategy();
114
        $fighters = $request->fighters;
0 ignored issues
show
Unused Code introduced by
$fighters 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...
115
        $tree->update($request);
116
117
//        $query = FightersGroup::with('fights')
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
118
//            ->where('championship_id', $championship->id);
119
//
120
//        $numFighter = 0;
121
//
122
//        $scores = $request->score;
123
//        if ($championship->hasPreliminary()) {
124
//            $query = $query->where('round', '>', 1);
125
//            $fighters = $request->preliminary_fighters;
126
//        }
127
//        $groups = $query->get();
128
129
//        foreach ($groups as $group) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
130
//            foreach ($group->fights as $fight) {
131
//                // Find the fight in array, and update order
132
//                $oldC1 = $fight->c1;
133
//                $oldC2 = $fight->c2;
134
//                $fight->c1 = $fighters[$numFighter];
135
//                $scores[$numFighter] != null
136
//                    ? $fight->winner_id = $fighters[$numFighter]
137
//                    : $fight->winner_id = null;
138
//                $numFighter++;
139
//
140
//                $fight->c2 = $fighters[$numFighter];
141
//                if ($fight->winner_id == null) {
142
//                    $scores[$numFighter] != null
143
//                        ? $fight->winner_id = $fighters[$numFighter]
144
//                        : $fight->winner_id = null;
145
//                }
146
//
147
//
148
//                $numFighter++;
149
//                $fight->save();
150
//
151
//
152
//                // Update group and group->competitor / group->team
153
//                if ($championship->isSingleEliminationCompetitor()) {
154
//                    try {
155
//
156
//                    } catch (QueryException $exception) {
157
//
158
//                    }
159
//                    $fgc = FighterGroupCompetitor::where('fighters_group_id', $group->id)
160
//                        ->where('competitor_id', $oldC1)
161
//                        ->first();
162
//
163
//                    $fgc->competitor_id = $fight->c1;
164
//                    $fgc->save();
165
//
166
//                    $fgc = FighterGroupCompetitor::where('fighters_group_id', $group->id)
167
//                        ->where('competitor_id', $oldC2)
168
//                        ->first();
169
//
170
//                    $fgc->competitor_id = $fight->c2;
171
//                    $fgc->save();
172
//                }
173
//                if ($championship->isSingleEliminationTeam()) {
174
//                    $fgt = FighterGroupTeam::where('fighters_group_id', $group->id)
175
//                        ->where('team_id', $oldC1)
176
//                        ->first();
177
//                    $fgt->team_id = $fight->c1;
178
//                    $fgt->save();
179
//
180
//                    $fgt = FighterGroupTeam::where('fighters_group_id', $group->id)
181
//                        ->where('team_id', $oldC2)
182
//                        ->first();
183
////                    dd($group->id,$oldC2);
184
//                    $fgt->team_id = $fight->c2;
185
//                    $fgt->save();
186
//
187
//                }
188
//            }
189
//        }
190
        return back();
191
    }
192
}
193