Test Failed
Branch scoresheet (1f7963)
by Julien
02:50
created

Fight::getActorsToFights()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 12
Ratio 48 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 25
rs 8.439
cc 6
eloc 16
nc 6
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
        'group_id',
24
        'c1',
25
        'c2',
26
    ];
27
28
    /**
29
     * @param Championship $championship
30
     *
31
     * @return mixed
32
     */
33
    private static function getActorsToFights(Championship $championship, FightersGroup $group = null)
34
    {
35
        if ($championship->category->isTeam) {
36
            $fighters = $group->teams;
37 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...
38
                $fighters->push(new Team());
39
                $fighters->push(new Team());
40
            } else if (count($fighters) % 2 != 0) {
41
                $fighters->push(new Team(['name' => 'BYE']));
42
            }
43
44
        } else {
45
            $fighters = $group->competitors;
46 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...
47
                $fighters->push(new Competitor());
48
                $fighters->push(new Competitor());
49
            }else if (count($fighters) % 2 != 0) { // If fighter is not pair, add a BYE
50
                $fighters->push(new Competitor());
51
            }
52
53
54
        }
55
56
        return $fighters;
57
    }
58
59
    /**
60
     * Get First Fighter.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    public function competitor1()
65
    {
66
        return $this->belongsTo(Competitor::class, 'c1', 'id');
67
    }
68
69
    /**
70
     * Get Second Fighter.
71
     *
72
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
73
     */
74
    public function competitor2()
75
    {
76
        return $this->belongsTo(Competitor::class, 'c2', 'id');
77
    }
78
79
    /**
80
     * Get First Fighter.
81
     *
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
83
     */
84
    public function team1()
85
    {
86
        return $this->belongsTo(Team::class, 'c1', 'id');
87
    }
88
89
    /**
90
     * Get Second Fighter.
91
     *
92
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
93
     */
94
    public function team2()
95
    {
96
        return $this->belongsTo(Team::class, 'c2', 'id');
97
    }
98
99
    /**
100
     * Save a Fight.
101
     *
102
     * @param Collection $groups
103
     * @param int $numGroup
104
     */
105
    public static function savePreliminaryFightGroup($groups, $numGroup = 1)
106
    {
107
108
        $c1 = $c2 = null;
109
        $order = 0;
110
111
        foreach ($groups as $group) {
112
113
            if ($group->championship->category->isTeam()) {
114
                $fighters = $group->teams;
115
            } else {
116
                $fighters = $group->competitors;
117
            }
118
119
            $fighter1 = $fighters->get(0);
120
            $fighter2 = $fighters->get(1);
121
            $fighter3 = $fighters->get(2);
122
123
            switch ($numGroup) {
124
                case 1:
125
                    $c1 = $fighter1;
126
                    $c2 = $fighter2;
127
                    break;
128
                case 2:
129
                    $c1 = $fighter2;
130
                    $c2 = $fighter3;
131
                    break;
132
                case 3:
133
                    $c1 = $fighter3;
134
                    $c2 = $fighter1;
135
                    break;
136
            }
137
            $fight = new self();
138
            $fight->fighters_group_id = $group->id;
139
            $fight->c1 = $c1 != null ? $c1->id : null;
140
            $fight->c2 = $c2 != null ? $c2->id : null;
141
            $fight->order = $order++;
142
            $fight->area = $group->area;
143
            $fight->save();
144
        }
145
    }
146
147
148
    /**
149
     * @param Collection $groups
150
     */
151
    public static function saveGroupFights(Championship $championship, $groups)
152
    {
153
        foreach ($groups as $group) {
154
            $fighters = self::getActorsToFights($championship, $group);
155
156
            $away = $fighters->splice(count($fighters) / 2); // 2
157
158
            $home = $fighters; // 1
159
160
            $order = 1;
161
162
            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...
163
                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...
164
165
                    $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...
166
                    $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...
167
                    $fight = new self();
168
                    $fight->fighters_group_id = $group->id;
169
                    $fight->c1 = $round[$i][$j]['Home']->id;
170
                    $fight->c2 = $round[$i][$j]['Away']->id;
171
                    $fight->order = $order++;
172
                    $fight->area = 1;
173
174
                    $fight->save();
175
176
                }
177
                if (count($home) + count($away) - 1 > 2) {
178
                    $away->prepend($home->splice(1, 1)->shift());
179
                    $home->push($away->pop());
180
                    $order++;
181
                }
182
            }
183
        }
184
    }
185
}
186