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 | public function fighters() |
||
| 53 | { |
||
| 54 | if ($this->category->isTeam) { |
||
| 55 | return $this->hasMany(Team::class); |
||
| 56 | } |
||
| 57 | return $this->hasMany(Competitor::class); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A championship belongs to a Category. |
||
| 62 | * |
||
| 63 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 64 | */ |
||
| 65 | public function category() |
||
| 66 | { |
||
| 67 | return $this->belongsTo(Category::class); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * A championship belongs to a Tournament. |
||
| 72 | * |
||
| 73 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 74 | */ |
||
| 75 | public function tournament() |
||
| 76 | { |
||
| 77 | return $this->belongsTo(Tournament::class); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get All competitors from a Championships. |
||
| 82 | * |
||
| 83 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 84 | */ |
||
| 85 | public function users() |
||
| 86 | { |
||
| 87 | return $this->belongsToMany(User::class, 'competitor', 'championship_id') |
||
| 88 | ->withPivot('confirmed') |
||
| 89 | ->withTimestamps(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * A championship only has 1 Settings. |
||
| 94 | * |
||
| 95 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
| 96 | */ |
||
| 97 | public function settings() |
||
| 98 | { |
||
| 99 | return $this->hasOne(ChampionshipSettings::class); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * A championship has Many Teams. |
||
| 104 | * |
||
| 105 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 106 | */ |
||
| 107 | public function teams() |
||
| 108 | { |
||
| 109 | return $this->hasMany(Team::class); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Check if Championship has Preliminary Round Configured. |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function hasPreliminary() |
||
| 118 | { |
||
| 119 | return $this->settings == null || $this->settings->hasPreliminary; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Check if 2nd Round of Championship is Round Robin. |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function isPlayOffType() |
||
| 128 | { |
||
| 129 | return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check if 2nd Round of Championship is Direct Elimination. |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function isDirectEliminationType() |
||
| 138 | { |
||
| 139 | return $this->settings != null && $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * A championship has Many Groups of Fighters. |
||
| 144 | * |
||
| 145 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 146 | */ |
||
| 147 | public function fightersGroups() |
||
| 148 | { |
||
| 149 | return $this->hasMany(FightersGroup::class); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * A championship has Many fights. |
||
| 154 | * |
||
| 155 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 156 | */ |
||
| 157 | public function fights() |
||
| 158 | { |
||
| 159 | return $this->hasManyThrough(Fight::class, FightersGroup::class); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the fights that happen to the first round |
||
| 164 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 165 | */ |
||
| 166 | public function firstRoundFights() |
||
| 167 | { |
||
| 168 | return $this->hasManyThrough(Fight::class, FightersGroup::class) |
||
| 169 | ->where('fighters_groups.round', 1); |
||
| 170 | } |
||
| 171 | |||
| 172 | private function hasNoCustomSettings() |
||
| 173 | { |
||
| 174 | return $this->settings == null; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function buildName() |
||
| 178 | { |
||
| 179 | if ($this->settings != null && $this->settings->alias != null) |
||
| 180 | return $this->settings->alias; |
||
| 181 | |||
| 182 | if ($this->hasNoCustomSettings()) { |
||
| 183 | return $this->category->name; |
||
| 184 | } |
||
| 185 | |||
| 186 | $genders = [ |
||
| 187 | 'M' => trans('categories.male'), |
||
| 188 | 'F' => trans('categories.female'), |
||
| 189 | 'X' => trans('categories.mixt') |
||
| 190 | ]; |
||
| 191 | |||
| 192 | $teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single'); |
||
| 193 | $ageCategoryText = $this->category->getAgeString(); |
||
| 194 | $gradeText = $this->category->getGradeString(); |
||
| 195 | |||
| 196 | return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText; |
||
| 197 | } |
||
| 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 Groups that belongs to a round |
||
| 216 | * @param integer $round |
||
| 217 | * @return HasMany |
||
| 218 | */ |
||
| 219 | public function groupsFromRound($round) |
||
| 220 | { |
||
| 221 | return $this->fightersGroups()->where('round', '>=', $round); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Return Fights that belongs to a round |
||
| 226 | * |
||
| 227 | * @param integer $round |
||
| 228 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
| 229 | */ |
||
| 230 | public function fightsByRound($round) |
||
| 234 | |||
| 235 | public function isPlayoffCompetitor() |
||
| 240 | |||
| 241 | public function isPlayoffTeam() |
||
| 246 | |||
| 247 | public function isDirectEliminationCompetitor() |
||
| 251 | |||
| 252 | public function isDirectEliminationTeam() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return TreeGenerable |
||
| 259 | */ |
||
| 260 | public function chooseGenerationStrategy() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return int |
||
| 283 | */ |
||
| 284 | public function getGroupSize() |
||
| 291 | } |
||
| 292 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.