Completed
Push — master ( f65102...332511 )
by Julien
02:46
created

DirectEliminationTreeGen::hasDeterminedParent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
c 0
b 0
f 0
rs 9.2
cc 4
eloc 6
nc 4
nop 0
1
<?php
2
3
namespace Xoco70\KendoTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\App;
7
use Xoco70\KendoTournaments\Models\Championship;
8
use Xoco70\KendoTournaments\Models\DirectEliminationFight;
9
use Xoco70\KendoTournaments\Models\Fight;
10
11
class DirectEliminationTreeGen extends TreeGen
12
{
13
14
    /**
15
     * Calculate the Byes need to fill the Championship Tree.
16
     * @param Championship $championship
17
     * @param $fighters
18
     * @return Collection
19
     */
20
    protected function getByeGroup(Championship $championship, $fighters)
0 ignored issues
show
Unused Code introduced by
The parameter $championship is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        $fighterCount = $fighters->count();
23
        $treeSize = $this->getTreeSize($fighterCount, 2);
24
        $byeCount = $treeSize - $fighterCount;
25
26
        return $this->createNullsGroup($byeCount);
27
    }
28
29
30
    /**
31
     * Create empty groups for direct Elimination Tree
32
     * @param $numFightersElim
33
     */
34
    public function pushEmptyGroupsToTree($numFightersElim)
35
    {
36
        // We calculate how much rounds we will have
37
        $numRounds = intval(log($numFightersElim, 2));
38
        $this->pushGroups($numRounds, $numFightersElim);
39
    }
40
41
    /**
42
     * Chunk Fighters into groups for fighting, and optionnaly shuffle
43
     * @param $fightersByEntity
44
     * @return Collection|null
45
     */
46
    protected function chunkAndShuffle($round = null, $fightersByEntity)
0 ignored issues
show
Unused Code introduced by
The parameter $round is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        $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...
49
50
        $fightersGroup = $fightersByEntity->chunk(2);
51
        if (!App::runningUnitTests()) {
52
            $fightersGroup = $fightersGroup->shuffle();
53
        }
54
        return $fightersGroup;
55
    }
56
57
58
    /**
59
     * Generate First Round Fights
60
     */
61
    public function generateFights()
62
    {
63
        parent::destroyPreviousFights($this->championship);
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...
64
        // Very specific case to common case : Preliminary with 3 fighters
65
        DirectEliminationFight::saveFights($this->championship);
66
    }
67
    /**
68
     *
69
     */
70
    public function generateNextRoundsFights()
71
    {
72
        $championship = $this->championship->withCount('teams', 'competitors')->first();
73
        $fightersCount = $championship->competitors_count + $championship->teams_count;
74
        $maxRounds = intval(ceil(log($fightersCount, 2)));
75
        for ($numRound = 1; $numRound < $maxRounds; $numRound++) {
76
            $fightsByRound = $championship->fightsByRound($numRound)->with('group.parent', 'group.children')->get();
77
            $this->updateParentFight($championship, $fightsByRound);
78
        }
79
    }
80
81
    /**
82
     * @param Championship $championship
83
     * @param $fightsByRound
84
     */
85
    private function updateParentFight(Championship $championship, $fightsByRound)
86
    {
87
        foreach ($fightsByRound as $fight) {
88
            $parentGroup = $fight->group->parent;
89
            if ($parentGroup == null) break;
90
            $parentFight = $parentGroup->fights->get(0); //TODO This Might change when extending to Preliminary
91
92
            // IN this $fight, is c1 or c2 has the info?
93
            if ($championship->isDirectEliminationType()) {
94
                // determine whether c1 or c2 must be updated
95
                $this->chooseAndUpdateParentFight($fight, $parentFight);
96
            }
97
        }
98
    }
99
100
    /**
101
     * @param $fight
102
     * @param $parentFight
103
     */
104
    private function chooseAndUpdateParentFight($fight, $parentFight)
105
    {
106
        $fighterToUpdate = $fight->getParentFighterToUpdate();
107
        $valueToUpdate = $fight->getValueToUpdate();
108
        // we need to know if the child has empty fighters, is this BYE or undetermined
109
        if ($fight->hasDeterminedParent() && $valueToUpdate != null) {
110
            $parentFight->$fighterToUpdate = $fight->$valueToUpdate;
111
            $parentFight->save();
112
        }
113
    }
114
115
116
    /**
117
     * Returns the parent field that need to be updated
118
     * @return null|string
119
     */
120 View Code Duplication
    public function getParentFighterToUpdate()
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...
121
    {
122
        $childrenGroup = $this->group->parent->children;
0 ignored issues
show
Bug introduced by
The property group does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
123
        foreach ($childrenGroup as $key => $children) {
124
            $childFight = $children->fights->get(0);
125
            if ($childFight->id == $this->id) {
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
126
                if ($key % 2 == 0) {
127
                    return "c1";
128
                }
129
                if ($key % 2 == 1) {
130
                    return "c2";
131
                }
132
            }
133
        }
134
        return null;
135
    }
136
137
    /**
138
     * In the original fight ( child ) return the field that contains data to copy to parent
139
     * @return null|string
140
     */
141 View Code Duplication
    public function getValueToUpdate()
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...
142
    {
143
        if ($this->c1 != null && $this->c2 != null) {
0 ignored issues
show
Bug introduced by
The property c1 does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property c2 does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
144
            return null;
145
        }
146
        if ($this->c1 != null) {
147
            return "c1";
148
        }
149
        if ($this->c2 != null) {
150
            return "c2";
151
        }
152
        return null;
153
    }
154
155
    /**
156
     * Check if we are able to fill the parent fight or not
157
     * If one of the children has c1 x c2, then we must wait to fill parent
158
     *
159
     * @return bool
160
     */
161 View Code Duplication
    public function hasDeterminedParent()
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...
162
    {
163
        if ($this->group->has2Fighters()) return true;
164
        foreach ($this->group->children as $child) {
165
            $fight = $child->fights->get(0);
166
            if ($fight->has2Fighters()) return false;
167
        }
168
        return true;
169
    }
170
}
171