Test Failed
Push — master ( 84ef8e...92b331 )
by Julien
08:45
created

Fight   B

Complexity

Total Complexity 54

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 54
c 10
b 1
f 1
lcom 3
cbo 4
dl 0
loc 279
rs 7.0642

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createFight() 0 11 3
A group() 0 4 1
A getActorsToFights() 0 14 4
A competitor1() 0 4 1
A competitor2() 0 4 1
A team1() 0 4 1
A team2() 0 4 1
B savePreliminaryFightGroup() 0 27 5
B saveGroupFights() 0 33 5
B getFighterAttr() 0 17 7
B updateParentFight() 0 15 8
B getParentFighterToUpdate() 0 16 5
B getValueToUpdate() 0 13 5
B hasDeterminedParent() 0 10 6

How to fix   Complexity   

Complex Class

Complex classes like Fight often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Fight, and based on these observations, apply Extract Interface, too.

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
     * @param int $userId1
13
     * @param int $userId2
14
     */
15
    public function __construct($userId1 = null, $userId2 = null)
16
    {
17
        $this->c1 = $userId1;
18
        $this->c2 = $userId2;
19
    }
20
21
    protected $table = 'fight';
22
    public $timestamps = true;
23
24
    protected $fillable = [
25
        'group_id',
26
        'c1',
27
        'c2',
28
    ];
29
30
    /**
31
     * @param $group
32
     * @param $competitor1
33
     * @param $competitor2
34
     * @param $order
35
     * @return mixed
36
     */
37
    private static function createFight($group, $competitor1, $competitor2, $order)
38
    {
39
        $fight = new self();
40
        $fight->fighters_group_id = $group->id;
41
        $fight->c1 = $competitor1 != null ? $competitor1->id : null;
42
        $fight->c2 = $competitor2 != null ? $competitor2->id : null;
43
        $fight->short_id = $order++;
44
        $fight->area = $group->area;
45
        $fight->save();
46
        return $order;
47
    }
48
49
    /**
50
     * Get First Fighter.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
53
     */
54
    public function group()
55
    {
56
        return $this->belongsTo(FightersGroup::class, 'fighters_group_id');
57
    }
58
59
    /**
60
     * @param Championship $championship
61
     *
62
     * @return Collection
63
     */
64
    private static function getActorsToFights(Championship $championship, FightersGroup $group = null)
65
    {
66
        if ($group == null) return null;
67
        $fighters = $group->getFighters();
68
        $fighterType = $group->getFighterType();
69
        if (sizeof($fighters) == 0) {
70
            $fighters->push(new $fighterType);
71
            $fighters->push(new $fighterType);
72
        } else if (count($fighters) % 2 != 0) {
73
            $fighters->push(new $fighterType);
74
        }
75
76
        return $fighters;
77
    }
78
79
    /**
80
     * Get First Fighter.
81
     *
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
83
     */
84
    public function competitor1()
85
    {
86
        return $this->belongsTo(Competitor::class, 'c1', 'id');
87
    }
88
89
    /**
90
     * Get Second Fighter.
91
     *
92
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
93
     */
94
    public function competitor2()
95
    {
96
        return $this->belongsTo(Competitor::class, 'c2', 'id');
97
    }
98
99
    /**
100
     * Get First Fighter.
101
     *
102
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
103
     */
104
    public function team1()
105
    {
106
        return $this->belongsTo(Team::class, 'c1', 'id');
107
    }
108
109
    /**
110
     * Get Second Fighter.
111
     *
112
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
113
     */
114
    public function team2()
115
    {
116
        return $this->belongsTo(Team::class, 'c2', 'id');
117
    }
118
119
    /**
120
     * Save a Fight.
121
     *
122
     * @param Collection $groups
123
     * @param int $numGroup
124
     */
125
    public static function savePreliminaryFightGroup($groups, $numGroup = 1)
126
    {
127
        $competitor1 = $competitor2 = null;
128
        $order = 1;
129
130
        foreach ($groups as $group) {
131
132
            $fighters = $group->getFighters();
133
            [$fighter1, $fighter2, $fighter3 ] = $fighters;
0 ignored issues
show
Bug introduced by
The variable $fighter1 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $fighter2 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $fighter3 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
134
135
            switch ($numGroup) {
136
                case 1:
137
                    $competitor1 = $fighter1;
138
                    $competitor2 = $fighter2;
139
                    break;
140
                case 2:
141
                    $competitor1 = $fighter2;
142
                    $competitor2 = $fighter3;
143
                    break;
144
                case 3:
145
                    $competitor1 = $fighter3;
146
                    $competitor2 = $fighter1;
147
                    break;
148
            }
149
            $order = self::createFight($group, $competitor1, $competitor2, $order);
150
        }
151
    }
