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 |
||
| 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() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A championship belongs to a Category. |
||
| 48 | * |
||
| 49 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 50 | */ |
||
| 51 | public function category() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * A championship belongs to a Tournament. |
||
| 58 | * |
||
| 59 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 60 | */ |
||
| 61 | public function tournament() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get All competitors from a Championships. |
||
| 68 | * |
||
| 69 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 70 | */ |
||
| 71 | public function users() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * A championship only has 1 Settings. |
||
| 80 | * |
||
| 81 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
| 82 | */ |
||
| 83 | public function settings() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * A championship has Many Teams. |
||
| 90 | * |
||
| 91 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 92 | */ |
||
| 93 | public function teams() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Check if Championship has Preliminary Round Configured. |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function hasPreliminary() |
||
| 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() |
||
| 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() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the fights that happen to the first round |
||
| 150 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 151 | */ |
||
| 152 | public function firstRoundFights() |
||
| 158 | |||
| 159 | private function hasNoCustomSettings() |
||
| 170 | |||
| 171 | public function buildName() |
||
| 192 | |||
| 193 | public function getSettings() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return Groups that belongs to a round |
||
| 200 | * @param integer $round |
||
| 201 | * @return HasMany |
||
| 202 | */ |
||
| 203 | public function groupsByRound($round) |
||
| 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) |
||
| 218 | |||
| 219 | public function isPlayoffCompetitor() |
||
| 224 | |||
| 225 | public function isPlayoffTeam() |
||
| 230 | |||
| 231 | public function isDirectEliminationCompetitor() |
||
| 235 | |||
| 236 | public function isDirectEliminationTeam() |
||
| 240 | } |
||
| 241 |
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.