Test Failed
Push — master ( fd8602...cf8b33 )
by Julien
03:21
created

Fight::savePreliminaryFightRound()   C

Complexity

Conditions 8
Paths 33

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 5.3846
cc 8
eloc 31
nc 33
nop 2
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
class Fight extends Model
9
{
10
    /**
11
     * Fight constructor.
12
     */
13
    public function __construct($userId1 = null, $userId2 = null)
14
    {
15
        $this->c1 = $userId1;
16
        $this->c2 = $userId2;
17
    }
18
19
    protected $table = 'fight';
20
    public $timestamps = true;
21
22
    protected $fillable = [
23
        'round_id',
24
        'c1',
25
        'c2',
26
    ];
27
28
    /**
29
     * @param Championship $championship
30
     *
31
     * @return mixed
32
     */
33
    private static function getActorsToFights(Championship $championship, Round $round = null)
34
    {
35
        if ($championship->category->isTeam) {
36
            $fighters = $round->teams;
37
            if (count($fighters) % 2 != 0) {
38
                $fighters->push(new Team(['name' => 'BYE']));
39
            }
40
        } else {
41
            $fighters = $round->competitors;
42
            if (count($fighters) % 2 != 0) {
43
                $fighters->push(new Competitor());
44
            }
45
        }
46
47
        return $fighters;
48
    }
49
50
    /**
51
     * Get First Fighter.
52
     *
53
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
54
     */
55
    public function competitor1()
56
    {
57
        return $this->belongsTo(Competitor::class, 'c1', 'id');
58
    }
59
60
    /**
61
     * Get Second Fighter.
62
     *
63
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
64
     */
65
    public function competitor2()
66
    {
67
        return $this->belongsTo(Competitor::class, 'c2', 'id');
68
    }
69
70
    /**
71
     * Get First Fighter.
72
     *
73
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
74
     */
75
    public function team1()
76
    {
77
        return $this->belongsTo(Team::class, 'c1', 'id');
78
    }
79
80
    /**
81
     * Get Second Fighter.
82
     *
83
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
84
     */
85
    public function team2()
86
    {
87
        return $this->belongsTo(Team::class, 'c2', 'id');
88
    }
89
90
    /**
91
     * Save a Fight.
92
     *
93
     * @param Collection $rounds
94
     * @param int $numRound
95
     */
96
    public static function savePreliminaryFightRound($rounds, $numRound = 1)
97
    {
98
        
99
        $c1 = $c2 = null;
100
        $order = 0;
101
102
        foreach ($rounds as $round) {
103
104
            if ($round->championship->category->isTeam()) {
105
                $fighters = $round->teams;
106
            } else {
107
                $fighters = $round->competitors;
108
            }
109
110
            $fighter1 = $fighters->get(0);
111
            $fighter2 = $fighters->get(1);
112
            $fighter3 = $fighters->get(2);
113
114
            switch ($numRound) {
115
                case 1:
116
                    $c1 = $fighter1;
117
                    $c2 = $fighter2;
118
                    break;
119
                case 2:
120
                    $c1 = $fighter2;
121
                    $c2 = $fighter3;
122
                    break;
123
                case 3:
124
                    $c1 = $fighter3;
125
                    $c2 = $fighter1;
126
                    break;
127
            }
128
            $fight = new self();
129
            $fight->round_id = $round->id;
130
            $fight->c1 = $c1 != null ? $c1->id : null;
131
            $fight->c2 = $c2 != null ? $c2->id : null;
132
            $fight->order = $order++;
133
            $fight->area = $round->area;
134
            $fight->save();
135
        }
136
    }
137
138
//    public static function saveRoundRobinFight(Championship $championship, $tree)
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
139
//    {
140
//        $championship->category->isTeam
141
//            ? $fighters = $championship->teams
142
//            : $fighters = $championship->users;
143
//
144
//        $reverseFighters = $fighters->reverse();
145
//        $order = 0;
146
//        foreach ($reverseFighters as $reverse) {
147
//            foreach ($fighters as $fighter) {
148
//                if ($fighter->id != $reverse->id) {
149
//                    $fight = new Fight();
150
//                    $fight->tree_id = $tree[0]->id;
151
//                    $fight->c1 = $fighter->id;
152
//                    $fight->c2 = $reverse->id;
153
//                    $fight->order = $order++;
154
//                    $fight->area = 1;
155
//                    $fight->save();
156
//                    $order++;
157
//                }
158
//            }
159
//        }
160
//    }
161
162
    /**
163
     * @param Collection $rounds
164
     */
165
    public static function saveRoundRobinFights(Championship $championship, $rounds)
166
    {
167
        foreach ($rounds as $round2) {
168
            $fighters = self::getActorsToFights($championship, $round2);
169
170
            $away = $fighters->splice(count($fighters) / 2); // 2
171
172
            $home = $fighters; // 1
173
174
            $order = 1;
175
176
            for ($i = 0; $i < count($home) + count($away) - 1; $i++) { // 0 -> 2
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
177
                for ($j = 0; $j < count($home); $j++) {  // 1 no mas
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
178
179
                    $round[$i][$j]['Home'] = $home[$j];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$round was never initialized. Although not strictly required by PHP, it is generally a good practice to add $round = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
180
                    $round[$i][$j]['Away'] = $away[$j];
0 ignored issues
show
Bug introduced by
The variable $round does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
181
                    $fight = new self();
182
                    $fight->round_id = $rounds[0]->id;
183
                    $fight->c1 = $round[$i][$j]['Home']->id;
184
                    $fight->c2 = $round[$i][$j]['Away']->id;
185
                    $fight->order = $order++;
186
                    $fight->area = 1;
187
188
                    // We ommit fights that have a BYE in Round robins, but not in Preliminary
189
190
                    if ($fight->c1 != null && $fight->c2 != null || !$championship->isRoundRobinType()) {
191
                        $fight->save();
192
                    }
193
                }
194
                if (count($home) + count($away) - 1 > 2) {
195
                    $away->prepend($home->splice(1, 1)->shift());
196
                    $home->push($away->pop());
197
                    $order++;
198
                }
199
            }
200
//            return $round;
201
        }
202
    }
203
}
204