152
153
154
    /**
155
     * @param Championship $championship
156
     */
157
    public static function saveGroupFights(Championship $championship)
158
    {
159
        $order = 1;
160
        $round = [];
161
        foreach ($championship->fightersGroups()->get() as $group) {
162
            $fighters = self::getActorsToFights($championship, $group);
163
            $away = $fighters->splice(count($fighters) / 2); // 2
164
            $home = $fighters; // 1
165
166
            $countHome = count($home);
167
            $countAway = count($away);
168
            for ($i = 0; $i < $countHome + $countAway - 1; $i++) {
169
                for ($j = 0; $j < $countHome; $j++) {
170
171
                    $round[$i][$j]['Home'] = $home[$j];
172
                    $round[$i][$j]['Away'] = $away[$j];
173
                    $fight = new self();
174
                    $fight->fighters_group_id = $group->id;
175
                    $fight->c1 = $round[$i][$j]['Home']->id;
176
                    $fight->c2 = $round[$i][$j]['Away']->id;
177
                    $fight->short_id = $order++;
178
                    $fight->area = $group->area;
179
                    $fight->save();
180
181
                }
182
                if ($countHome + $countAway - 1 > 2) {
183
                    $away->prepend($home->splice(1, 1)->shift());
184
                    $home->push($away->pop());
185
                    $order++;
186
                }
187
            }
188
        }
189
    }
190
191
    public function getFighterAttr($numFighter, $attr)
192
    {
193
        $isTeam = $this->group->championship->category->isTeam;
194
        if ($isTeam) {
195
            $teamToUpdate = 'team' . $numFighter;
196
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
197
        }
198
        $competitorToUpdate = 'competitor' . $numFighter;
199
        if ($attr == 'name') {
200
            return $this->$competitorToUpdate == null
201
                ? 'BYE'
202
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
203
        } elseif ($attr == 'short_id') {
204
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
205
        }
206
        return null;
207
    }
208
209
    /**
210
     * Update parent Fight
211
     * @param $fighterToUpdate
212
     * @param $fight
213
     */
214
    public function updateParentFight($fighterToUpdate, $fight)
215
    {
216
        if ($fight != null) {
217
            if (($fight->c1 != null || $fight->c2 == null)) {
218
                $this->$fighterToUpdate = $fight->c1;
219
            }
220
            if ($fight->c1 == null || $fight->c2 != null) {
221
                $this->$fighterToUpdate = $fight->c2;
222
            }
223
            if ($fight->c1 == null || $fight->c2 == null) {
224
                $this->$fighterToUpdate = null;
225
            }
226
        }
227
228
    }
229
230
    /**
231
     * Returns the parent field that need to be updated
232
     * @return null|string
233
     */
234
    public function getParentFighterToUpdate()
235
    {
236
        $childrenGroup = $this->group->parent->children;
237
        foreach ($childrenGroup as $key => $children) {
238
            $childFight = $children->fights->get(0);
239
            if ($childFight->id == $this->id) {
240
                if ($key % 2 == 0) {
241
                    return "c1";
242
                }
243
                if ($key % 2 == 1) {
244
                    return "c2";
245
                }
246
            }
247
        }
248
        return null;
249
    }
250
251
    /**
252
     * In the original fight ( child ) return the field that contains data to copy to parent
253
     * @return null|string
254
     */
255
    public function getValueToUpdate()
256
    {
257
        if ($this->c1 != null && $this->c2 != null) {
258
            return null;
259
        }
260
        if ($this->c1 != null) {
261
            return "c1";
262
        }
263
        if ($this->c2 != null) {
264
            return "c2";
265
        }
266
        return null;
267
    }
268
269
    /**
270
     * Check if we are able to fill the parent fight or not
271
     * If one of the children has c1 x c2, then we must wait to fill parent
272
     *
273
     * @return bool
274
     */
275
    function hasDeterminedParent()
276
    {
277
278
        if ($this->c1 != null && $this->c2 != null) return true;
279
        foreach ($this->group->children as $child) {
280
            $fight = $child->fights->get(0);
281
            if ($fight->c1 != null && $fight->c2 != null) return false;
282
        }
283
        return true;
284
    }
285
286
}