Test Failed
Push — master ( 241073...2d5b3a )
by Julien
03:08
created

Fight   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 31
lcom 2
cbo 4
dl 0
loc 192
rs 9.8
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getActorsToFights() 0 16 4
A competitor1() 0 4 1
A competitor2() 0 4 1
A team1() 0 4 1
A team2() 0 4 1
F savePreliminaryFightRound() 0 39 14
C saveRoundRobinFights() 0 42 8
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
    /**
90
     * Save a Fight.
91
     * @param $rounds
92
     * @param int $numRound
93
     */
94
    public static function savePreliminaryFightRound($rounds, $numRound = 1)
95
    {
96
97
        $c1 = $c2 = $c3 = null;
0 ignored issues
show
Unused Code introduced by
$c3 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...
98
        $order = 0;
99
100
        foreach ($rounds as $round) {
101
            if ($round->championship->isTeam) {
102
                $fighter1 = isset($round->teams[0]) ? $round->teams[0] : null;
103
                $fighter2 = isset($round->teams[1]) ? $round->teams[1] : null;
104
                $fighter3 = isset($round->teams[2]) ? $round->teams[2] : null;
105
            } else {
106
                $fighter1 = isset($round->competitors[0])  ? $round->competitors[0] : null;
107
                $fighter2 = isset($round->competitors[1])  ? $round->competitors[1] : null;
108
                $fighter3 = isset($round->competitors[2])  ? $round->competitors[2] : null;
109
            }
110
            switch ($numRound) {
111
                case 1:
112
                    $c1 = $fighter1;
113
                    $c2 = $fighter2;
114
                    break;
115
                case 2:
116
                    $c1 = $fighter2;
117
                    $c2 = $fighter3;
118
                    break;
119
                case 3:
120
                    $c1 = $fighter3;
121
                    $c2 = $fighter1;
122
                    break;
123
            }
124
            $fight = new Fight();
125
            $fight->round_id = $round->id;
126
            $fight->c1 = $c1 != null ? $c1->id : null;
127
            $fight->c2 = $c2 != null ? $c2->id : null;
128
            $fight->order = $order++;
129
            $fight->area = $round->area;
130
            $fight->save();
131
        }
132
    }
133
134
135
//    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...
136
//    {
137
//        $championship->category->isTeam
138
//            ? $fighters = $championship->teams
139
//            : $fighters = $championship->users;
140
//
141
//        $reverseFighters = $fighters->reverse();
142
//        $order = 0;
143
//        foreach ($reverseFighters as $reverse) {
144
//            foreach ($fighters as $fighter) {
145
//                if ($fighter->id != $reverse->id) {
146
//                    $fight = new Fight();
147
//                    $fight->tree_id = $tree[0]->id;
148
//                    $fight->c1 = $fighter->id;
149
//                    $fight->c2 = $reverse->id;
150
//                    $fight->order = $order++;
151
//                    $fight->area = 1;
152
//                    $fight->save();
153
//                    $order++;
154
//                }
155
//            }
156
//        }
157
//    }
158
159
    public static function saveRoundRobinFights(Championship $championship, $rounds)
160
    {
161
162
163
        foreach ($rounds as $round2) {
164
165
            $fighters = self::getActorsToFights($championship, $round2);
166
167
            $away = $fighters->splice(sizeof($fighters) / 2);  // 2
168
169
            $home = $fighters; // 1
170
171
            $order = 1;
172
173
            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...
174
                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...
175
176
                    $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...
177
                    $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...
178
                    $fight = new Fight();
179
                    $fight->round_id = $rounds[0]->id;
180
                    $fight->c1 = $round[$i][$j]["Home"]->id;
181
                    $fight->c2 = $round[$i][$j]["Away"]->id;
182
                    $fight->order = $order++;
183
                    $fight->area = 1;
184
185
                    // We ommit fights that have a BYE in Round robins, but not in Preliminary
186
187
                    if ($fight->c1 != null && $fight->c2 != null || !$championship->isRoundRobinType()) {
188
                        $fight->save();
189
                    }
190
                }
191
                if (sizeof($home) + sizeof($away) - 1 > 2) {
192
                    $away->prepend($home->splice(1, 1)->shift());
193
                    $home->push($away->pop());
194
                    $order++;
195
                }
196
            }
197
//            return $round;
198
        }
199
200
    }
201
202
}