Test Failed
Branch master (46da36)
by Julien
03:07
created

DirectEliminationTreeGen::firstRoundGroupSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Xoco70\KendoTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\KendoTournaments\Models\DirectEliminationFight;
7
use Xoco70\KendoTournaments\Models\PreliminaryFight;
8
9
abstract class DirectEliminationTreeGen extends TreeGen
10
{
11
12
    /**
13
     * Calculate the Byes need to fill the Championship Tree.
14
     * @param $fighters
15
     * @return Collection
16
     */
17 View Code Duplication
    protected function getByeGroup($fighters)
0 ignored issues
show
Duplication introduced by
This method 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...
18
    {
19
        $fighterCount = $fighters->count();
20
        $firstRoundGroupSize = $this->firstRoundGroupSize();
21
        $treeSize = $this->getTreeSize($fighterCount, $firstRoundGroupSize);
22
        $byeCount = $treeSize - $fighterCount;
23
24
        return $this->createByeGroup($byeCount);
25
    }
26
27
    /**
28
     * Save Groups with their parent info
29
     * @param integer $numRounds
30
     * @param integer $numFighters
31
     */
32 View Code Duplication
    protected function pushGroups($numRounds, $numFighters)
0 ignored issues
show
Duplication introduced by
This method 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...
33
    {
34
        // TODO Here is where you should change when enable several winners for preliminary
35
        for ($roundNumber = 2; $roundNumber <= $numRounds + 1; $roundNumber++) {
36
            // From last match to first match
37
            $maxMatches = ($numFighters / pow(2, $roundNumber));
38
39
            for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) {
40
                $fighters = $this->createByeGroup(2);
41
                $group = $this->saveGroup($matchNumber, $roundNumber, null);
42
                $this->syncGroup($group, $fighters);
43
            }
44
        }
45
    }
46
47
    /**
48
     * Create empty groups for direct Elimination Tree
49
     * @param $numFighters
50
     */
51
    protected function pushEmptyGroupsToTree($numFighters)
52
    {
53
        if ($this->championship->hasPreliminary()) {
54
            $numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2;
55
            // We calculate how much rounds we will have
56
            $numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4)
57
            return $this->pushGroups($numRounds, $numFightersElim);
58
        }
59
        // We calculate how much rounds we will have
60
        $numRounds = $this->getNumRounds($numFighters);
61
        return $this->pushGroups($numRounds, $numFighters);
62
63
    }
64
65
    /**
66
     * Chunk Fighters into groups for fighting, and optionnaly shuffle
67
     * @param $fightersByEntity
68
     * @return Collection|null
69
     */
70
    protected function chunkAndShuffle(Collection $fightersByEntity)
71
    {
72
        //TODO Should Pull down to know if team or competitor
73
        if ($this->championship->hasPreliminary()) {
74
            return (new PlayOffCompetitorTreeGen($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\KendoTournaments\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...
75
        }
76
        $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...
77
78
        $fightersGroup = $fightersByEntity->chunk(2);
79
        if (!app()->runningUnitTests()) {
80
            $fightersGroup = $fightersGroup->shuffle();
81
        }
82
        return $fightersGroup;
83
    }
84
85
86
    /**
87
     * Generate First Round Fights
88
     */
89
    protected function generateFights()
90
    {
91
        //  First Round Fights
92
        $settings = $this->championship->getSettings();
93
        parent::destroyPreviousFights();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (destroyPreviousFights() instead of generateFights()). Are you sure this is correct? If so, you might want to change this to $this->destroyPreviousFights().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
94
        $groups = $this->championship->groupsByRound(1)->get();
95
        $initialRound = 1;
96
        // Very specific case to common case : Preliminary with 3 fighters
97
        if ($this->championship->hasPreliminary() && $settings->preliminaryGroupSize == 3) {
98
            // First we make all first fights of all groups
99
            // Then we make all second fights of all groups
100
            // Then we make all third fights of all groups
101
            for ($numFight = 1; $numFight <= $settings->preliminaryGroupSize; $numFight++) {
102
                $fight = new PreliminaryFight;
103
                $fight->saveFights($groups, $numFight);
104
            }
105
            $initialRound++;
106
        }
107
        // Save Next rounds
108
        $fight = new DirectEliminationFight;
109
        $fight->saveFights($this->championship, $initialRound);
110
    }
111
112
113
    /**
114
     * Return number of rounds for the tree based on fighter count
115
     * @param $numFighters
116
     * @return int
117
     */
118
    protected function getNumRounds($numFighters)
119
    {
120
        return intval(log($numFighters / $this->firstRoundGroupSize() * 2, 2));
121
    }
122
123
    private function firstRoundGroupSize()
124
    {
125
        return $this->championship->hasPreliminary()
126
            ? $this->championship->getSettings()->preliminaryGroupSize
127
            : 2;
128
    }
129
}
130