Passed
Push — master ( 59b1f2...d98836 )
by Julien
33:15
created

Championship   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 46
c 6
b 0
f 0
lcom 3
cbo 4
dl 0
loc 231
rs 8.3999

22 Methods

Rating   Name   Duplication   Size   Complexity  
A competitors() 0 4 1
A boot() 0 13 1
A category() 0 4 1
A tournament() 0 4 1
A users() 0 6 1
A settings() 0 4 1
A teams() 0 4 1
A hasPreliminary() 0 4 2
A isDirectEliminationType() 0 4 2
A fights() 0 4 1
A isPlayOffType() 0 4 2
A fightersGroups() 0 4 1
A firstRoundFights() 0 6 1
B hasNoCustomSettings() 0 11 9
B buildName() 0 21 6
A getSettings() 0 4 1
A groupsByRound() 0 4 1
A fightsByRound() 0 4 1
A isPlayoffCompetitor() 0 5 3
A isPlayoffTeam() 0 5 3
A isDirectEliminationCompetitor() 0 4 3
A isDirectEliminationTeam() 0 4 3

How to fix   Complexity   

Complex Class

Complex classes like Championship 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 Championship, 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\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Illuminate\Foundation\Auth\User;
9
10
class Championship extends Model
11
{
12
    use SoftDeletes;
13
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
14
    protected $table = 'championship';
15
16
    public $timestamps = true;
17
    protected $fillable = [
18
        'tournament_id',
19
        'category_id',
20
    ];
21
22
    protected static function boot()
23
    {
24
        parent::boot();
25
26
        static::deleting(function ($championship) {
27
            $championship->competitors()->delete();
28
            $championship->settings()->delete();
29
        });
30
        static::restoring(function ($championship) {
31
            $championship->competitors()->restore();
32
            $championship->settings()->restore();
33
        });
34
    }
35
36
    /**
37
     * A championship has many Competitors.
38
     *
39
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
40
     */
41
    public function competitors()
42
    {
43
        return $this->hasMany(Competitor::class);
44
    }
45
46
    /**
47
     * A championship belongs to a Category.
48
     *
49
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
50
     */
51
    public function category()
52
    {
53
        return $this->belongsTo(Category::class);
54
    }
55
56
    /**
57
     * A championship belongs to a Tournament.
58
     *
59
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
60
     */
61
    public function tournament()
62
    {
63
        return $this->belongsTo(Tournament::class);
64
    }
65
66
    /**
67
     * Get All competitors from a Championships.
68
     *
69
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
70
     */
71
    public function users()
72
    {
73
        return $this->belongsToMany(User::class, 'competitor', 'championship_id')
74
            ->withPivot('confirmed')
75
            ->withTimestamps();
76
    }
77
78
    /**
79
     * A championship only has 1 Settings.
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
82
     */
83
    public function settings()
84
    {
85
        return $this->hasOne(ChampionshipSettings::class);
86
    }
87
88
    /**
89
     * A championship has Many Teams.
90
     *
91
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
92
     */
93
    public function teams()
94
    {
95
        return $this->hasMany(Team::class);
96
    }
97
98
    /**
99
     * Check if Championship has Preliminary Round Configured.
100
     *
101
     * @return bool
102
     */
103
    public function hasPreliminary()
104
    {
105
        return $this->settings == null || $this->settings->hasPreliminary;
106
    }
107
108
    /**
109
     * Check if 2nd Round of Championship is Round Robin.
110
     *
111
     * @return bool
112
     */
113
    public function isPlayOffType()
114
    {
115
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF;
116
    }
117
118
    /**
119
     * Check if 2nd Round of Championship is Direct Elimination.
120
     *
121
     * @return bool
122
     */
123
    public function isDirectEliminationType()
124
    {
125
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION;
126
    }
127
128
    /**
129
     * A championship has Many Groups of Fighters.
130
     *
131
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
132
     */
133
    public function fightersGroups()
134
    {
135
        return $this->hasMany(FightersGroup::class);
136
    }
137
138
    /**
139
     * A championship has Many fights.
140
     *
141
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
142
     */
143
    public function fights()
144
    {
145
        return $this->hasManyThrough(Fight::class, FightersGroup::class);
146
    }
147
148
    /**
149
     * Get the fights that happen to the first round
150
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
151
     */
152
    public function firstRoundFights()
153
    {
154
        return $this->hasManyThrough(Fight::class, FightersGroup::class)
155
            ->where('fighters_groups.round', 1)
156
            ->orderby('fighters_groups.id', 'desc');
157
    }
158
159
    private function hasNoCustomSettings()
160
    {
161
        return
162
            $this->settings == null ||
163
            ($this->settings->ageCategory == null || $this->settings->ageCategory == 0) &&
164
            $this->settings->ageMin == null &&
165
            $this->settings->ageMax == null &&
166
            $this->settings->gradeMin == null &&
167
            $this->settings->gradeMax == null &&
168
            ($this->settings->gradeCategory == null || $this->settings->gradeCategory == 0);
169
    }
170
171
    public function buildName()
172
    {
173
        if ($this->settings != null && $this->settings->alias != null && $this->settings->alias != '')
174
            return $this->settings->alias;
175
176
        if ($this->hasNoCustomSettings()) {
177
            return $this->category->name;
178
        }
179
180
        $genders = [
181
            'M' => trans('categories.male'),
182
            'F' => trans('categories.female'),
183
            'X' => trans('categories.mixt')
184
        ];
185
186
        $teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single');
187
        $ageCategoryText = $this->category->getAgeString();
188
        $gradeText = $this->category->getGradeString();
189
190
        return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText;
191
    }
192
193
    public function getSettings()
194
    {
195
        return $setting = $this->settings ?? new ChampionshipSettings(ChampionshipSettings::DEFAULT_SETTINGS);
0 ignored issues
show
Unused Code introduced by
$setting 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...
196
    }
197
198
    /**
199
     * Return Groups that belongs to a round
200
     * @param integer $round
201
     * @return HasMany
202
     */
203
    public function groupsByRound($round)
204
    {
205
        return $this->fightersGroups()->where('round', $round);
206
    }
207
208
    /**
209
     * Return Fights that belongs to a round
210
     *
211
     * @param integer $round
212
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
213
     */
214
    public function fightsByRound($round)
215
    {
216
        return $this->hasManyThrough(Fight::class, FightersGroup::class)->where('round', $round);
217
    }
218
219
    public function isPlayoffCompetitor()
220
    {
221
        return !$this->category->isTeam() &&
222
            ($this->isPlayOffType() || $this->hasPreliminary());
223
    }
224
225
    public function isPlayoffTeam()
226
    {
227
        return $this->category->isTeam() &&
228
            ($this->isPlayOffType() || $this->hasPreliminary());
229
    }
230
231
    public function isDirectEliminationCompetitor()
232
    {
233
        return !$this->category->isTeam() && $this->isDirectEliminationType() && !$this->hasPreliminary();
234
    }
235
236
    public function isDirectEliminationTeam()
237
    {
238
        return $this->category->isTeam() && $this->isDirectEliminationType() && !$this->hasPreliminary();
239
    }
240
}
241