Complex classes like Tournament 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 Tournament, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Tournament extends \Xoco70\LaravelTournaments\Models\Tournament |
||
| 25 | { |
||
| 26 | use SoftDeletes; |
||
| 27 | use Sluggable; |
||
| 28 | use AuditingTrait; |
||
| 29 | use SluggableScopeHelpers; |
||
| 30 | |||
| 31 | public $timestamps = true; |
||
| 32 | protected $table = 'tournament'; |
||
| 33 | protected $fillable = [ |
||
| 34 | 'name', |
||
| 35 | 'dateIni', |
||
| 36 | 'dateFin', |
||
| 37 | 'registerDateLimit', |
||
| 38 | 'sport', |
||
| 39 | 'promoter', |
||
| 40 | 'host_organization', |
||
| 41 | 'technical_assistance', |
||
| 42 | 'category', |
||
| 43 | 'rule_id', |
||
| 44 | 'type', |
||
| 45 | 'venue_id', |
||
| 46 | 'level_id' |
||
| 47 | ]; |
||
| 48 | protected $dates = ['dateIni', 'dateFin', 'registerDateLimit', 'created_at', 'updated_at', 'deleted_at']; |
||
| 49 | |||
| 50 | 34 | protected static function boot() |
|
| 51 | { |
||
| 52 | 34 | parent::boot(); |
|
| 53 | 34 | static::deleting(function ($tournament) { |
|
| 54 | 2 | $tournament->championships->each->delete(); |
|
| 55 | 2 | $tournament->invites->each->delete(); |
|
| 56 | |||
| 57 | 34 | }); |
|
| 58 | 34 | static::restoring(function ($tournament) { |
|
| 59 | 1 | $tournament->championships()->withTrashed()->get()->each->restore(); |
|
| 60 | 34 | }); |
|
| 61 | |||
| 62 | 34 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Return the sluggable configuration array for this model. |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | 20 | public function sluggable() |
|
| 70 | { |
||
| 71 | return [ |
||
| 72 | 20 | 'slug' => [ |
|
| 73 | 'source' => 'name' |
||
| 74 | ] |
||
| 75 | ]; |
||
| 76 | } |
||
| 77 | |||
| 78 | 1 | public function customizeSlugEngine(Slugify $engine, $attribute) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * A tournament is owned by a user |
||
| 91 | * |
||
| 92 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 93 | */ |
||
| 94 | 13 | public function owner() |
|
| 95 | { |
||
| 96 | 13 | return $this->belongsTo(User::class, 'user_id', 'id'); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get All Tournaments levels |
||
| 101 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 102 | */ |
||
| 103 | 1 | public function level() |
|
| 104 | { |
||
| 105 | 1 | return $this->belongsTo(TournamentLevel::class, 'level_id', 'id'); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get Full venue object |
||
| 110 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 111 | */ |
||
| 112 | 8 | public function venue() |
|
| 113 | { |
||
| 114 | 8 | return $this->belongsTo(Venue::class); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get All categoriesTournament that belongs to a tournament |
||
| 119 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 120 | */ |
||
| 121 | 15 | public function championships() |
|
| 122 | { |
||
| 123 | 15 | return $this->hasMany(Championship::class); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get All categoriesSettings that belongs to a tournament |
||
| 128 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 129 | */ |
||
| 130 | 8 | public function championshipSettings() |
|
| 131 | { |
||
| 132 | 8 | return $this->hasManyThrough(ChampionshipSettings::class, Championship::class); |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * çGet All teams that belongs to a tournament |
||
| 137 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 138 | */ |
||
| 139 | 6 | public function teams() |
|
| 140 | { |
||
| 141 | 6 | return $this->hasManyThrough(Team::class, Championship::class); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get All competitors that belongs to a tournament |
||
| 146 | * @param null $championshipId |
||
| 147 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 148 | */ |
||
| 149 | 11 | public function competitors($championshipId = null) |
|
| 150 | { |
||
| 151 | 11 | return $this->hasManyThrough(Competitor::class, Championship::class); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get All trees that belongs to a tournament |
||
| 156 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 157 | */ |
||
| 158 | 5 | public function trees() |
|
| 159 | { |
||
| 160 | 5 | return $this->hasManyThrough(FightersGroup::class, Championship::class); |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get all Invitations that belongs to a tournament |
||
| 165 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 166 | */ |
||
| 167 | 2 | public function invites() |
|
| 168 | { |
||
| 169 | 2 | return $this->morphMany(Invite::class, 'object'); |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get Category List with <Select> Format |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | 6 | public function getCategoryList() |
|
| 177 | { |
||
| 178 | 6 | return $this->categories->pluck('id')->all(); |
|
| 179 | } |
||
| 180 | |||
| 181 | public function getDateAttribute($date) |
||
| 182 | { |
||
| 183 | return $date; |
||
| 184 | } |
||
| 185 | |||
| 186 | 6 | public function getRegisterDateLimitAttribute($date) |
|
| 187 | { |
||
| 188 | 6 | return $date; |
|
| 189 | } |
||
| 190 | |||
| 191 | 8 | public function getDateIniAttribute($date) |
|
| 192 | { |
||
| 193 | 8 | return $date; |
|
| 194 | } |
||
| 195 | |||
| 196 | 6 | public function getDateFinAttribute($date) |
|
| 197 | { |
||
| 198 | 6 | return $date; |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Check if the tournament is Open |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 1 | public function isOpen() |
|
| 206 | { |
||
| 207 | 1 | return $this->type == 1; |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * * Check if the tournament needs Invitation |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function needsInvitation() |
||
| 215 | { |
||
| 216 | return $this->type == 0; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function isInternational() |
||
| 223 | { |
||
| 224 | return $this->level_id == 8; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function isNational() |
||
| 231 | { |
||
| 232 | return $this->level_id == 7; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isRegional() |
||
| 239 | { |
||
| 240 | return $this->level_id == 6; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function isEstate() |
||
| 247 | { |
||
| 248 | return $this->level_id == 5; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function isMunicipal() |
||
| 255 | { |
||
| 256 | return $this->level_id == 4; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function isDistrictal() |
||
| 263 | { |
||
| 264 | return $this->level_id == 3; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function isLocal() |
||
| 271 | { |
||
| 272 | return $this->level_id == 2; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function hasNoLevel() |
||
| 279 | { |
||
| 280 | return $this->level_id == 1; |
||
| 281 | } |
||
| 282 | |||
| 283 | 15 | public function getRouteKeyName() |
|
| 284 | { |
||
| 285 | 15 | return 'slug'; |
|
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 2 | public function isDeleted() |
|
| 292 | { |
||
| 293 | 2 | return $this->deleted_at != null; |
|
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Create and Configure Championships depending the rule ( IKF, EKF, LAKF, etc ) |
||
| 298 | * @param $ruleId |
||
| 299 | */ |
||
| 300 | 1 | public function setAndConfigureCategories($ruleId) |
|
| 301 | { |
||
| 302 | 1 | if ($ruleId == 0) return; // No Rules Selected |
|
| 303 | |||
| 304 | 1 | $options = $this->loadRulesOptions($ruleId); |
|
| 305 | |||
| 306 | // Create Tournament Categories |
||
| 307 | 1 | $arrCategories = array_keys($options); |
|
| 308 | 1 | $this->categories()->sync($arrCategories); |
|
| 309 | |||
| 310 | // Configure each category creating categorySetting Object |
||
| 311 | |||
| 312 | 1 | foreach ($this->championships as $championship) { |
|
| 313 | 1 | $rules = $options[$championship->category->id]; |
|
| 314 | 1 | $rules['championship_id'] = $championship->id; |
|
| 315 | 1 | ChampionshipSettings::create($rules); |
|
| 316 | } |
||
| 317 | 1 | } |
|
| 318 | |||
| 319 | /** |
||
| 320 | * return correct presets rues |
||
| 321 | * @param $ruleId |
||
| 322 | * @return mixed|null |
||
| 323 | */ |
||
| 324 | 1 | private function loadRulesOptions($ruleId) |
|
| 325 | { |
||
| 326 | switch ($ruleId) { |
||
| 327 | 1 | case 0: // No preset selected |
|
| 328 | return null; |
||
| 329 | 1 | case 1: |
|
| 330 | 1 | return $options = config('options.ikf_settings'); |
|
|
|
|||
| 331 | break; |
||
| 332 | case 2: |
||
| 333 | return $options = config('options.ekf_settings'); |
||
| 334 | break; |
||
| 335 | case 3: |
||
| 336 | return $options = config('options.lakc_settings'); |
||
| 337 | break; |
||
| 338 | default: |
||
| 339 | return null; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * We can use $tournament->categories()->attach(id); |
||
| 345 | * Or $tournament->categories()->sync([1, 2, 3]); |
||
| 346 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 347 | */ |
||
| 348 | 9 | public function categories() |
|
| 349 | { |
||
| 350 | 9 | return $this->belongsToMany(Category::class, 'championship') |
|
| 351 | 9 | ->withPivot('id') |
|
| 352 | 9 | ->withTimestamps(); |
|
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * create a category List with Category name associated to championshipId |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function buildCategoryList() |
||
| 361 | { |
||
| 362 | $championships = Championship::with('category', 'settings') |
||
| 363 | ->whereHas('category', function ($query) { |
||
| 364 | return $query->where('isTeam', 1); |
||
| 365 | }) |
||
| 366 | ->where('tournament_id', $this->id) |
||
| 367 | ->get(); |
||
| 368 | |||
| 369 | $array = []; |
||
| 370 | foreach ($championships as $championship) { |
||
| 371 | $array[$championship->id] = $championship->settings->alias != '' |
||
| 372 | ? $championship->settings->alias |
||
| 373 | : trim($championship->buildName()); |
||
| 374 | } |
||
| 375 | return $array; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get predefined translatable categories for categories quick add in tournament editing |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | 5 | public function getDefaultCategoriesName() |
|
| 389 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.