Passed
Branch bigRefactor (c260a1)
by Julien
34:53
created

TreeController::store()   C

Complexity

Conditions 15
Paths 192

Size

Total Lines 56
Code Lines 39

Duplication

Lines 14
Ratio 25 %

Importance

Changes 0
Metric Value
dl 14
loc 56
rs 5.9829
c 0
b 0
f 0
cc 15
eloc 39
nc 192
nop 2

How to fix   Long Method    Complexity   

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\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\Tournament;
15
use Xoco70\KendoTournaments\TreeGen\DirectEliminationCompetitorTreeGen;
16
use Xoco70\KendoTournaments\TreeGen\DirectEliminationTeamTreeGen;
17
use Xoco70\KendoTournaments\TreeGen\PlayOffCompetitorTreeGen;
18
use Xoco70\KendoTournaments\TreeGen\PlayOffTeamTreeGen;
19
use Xoco70\KendoTournaments\TreeGen\TreeGen;
20
21
class TreeController extends Controller
22
{
23
    /**
24
     * Display a listing of trees.
25
     *
26
     * @param Request $request
27
     *
28
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
29
     */
30
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
31
    {
32
        $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...
33
            'competitors',
34
            'championshipSettings',
35
            'championships.settings',
36
            'championships.category')->first();
37
38
        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...
39
            ->with('tournament', $tournament)
40
            ->with('settings', $tournament->championships[0]->setting);
41
    }
42
43
    /**
44
     * Build Tree.
45
     *
46
     * @param Request $request
47
     * @param Championship $championship
48
     *
49
     * @return \Illuminate\Http\Response|string
50
     */
51
    public function store(Request $request, Championship $championship)
52
    {
53
        DB::table('fight')->delete();
54
        DB::table('fighters_groups')->delete();
55
        DB::table('fighters_group_competitor')->delete();
56
        DB::table('fighters_group_team')->delete();
57
        DB::table('competitor')->delete();
58
        DB::table('users')->where('id', '<>', 1)->delete();
59
60
        $championship = Championship::with('teams', 'users', 'category', 'settings')->find($championship->id);
0 ignored issues
show
Bug introduced by
The method find 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
        $numFighters = $request->numFighters;
62
63
        $users = factory(User::class, (int)$numFighters)->create();
64
65 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...
66
            factory(Competitor::class)->create([
67
                'championship_id' => $championship->id,
68
                'user_id' => $user->id,
69
                'confirmed' => 1,
70
                'short_id' => $user->id
71
            ]);
72
        }
73
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
74
75
        $generation = new TreeGen($championship, null);
76
        //TODO Set groupBy argument to NULL for now
77
        if ($championship->hasPreliminary() && $championship->category->isTeam()) {
78
            $generation = new PlayOffTeamTreeGen($championship, null);
79
        }
80
        if ($championship->hasPreliminary() && !$championship->category->isTeam()) {
81
            $generation = new PlayOffCompetitorTreeGen($championship, null);
82
        }
83 View Code Duplication
        if (!$championship->hasPreliminary() && $championship->isDirectEliminationType() && $championship->category->isTeam()) {
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...
84
            $generation = new DirectEliminationTeamTreeGen($championship, null);
85
        }
86 View Code Duplication
        if (!$championship->hasPreliminary() && $championship->isDirectEliminationType() && !$championship->category->isTeam()) {
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...
87
            $generation = new DirectEliminationCompetitorTreeGen($championship, null);
88
        }
89
90
        $fighterToUpdate = null;
0 ignored issues
show
Unused Code introduced by
$fighterToUpdate 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...
91
        try {
92
            $generation->run();
93
            FightersGroup::generateFights($championship);
94
            // For Now, We don't generate fights when Preliminary
95
            if ($championship->isDirectEliminationType() && !$championship->hasPreliminary()) {
96
                FightersGroup::generateNextRoundsFights($championship);
97
            }
98
        } catch (TreeGenerationException $e) {
99
            redirect()->back()
100
                ->withErrors([$numFighters . "-" . $e->getMessage()]);
101
        }
102
        return redirect()->back()
103
            ->with('numFighters', $numFighters)
104
            ->with('hasPreliminary', $championship->settings->hasPreliminary)
105
            ->with(['success', "Success"]);
106
    }
107
}
108