Complex classes like Fight 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 Fight, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Fight extends Model |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Fight constructor. |
||
| 12 | * @param int $userId1 |
||
| 13 | * @param int $userId2 |
||
| 14 | */ |
||
| 15 | public function __construct($userId1 = null, $userId2 = null) |
||
| 20 | |||
| 21 | protected $table = 'fight'; |
||
| 22 | public $timestamps = true; |
||
| 23 | |||
| 24 | protected $fillable = [ |
||
| 25 | 'group_id', |
||
| 26 | 'c1', |
||
| 27 | 'c2', |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param $group |
||
| 32 | * @param $competitor1 |
||
| 33 | * @param $competitor2 |
||
| 34 | * @param $order |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | private static function createFight($group, $competitor1, $competitor2, $order) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get First Fighter. |
||
| 51 | * |
||
| 52 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 53 | */ |
||
| 54 | public function group() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Championship $championship |
||
| 61 | * |
||
| 62 | * @return Collection |
||
| 63 | */ |
||
| 64 | private static function getActorsToFights(Championship $championship, FightersGroup $group = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get First Fighter. |
||
| 81 | * |
||
| 82 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 83 | */ |
||
| 84 | public function competitor1() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get Second Fighter. |
||
| 91 | * |
||
| 92 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 93 | */ |
||
| 94 | public function competitor2() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get First Fighter. |
||
| 101 | * |
||
| 102 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 103 | */ |
||
| 104 | public function team1() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get Second Fighter. |
||
| 111 | * |
||
| 112 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 113 | */ |
||
| 114 | public function team2() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Save a Fight. |
||
| 121 | * |
||
| 122 | * @param Collection $groups |
||
| 123 | * @param int $numGroup |
||
| 124 | */ |
||
| 125 | public static function savePreliminaryFightGroup($groups, $numGroup = 1) |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * @param Championship $championship |
||
| 156 | */ |
||
| 157 | public static function saveGroupFights(Championship $championship) |
||
| 190 | |||
| 191 | public function getFighterAttr($numFighter, $attr) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Update parent Fight |
||
| 211 | * @param $fighterToUpdate |
||
| 212 | * @param $fight |
||
| 213 | */ |
||
| 214 | public function updateParentFight($fighterToUpdate, $fight) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns the parent field that need to be updated |
||
| 232 | * @return null|string |
||
| 233 | */ |
||
| 234 | public function getParentFighterToUpdate() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * In the original fight ( child ) return the field that contains data to copy to parent |
||
| 253 | * @return null|string |
||
| 254 | */ |
||
| 255 | public function getValueToUpdate() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Check if we are able to fill the parent fight or not |
||
| 271 | * If one of the children has c1 x c2, then we must wait to fill parent |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | function hasDeterminedParent() |
||
| 285 | |||
| 286 | } |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.