Passed
Push — master ( d98836...b20032 )
by Julien
31:32
created

TreeController::chooseGenerationStrategy()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 5
eloc 18
nc 5
nop 1
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\Contracts\TreeGenerable;
10
use Xoco70\KendoTournaments\Exceptions\TreeGenerationException;
11
use Xoco70\KendoTournaments\Models\Championship;
12
use Xoco70\KendoTournaments\Models\ChampionshipSettings;
13
use Xoco70\KendoTournaments\Models\Competitor;
14
use Xoco70\KendoTournaments\Models\FightersGroup;
15
use Xoco70\KendoTournaments\Models\Tournament;
16
use Xoco70\KendoTournaments\TreeGen\DirectEliminationCompetitorTreeGen;
17
use Xoco70\KendoTournaments\TreeGen\DirectEliminationTeamTreeGen;
18
use Xoco70\KendoTournaments\TreeGen\PlayOffCompetitorTreeGen;
19
use Xoco70\KendoTournaments\TreeGen\PlayOffTeamTreeGen;
20
use Xoco70\KendoTournaments\TreeGen\TreeGen;
21
22
class TreeController extends Controller
23
{
24
    /**
25
     * Display a listing of trees.
26
     *
27
     * @param Request $request
28
     *
29
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
30
     */
31
    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...
32
    {
33
        $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...
34
            'competitors',
35
            'championshipSettings',
36
            'championships.settings',
37
            'championships.category')->first();
38
39
        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...
40
            ->with('tournament', $tournament)
41
            ->with('settings', $tournament->championships[0]->setting);
42
    }
43
44
    /**
45
     * Build Tree.
46
     *
47
     * @param Request $request
48
     * @param Championship $championship
49
     *
50
     * @return \Illuminate\Http\Response|string
51
     */
52
    public function store(Request $request, Championship $championship)
53
    {
54
        DB::table('fight')->delete();
55
        DB::table('fighters_groups')->delete();
56
        DB::table('fighters_group_competitor')->delete();
57
        DB::table('fighters_group_team')->delete();
58
        DB::table('competitor')->delete();
59
        DB::table('users')->where('id', '<>', 1)->delete();
60
61
        $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...
62
        $numFighters = $request->numFighters;
63
64
        $users = factory(User::class, (int)$numFighters)->create();
65
66 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...
67
            factory(Competitor::class)->create([
68
                'championship_id' => $championship->id,
69
                'user_id' => $user->id,
70
                'confirmed' => 1,
71
                'short_id' => $user->id
72
            ]);
73
        }
74
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
75
        $generation = $championship->chooseGenerationStrategy();
76
77
        $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...
78
        try {
79
            $generation->run();
80
            FightersGroup::generateFights($championship);
81
            // For Now, We don't generate fights when Preliminary
82
            if ($championship->isDirectEliminationType() && !$championship->hasPreliminary()) {
83
                FightersGroup::generateNextRoundsFights($championship);
84
            }
85
        } catch (TreeGenerationException $e) {
86
            redirect()->back()
87
                ->withErrors([$numFighters . "-" . $e->getMessage()]);
88
        }
89
        return redirect()->back()
90
            ->with('numFighters', $numFighters)
91
            ->with('hasPreliminary', $championship->settings->hasPreliminary)
92
            ->with(['success', "Success"]);
93
    }
94
95
96
}
97