Completed
Push — master ( 0a3a64...75d382 )
by Julien
02:47
created

DirectEliminationTreeGen::updateParentFight()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.2
cc 4
eloc 7
nc 4
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A DirectEliminationTreeGen::getNumRounds() 0 4 1
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 $fighters
17
     * @return Collection
18
     */
19
    protected function getByeGroup($fighters)
20
    {
21
        $fighterCount = $fighters->count();
22
        $treeSize = $this->getTreeSize($fighterCount, 2);
23
        $byeCount = $treeSize - $fighterCount;
24
25
        return $this->createNullsGroup($byeCount);
26
    }
27
28
29
    /**
30
     * Create empty groups for direct Elimination Tree
31
     * @param $numFightersElim
32
     */
33
    public function pushEmptyGroupsToTree($numFightersElim)
34
    {
35
        // We calculate how much rounds we will have
36
        $numRounds = $this->getNumRounds($numFightersElim);
37
        $this->pushGroups($numRounds, $numFightersElim);
38
    }
39
40
    /**
41
     * Chunk Fighters into groups for fighting, and optionnaly shuffle
42
     * @param $fightersByEntity
43
     * @return Collection|null
44
     */
45
    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...
46
    {
47
        $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...
48
49
        $fightersGroup = $fightersByEntity->chunk(2);
50
        if (!App::runningUnitTests()) {
51
            $fightersGroup = $fightersGroup->shuffle();
52
        }
53
        return $fightersGroup;
54
    }
55
56
57
    /**
58
     * Generate First Round Fights
59
     */
60
    public function generateFights()
61
    {
62
        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...
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...
63
        DirectEliminationFight::saveFights($this->championship);
64
    }
65
66
67
    /**
68
     * Save Groups with their parent info
69
     * @param integer $numRounds
70
     * @param $numFightersElim
71
     */
72 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...
73
    {
74
        for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) {
75
            // From last match to first match
76
            $maxMatches = ($numFightersElim / pow(2, $roundNumber));
77
78
            for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) {
79
                $fighters = $this->createByeGroup(2);
80
                $group = $this->saveGroup(1, $matchNumber, $roundNumber, null);
81
                $this->syncGroup($group, $fighters);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xoco70\KendoTournaments\...irectEliminationTreeGen as the method syncGroup() does only exist in the following sub-classes of Xoco70\KendoTournaments\...irectEliminationTreeGen: Xoco70\KendoTournaments\...nationCompetitorTreeGen, Xoco70\KendoTournaments\...tEliminationTeamTreeGen. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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
    public function getNumRounds($numFighters)
92
    {
93
        return intval(log($numFighters, 2));
94
    }
95
}
96