Test Failed
Push — master ( 2dcdef...5f4570 )
by Julien
03:01
created

Fight::user1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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