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

SingleEliminationTeamTreeGen::chunkAndShuffle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Xoco70\LaravelTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\LaravelTournaments\Models\Fight;
7
use Xoco70\LaravelTournaments\Models\FighterGroupTeam;
8
use Xoco70\LaravelTournaments\Models\FightersGroup;
9
use Xoco70\LaravelTournaments\Models\Team;
10
11 View Code Duplication
class SingleEliminationTeamTreeGen extends SingleEliminationTreeGen
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
12
{
13
    /**
14
     * get Fighter by Id.
15
     *
16
     * @return Team
17
     */
18
    protected function getFighter($teamId)
19
    {
20
        return Team::find($teamId);
21
    }
22
23
    /**
24
     * Fighter is the name for competitor or team, depending on the case.
25
     *
26
     * @return Collection
27
     */
28
    protected function getFighters()
29
    {
30
        return $this->championship->teams;
31
    }
32
33
    /**
34
     * @param FightersGroup $group
35
     * @param $fighters
36
     *
37
     * @return FightersGroup
38
     */
39
    public function syncGroup(FightersGroup $group, $fighters)
40
    {
41
        // Add all competitors to Pivot Table
42
        return $group->syncTeams($fighters);
43
    }
44
45
    protected function createByeFighter()
46
    {
47
        return new Team();
48
    }
49
50
    protected function addFighterToGroup(FightersGroup $group, $team)
51
    {
52
        $group->teams()->attach($team->id);
53
    }
54
    /**
55
     * Chunk Fighters into groups for fighting, and optionnaly shuffle.
56
     *
57
     * @param $fightersByEntity
58
     *
59
     * @return Collection|null
60
     */
61
    protected function chunkAndShuffle(Collection $fightersByEntity)
62
    {
63
        if ($this->championship->hasPreliminary()) {
64
            return (new PlayOffTeamTreeGen($this->championship, null))->chunkAndShuffle($fightersByEntity);
0 ignored issues
show
Bug introduced by
The method chunkAndShuffle() cannot be called from this context as it is declared protected in class Xoco70\LaravelTournaments\TreeGen\PlayOffTreeGen.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
65
        }
66
        $fightersGroup = null;
0 ignored issues
show
Unused Code introduced by
$fightersGroup 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...
67
68
        $fightersGroup = $fightersByEntity->chunk(2);
69
        if (!app()->runningUnitTests()) {
70
            $fightersGroup = $fightersGroup->shuffle();
71
        }
72
73
        return $fightersGroup;
74
    }
75
76
77
    /**
78
     * @param FightersGroup $group
79
     * @param Fight $fight
80
     * @param $oldTeams
81
     */
82
    protected function updateGroupFighters(FightersGroup $group, Fight $fight, $oldTeams)
83
    {
84
85
        for ($num = 0; $num <=1;$num++){
86
            $c1or2 = "c".($num+1);
87
            $fgc = FighterGroupTeam::where('fighters_group_id', $group->id)
88
                ->where('team_id', $oldTeams[$num])
89
                ->first();
90
            $fgc->team_id = $fight->$c1or2;
91
        }
92
        $fgc->save();
0 ignored issues
show
Bug introduced by
The variable $fgc does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
93
94
        // Get the round of this FGC
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
95
//        $numRound = $fgc->group->round;
96
//        $groupsIdInThisRound = $this->championship->groupsByRound($numRound)->pluck('id');
97
//        $count = FighterGroupCompetitor::whereIn('fighters_group_id', $groupsIdInThisRound)
98
//            ->where('competitor_id', $fgc->competitor_id)
99
//            ->count();
100
//
101
//        if ($count > 1) {
102
//            throw new DuplicatedFighterException(trans('laravel-tournaments::core.duplicated_fighter_in_group'));
103
//        }
104
    }
105
}
106