Completed
Push — master ( 26d986...9b7bb9 )
by Julien
09:29
created

SingleEliminationFight::saveFights()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 6
nop 2
1
<?php
2
3
namespace Xoco70\LaravelTournaments\Models;
4
5
class SingleEliminationFight extends Fight
6
{
7 View Code Duplication
    public function __construct(Fight $fight = null)
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...
8
    {
9
        parent::__construct();
10
        if ($fight != null) {
11
            $this->id = $fight->id;
12
            $this->short_id = $fight->short_id;
13
            $this->fighters_group_id = $fight->fighters_group_id;
14
            $this->c1 = $fight->c1;
15
            $this->c2 = $fight->c2;
16
        }
17
    }
18
19
    /**
20
     * @param Championship $championship
21
     */
22
    public static function saveFights(Championship $championship, $fromRound = 1)
23
    {
24
//        dd("generating fights");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
        $round = [];
26
        $groupsFromRound = $championship->groupsFromRound($fromRound)->get();
27
        foreach ($groupsFromRound as $group) {
28
            $fighters = parent::getFightersWithByes($group);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getFightersWithByes() instead of saveFights()). Are you sure this is correct? If so, you might want to change this to $this->getFightersWithByes().

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...
29
            $away = $fighters->splice(count($fighters) / 2); // 2
30
            $home = $fighters; // 1
31
32
            $countHome = count($home);
33
            $countAway = count($away);
34
            for ($i = 0; $i < $countHome + $countAway - 1; $i++) {
35
                for ($j = 0; $j < $countHome; $j++) {
36
                    $round[$i][$j]['Home'] = $home[$j];
37
                    $round[$i][$j]['Away'] = $away[$j];
38
                    $fight = new Fight();
39
                    $fight->fighters_group_id = $group->id;
40
                    $fight->c1 = $round[$i][$j]['Home']->id;
41
                    $fight->c2 = $round[$i][$j]['Away']->id;
42
                    $fight->area = $group->area;
43
                    $fight->save();
44
                }
45
                if ($countHome + $countAway - 1 > 2) {
46
                    $away->prepend($home->splice(1, 1)->shift());
47
                    $home->push($away->pop());
48
                }
49
            }
50
        }
51
    }
52
}
53