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

PlayOffTreeGen::generateFights()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Xoco70\KendoTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\KendoTournaments\Models\DirectEliminationFight;
7
8
9
abstract class PlayOffTreeGen extends TreeGen
10
{
11
    /**
12
     * Calculate the Byes need to fill the Championship Tree.
13
     * @param $fighters
14
     * @return Collection
15
     */
16 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...
17
    {
18
        $fighterCount = $fighters->count();
19
        $treeSize = $this->getTreeSize($fighterCount, 2);
20
        $byeCount = $treeSize - $fighterCount;
21
22
        return $this->createByeGroup($byeCount);
23
    }
24
25
26
    /**
27
     * Create empty groups for PlayOff Round
28
     * @param $numFighters
29
     */
30
    protected function pushEmptyGroupsToTree($numFighters)
31
    {
32
        //TODO CHANGE HERE TOO
33
        $numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2;
34
        // We calculate how much rounds we will have
35
        $numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4)
36
        $this->pushGroups($numRounds, $numFightersElim);
37
    }
38
39
    /**
40
     * Chunk Fighters into groups for fighting, and optionnaly shuffle
41
     * @param $fightersByEntity
42
     * @return mixed
43
     */
44
    protected function chunkAndShuffle(Collection $fightersByEntity)
45
    {
46
        if ($this->championship->hasPreliminary()) {
47
48
            $fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize);
49
            if (!app()->runningUnitTests()) {
50
                $fightersGroup = $fightersGroup->shuffle();
51
            }
52
            return $fightersGroup;
53
        }
54
        return $fightersByEntity->chunk($fightersByEntity->count());
55
    }
56
57
58
    /**
59
     * Generate First Round Fights
60
     */
61
    public function generateFights()
62
    {
63
        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...
64
        DirectEliminationFight::saveFights($this->championship);
65
    }
66
67
68
    /**
69
     * Save Groups with their parent info
70
     * @param integer $numRounds
71
     * @param $numFightersElim
72
     */
73 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...
74
    {
75
        for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) {
76
            // From last match to first match
77
            $maxMatches = ($numFightersElim / pow(2, $roundNumber));
78
            for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) {
79
                $fighters = $this->createByeGroup(2);
80
                $group = $this->saveGroup($matchNumber, $roundNumber, null);
81
                $this->syncGroup($group, $fighters);
82
            }
83
        }
84
    }
85
86
    /**
87
     * Return number of rounds for the tree based on fighter count
88
     * @param $numFighters
89
     * @return int
90
     */
91
    protected function getNumRounds($numFighters)
92
    {
93
        return intval(log($numFighters, 2));
94
    }
95
96
}
97