Completed
Push — master ( 17e0da...c0bdc3 )
by Julien
07:40
created

PlayOffTreeGen::generateAllTrees()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Xoco70\LaravelTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\LaravelTournaments\Models\DirectEliminationFight;
7
8
abstract class PlayOffTreeGen extends TreeGen
9
{
10
    /**
11
     * Calculate the Byes need to fill the Championship Tree.
12
     *
13
     * @param $fighters
14
     *
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
        $treeSize = $this->getTreeSize($fighterCount, 2);
21
        $byeCount = $treeSize - $fighterCount;
22
23
        return $this->createByeGroup($byeCount);
24
    }
25
26
    /**
27
     * Create empty groups for PlayOff Round.
28
     *
29
     * @param $numFighters
30
     */
31
    protected function pushEmptyGroupsToTree($numFighters)
32
    {
33
        //TODO CHANGE HERE TOO
34
        $numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2;
35
        // We calculate how much rounds we will have
36
        $numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4)
37
        $this->pushGroups($numRounds, $numFightersElim);
38
    }
39
40
    /**
41
     * Chunk Fighters into groups for fighting, and optionnaly shuffle.
42
     *
43
     * @param $fightersByEntity
44
     *
45
     * @return mixed
46
     */
47
    protected function chunkAndShuffle(Collection $fightersByEntity)
48
    {
49
        if ($this->championship->hasPreliminary()) {
50
            $fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize);
51
            if (!app()->runningUnitTests()) {
52
                $fightersGroup = $fightersGroup->shuffle();
53
            }
54
55
            return $fightersGroup;
56
        }
57
58
        return $fightersByEntity->chunk($fightersByEntity->count());
59
    }
60
61
    /**
62
     * Generate First Round Fights.
63
     */
64
    public function generateFights()
65
    {
66
        parent::destroyPreviousFights($this->championship);
0 ignored issues
show
Unused Code introduced by
The call to TreeGen::destroyPreviousFights() has too many arguments starting with $this->championship.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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...
67
        DirectEliminationFight::saveFights($this->championship);
68
    }
69
70
    /**
71
     * Save Groups with their parent info.
72
     *
73
     * @param int $numRounds
74
     * @param $numFightersElim
75
     */
76 View Code Duplication
    protected function pushGroups($numRounds, $numFightersElim)
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...
77
    {
78
        for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) {
79
            // From last match to first match
80
            $maxMatches = ($numFightersElim / pow(2, $roundNumber));
81
            for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) {
82
                $fighters = $this->createByeGroup(2);
83
                $group = $this->saveGroup($matchNumber, $roundNumber, null);
84
                $this->syncGroup($group, $fighters);
85
            }
86
        }
87
    }
88
89
    /**
90
     * Return number of rounds for the tree based on fighter count.
91
     *
92
     * @param $numFighters
93
     *
94
     * @return int
95
     */
96
    protected function getNumRounds($numFighters)
97
    {
98
        return intval(log($numFighters, 2));
99
    }
100
101
    protected function generateAllTrees()
102
    {
103
        // TODO This is limiting Playoff only have 1 area
104
        $fighters = $this->championship->fighters;
105
        // This means that when playoff, we only generate 1 group
106
        // Could be better, for now it is ok
107
        $this->generateGroupsForRound($fighters, 1);
108
109
    }
110
}
111