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 |
||
| 16 | class Championship extends Model |
||
| 17 | { |
||
| 18 | use SoftDeletes; |
||
| 19 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
| 20 | protected $table = 'championship'; |
||
| 21 | |||
| 22 | public $timestamps = true; |
||
| 23 | protected $fillable = [ |
||
| 24 | 'tournament_id', |
||
| 25 | 'category_id', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | protected static function boot() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * A championship has many Competitors. |
||
| 44 | * |
||
| 45 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 46 | */ |
||
| 47 | public function competitors() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * A championship belongs to a Category. |
||
| 54 | * |
||
| 55 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 56 | */ |
||
| 57 | public function category() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * A championship belongs to a Tournament. |
||
| 64 | * |
||
| 65 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 66 | */ |
||
| 67 | public function tournament() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get All competitors from a Championships. |
||
| 74 | * |
||
| 75 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 76 | */ |
||
| 77 | public function users() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * A championship only has 1 Settings. |
||
| 86 | * |
||
| 87 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
| 88 | */ |
||
| 89 | public function settings() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * A championship has Many Teams. |
||
| 96 | * |
||
| 97 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 98 | */ |
||
| 99 | public function teams() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check if Championship has Preliminary Round Configured. |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function hasPreliminary() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Check if 2nd Round of Championship is Round Robin. |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function isPlayOffType() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Check if 2nd Round of Championship is Direct Elimination. |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | public function isDirectEliminationType() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * A championship has Many Groups of Fighters. |
||
| 136 | * |
||
| 137 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 138 | */ |
||
| 139 | public function fightersGroups() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * A championship has Many fights. |
||
| 146 | * |
||
| 147 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 148 | */ |
||
| 149 | public function fights() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the fights that happen to the first round |
||
| 156 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 157 | */ |
||
| 158 | public function firstRoundFights() |
||
| 164 | |||
| 165 | private function hasNoCustomSettings() |
||
| 176 | |||
| 177 | public function buildName() |
||
| 198 | |||
| 199 | public function getSettings() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return Groups that belongs to a round |
||
| 206 | * @param integer $round |
||
| 207 | * @return HasMany |
||
| 208 | */ |
||
| 209 | public function groupsByRound($round) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Return Fights that belongs to a round |
||
| 216 | * |
||
| 217 | * @param integer $round |
||
| 218 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 219 | */ |
||
| 220 | public function fightsByRound($round) |
||
| 224 | |||
| 225 | public function isPlayoffCompetitor() |
||
| 230 | |||
| 231 | public function isPlayoffTeam() |
||
| 236 | |||
| 237 | public function isDirectEliminationCompetitor() |
||
| 241 | |||
| 242 | public function isDirectEliminationTeam() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return TreeGenerable |
||
| 249 | */ |
||
| 250 | public function chooseGenerationStrategy() |
||
| 271 | |||
| 272 | } |
||
| 273 |
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.