Passed
Push — master ( 24a4a4...d8896b )
by Julien
06:15
created

PlayOffTreeGen   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 16.35 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 17
loc 104
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getByeGroup() 8 8 1
A chunk() 0 9 2
A generateFights() 0 5 1
A pushGroups() 9 12 3
A getNumRounds() 0 4 1
A generateAllTrees() 0 18 3
A generateGroupsForRound() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Xoco70\LaravelTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException;
7
use Xoco70\LaravelTournaments\Models\SingleEliminationFight;
8
9
abstract class PlayOffTreeGen extends TreeGen
10
{
11
    /**
12
     * Calculate the Byes need to fill the Championship Tree.
13
     *
14
     * @param $fighters
15
     *
16
     * @return Collection
17
     */
18 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...
19
    {
20
        $fighterCount = $fighters->count();
21
        $treeSize = $this->getTreeSize($fighterCount, 2);
22
        $byeCount = $treeSize - $fighterCount;
23
24
        return $this->createByeGroup($byeCount);
25
    }
26
27
    /**
28
     * Chunk Fighters into groups for fighting, and optionnaly shuffle.
29
     *
30
     * @param $fightersByEntity
31
     *
32
     * @return mixed
33
     */
34
    protected function chunk(Collection $fightersByEntity)
35
    {
36
        if ($this->championship->hasPreliminary()) {
37
            $fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize);
38
            return $fightersGroup;
39
        }
40
41
        return $fightersByEntity->chunk($fightersByEntity->count());
42
    }
43
44
    /**
45
     * Generate First Round Fights.
46
     */
47
    public function generateFights()
48
    {
49
        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...
50
        SingleEliminationFight::saveFights($this->championship);
51
    }
52
53
    /**
54
     * Save Groups with their parent info.
55
     *
56
     * @param int $numRounds
57
     * @param $numFightersElim
58
     */
59
    protected function pushGroups($numRounds, $numFightersElim)
60
    {
61 View Code Duplication
        for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
            // From last match to first match
63
            $maxMatches = ($numFightersElim / pow(2, $roundNumber));
64
            for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) {
65
                $fighters = $this->createByeGroup(2);
66
                $group = $this->saveGroup($matchNumber, $roundNumber, null);
67
                $this->syncGroup($group, $fighters);
68
            }
69
        }
70
    }
71
72
    /**
73
     * Return number of rounds for the tree based on fighter count.
74
     *
75
     * @param $numFighters
76
     *
77
     * @return int
78
     */
79
    protected function getNumRounds($numFighters)
80
    {
81
        return intval(log($numFighters, 2));
82
    }
83
84
    /**
85
     * @throws TreeGenerationException
86
     */
87
    protected function generateAllTrees()
88
    {
89
        // TODO This is limiting Playoff only have 1 area
90
        $fighters = $this->championship->fighters;
0 ignored issues
show
Documentation introduced by
The property fighters does not exist on object<Xoco70\LaravelTou...ts\Models\Championship>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
        // This means that when playoff, we only generate 1 group
92
        // Could be better, for now it is ok
93
        $fighterType = $this->settings->isTeam
94
            ? trans_choice('laravel-tournaments::core.team', 2)
95
            : trans_choice('laravel-tournaments::core.competitor', 2);
96
97
        if (count($fighters) <= 1) {
98
            throw new TreeGenerationException(trans('laravel-tournaments::core.min_competitor_required', [
99
                'number'       => $this->settings->preliminaryGroupSize,
100
                'fighter_type' => $fighterType,
101
            ]), 422);
102
        }
103
        $this->generateGroupsForRound($fighters, 1);
104
    }
105
106
    protected function generateGroupsForRound(Collection $fightersByArea, $round)
107
    {
108
        $fightersId = $fightersByArea->pluck('id');
109
        $group = $this->saveGroup($round, $round, null);
110
        $this->syncGroup($group, $fightersId);
111
    }
112
}
113