Passed
Push — master ( 7c37ab...051cab )
by Julien
21:18
created

Fight::group()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
        'group_id',
24
        'c1',
25
        'c2',
26
    ];
27
28
    /**
29
     * Get First Fighter.
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
32
     */
33
    public function group()
34
    {
35
        return $this->belongsTo(\Xoco70\KendoTournaments\Models\FightersGroup::class, 'fighters_group_id');
36
    }
37
    /**
38
     * @param Championship $championship
39
     *
40
     * @return mixed
41
     */
42
    private static function getActorsToFights(\Xoco70\KendoTournaments\Models\Championship $championship, FightersGroup $group = null)
43
    {
44
        if ($championship->category->isTeam) {
45
            $fighters = $group->teams;
46 View Code Duplication
            if (sizeof($fighters) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
                $fighters->push(new Team());
48
                $fighters->push(new Team());
49
            } else if (count($fighters) % 2 != 0) {
50
                $fighters->push(new Team(['name' => 'BYE']));
51
            }
52
53
        } else {
54
            $fighters = $group->competitors;
55 View Code Duplication
            if (sizeof($fighters) == 0) { // If
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
                $fighters->push(new Competitor());
57
                $fighters->push(new Competitor());
58
            } else if (count($fighters) % 2 != 0) { // If fighter is not pair, add a BYE
59
                $fighters->push(new Competitor());
60
            }
61
62
63
        }
64
65
        return $fighters;
66
    }
67
68
    /**
69
     * Get First Fighter.
70
     *
71
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
72
     */
73
    public function competitor1()
74
    {
75
        return $this->belongsTo(\Xoco70\KendoTournaments\Models\Competitor::class, 'c1', 'id');
76
    }
77
78
    /**
79
     * Get Second Fighter.
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
82
     */
83
    public function competitor2()
84
    {
85
        return $this->belongsTo(\Xoco70\KendoTournaments\Models\Competitor::class, 'c2', 'id');
86
    }
87
88
    /**
89
     * Get First Fighter.
90
     *
91
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
92
     */
93
    public function team1()
94
    {
95
        return $this->belongsTo(\Xoco70\KendoTournaments\Models\Team::class, 'c1', 'id');
96
    }
97
98
    /**
99
     * Get Second Fighter.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
102
     */
103
    public function team2()
104
    {
105
        return $this->belongsTo(\Xoco70\KendoTournaments\Models\Team::class, 'c2', 'id');
106
    }
107
108
    /**
109
     * Save a Fight.
110
     *
111
     * @param Collection $groups
112
     * @param int $numGroup
113
     */
114
    public static function savePreliminaryFightGroup($groups, $numGroup = 1)
115
    {
116
117
        $c1 = $c2 = null;
118
        $order = 0;
119
120
        foreach ($groups as $group) {
121
122
            if ($group->championship->category->isTeam()) {
123
                $fighters = $group->teams;
124
            } else {
125
                $fighters = $group->competitors;
126
            }
127
128
            $fighter1 = $fighters->get(0);
129
            $fighter2 = $fighters->get(1);
130
            $fighter3 = $fighters->get(2);
131
132
            switch ($numGroup) {
133
                case 1:
134
                    $c1 = $fighter1;
135
                    $c2 = $fighter2;
136
                    break;
137
                case 2:
138
                    $c1 = $fighter2;
139
                    $c2 = $fighter3;
140
                    break;
141
                case 3:
142
                    $c1 = $fighter3;
143
                    $c2 = $fighter1;
144
                    break;
145
            }
146
            $fight = new self();
147
            $fight->fighters_group_id = $group->id;
148
            $fight->c1 = $c1 != null ? $c1->id : null;
149
            $fight->c2 = $c2 != null ? $c2->id : null;
150
            $fight->order = $order++;
151
            $fight->area = $group->area;
152
            $fight->save();
153
        }
154
    }
155
156
157
    /**
158
     * @param Championship $championship
159
     * @param Collection $groups
160
     */
161
    public static function saveGroupFights(\Xoco70\KendoTournaments\Models\Championship $championship, $groups)
162
    {
163
        foreach ($groups as $group) {
164
            $fighters = self::getActorsToFights($championship, $group);
165
166
            $away = $fighters->splice(count($fighters) / 2); // 2
167
168
            $home = $fighters; // 1
169
170
            $order = 1;
171
172
            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...
173
                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...
174
175
                    $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...
176
                    $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...
177
                    $fight = new self();
178
                    $fight->fighters_group_id = $group->id;
179
                    $fight->c1 = $round[$i][$j]['Home']->id;
180
                    $fight->c2 = $round[$i][$j]['Away']->id;
181
                    $fight->order = $order++;
182
                    $fight->area = 1;
183
184
                    $fight->save();
185
186
                }
187
                if (count($home) + count($away) - 1 > 2) {
188
                    $away->prepend($home->splice(1, 1)->shift());
189
                    $home->push($away->pop());
190
                    $order++;
191
                }
192
            }
193
        }
194
    }
195
196
    /**
197
     * @return string
198
     */
199 View Code Duplication
    public function getFighter1Name()
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...
200
    {
201
        $isTeam = $this->group->championship->category->isTeam;
202
        if ($isTeam) {
203
204
            return $this->team1 == null ? '' : $this->team1->name;
205
        }
206
        return
207
            $this->competitor1 == null ? 'BYE' : $this->competitor1->user->firstname . " " . $this->competitor1->user->lastname;
208
209
    }
210
211
    /**
212
     * @return string
213
     */
214 View Code Duplication
    public function getFighter2Name()
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...
215
    {
216
        $isTeam = $this->group->championship->category->isTeam;
217
        if ($isTeam) {
218
219
            return $this->team2 == null ? 'BYE' : $this->team2->name;
220
        }
221
222
        return
223
            $this->competitor2 == null ? 'BYE' : $this->competitor2->user->firstname . " " . $this->competitor2->user->lastname;
224
    }
225
226
227
    /**
228
     * @return string
229
     */
230 View Code Duplication
    public function getFighter1ShortId()
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...
231
    {
232
233
        $isTeam = $this->group->championship->category->isTeam;
234
        if ($isTeam) {
235
            return $this->team1 == null ? '' : $this->team1->short_id;
236
        }
237
238
        return $this->competitor1 == null ? '' : $this->competitor1->short_id;
239
240
    }
241
242
    /**
243
     * @return string
244
     */
245 View Code Duplication
    public function getFighter2ShortId()
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...
246
    {
247
248
        $isTeam = $this->group->championship->category->isTeam;
249
        if ($isTeam) {
250
            return $this->team2 == null ? '' : $this->team2->short_id;
251
        }
252
253
        return $this->competitor2 == null ? '' : $this->competitor2->short_id;
254
255
    }
256
}