Test Failed
Push — master ( 5f4570...241073 )
by Julien
02:51
created

TreeController::store()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 48
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
cc 2
eloc 21
nc 3
nop 2
1
<?php
2
3
namespace Xoco70\KendoTournaments;
4
5
use App\Http\Controllers\Controller;
6
use App\User;
7
use Illuminate\Http\Request;
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\Tournament;
15
use Xoco70\KendoTournaments\Models\Round;
16
use Xoco70\KendoTournaments\TreeGen\TreeGen;
17
use Faker\Factory as Faker;
18
19
class TreeController extends Controller
20
{
21
    /**
22
     * Display a listing of trees.
23
     *
24
     * @param Request $request
25
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
26
     */
27
    public function index(Request $request)
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
            'championshipSettings',
32
            'championships.settings',
33
            'championships.category')->first();
34
35
        return view('kendo-tournaments::tree.index')
36
            ->with('tournament', $tournament);
37
    }
38
39
    /**
40
     * Build Tree
41
     *
42
     * @param Request $request
43
     * @param Championship $championship
44
     * @return \Illuminate\Http\Response|string
45
     */
46
    public function store(Request $request, Championship $championship)
47
    {
48
        // Store Championship Settings
49
50
//        DB::table('competitor')->delete();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
51
52
        $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...
53
            'competitors',
54
            'championshipSettings'
55
        )->first();
56
57
        $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...
58
59
        $numFighters = $request->numFighters;
0 ignored issues
show
Unused Code introduced by
$numFighters 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...
60
61
//        $users = factory(User::class, (int)$numFighters)->create();
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
62
//
63
//        foreach ($users as $user) {
64
//            factory(Competitor::class)->create([
65
//                'championship_id' => $championship->id,
66
//                'user_id' => $user->id,
67
//                'confirmed' => 1,
68
//            ]);
69
//        }
70
71
        $settings = ChampionshipSettings::createOrUpdate($request, $championship);
72
73
74
        $generation = new TreeGen($championship, null, $settings);
75
        $generation->championship = $championship;
76
        try {
77
78
            $tree = $generation->run();
79
            $championship->tree = $tree;
80
81
82
            Round::generateFights($tree, $settings, $championship);
83
84
        } catch (TreeGenerationException $e) {
85
            return view('kendo-tournaments::tree.index')
86
                ->with('tournament', $tournament)
87
                ->with('error', "Error Generating Tree");
88
        }
89
90
        return view('kendo-tournaments::tree.index')
91
            ->with('tournament', $tournament)
92
            ->with('message', "Tree Generated");
93
    }
94
95
96
}
97