Completed
Push — master ( 45fc90...4a35b2 )
by Julien
08:41
created

PlayOffTreeGen::pushEmptyGroupsToTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Xoco70\LaravelTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\LaravelTournaments\Models\SingleEliminationFight;
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
28
    /**
29
     * Chunk Fighters into groups for fighting, and optionnaly shuffle.
30
     *
31
     * @param $fightersByEntity
32
     *
33
     * @return mixed
34
     */
35
    protected function chunkAndShuffle(Collection $fightersByEntity)
36
    {
37
        if ($this->championship->hasPreliminary()) {
38
            $fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize);
39
            if (!app()->runningUnitTests()) {
40
                $fightersGroup = $fightersGroup->shuffle();
41
            }
42
43
            return $fightersGroup;
44
        }
45
46
        return $fightersByEntity->chunk($fightersByEntity->count());
47
    }
48
49
    /**
50
     * Generate First Round Fights.
51
     */
52
    public function generateFights()
53
    {
54
        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...
55
        SingleEliminationFight::saveFights($this->championship);
56
    }
57
58
    /**
59
     * Save Groups with their parent info.
60
     *
61
     * @param int $numRounds
62
     * @param $numFightersElim
63
     */
64 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...
65
    {
66
        for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) {
67
            // From last match to first match
68
            $maxMatches = ($numFightersElim / pow(2, $roundNumber));
69
            for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) {
70
                $fighters = $this->createByeGroup(2);
71
                $group = $this->saveGroup($matchNumber, $roundNumber, null);
72
                $this->syncGroup($group, $fighters);
73
            }
74
        }
75
    }
76
77
    /**
78
     * Return number of rounds for the tree based on fighter count.
79
     *
80
     * @param $numFighters
81
     *
82
     * @return int
83
     */
84
    protected function getNumRounds($numFighters)
85
    {
86
        return intval(log($numFighters, 2));
87
    }
88
89
    protected function generateAllTrees()
90
    {
91
        // TODO This is limiting Playoff only have 1 area
92
        $fighters = $this->championship->fighters;
93
        // This means that when playoff, we only generate 1 group
94
        // Could be better, for now it is ok
95
        if (sizeof($fighters) > 0) {
96
            $this->generateGroupsForRound($fighters, 1);
97
        }
98
99
    }
100
101
    protected function generateGroupsForRound(Collection $fightersByArea, $round)
102
    {
103
        $fightersId = $fightersByArea->pluck('id');
104
        $group = $this->saveGroup($round, $round, null);
105
        $this->syncGroup($group, $fightersId);
106
    }
107
}
108