Complex classes like Sprint 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 Sprint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Sprint extends Model |
||
| 16 | { |
||
| 17 | use SoftDeletes; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The database table used by the model. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $table = 'sprints'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Attributes that should be mass-assignable. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $fillable = ['user_id', 'product_backlog_id', 'slug', 'title', 'description', 'version', |
||
| 32 | 'is_private', 'date_start', 'date_finish', 'state', 'color', 'position', 'closed_at', ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The attributes excluded from the model's JSON form. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $hidden = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The attributes that should be casted to native types. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $casts = []; |
||
| 47 | |||
| 48 | protected static function boot() |
||
| 52 | |||
| 53 | public function productBacklog() |
||
| 57 | |||
| 58 | public function branches() |
||
| 62 | |||
| 63 | public function issues() |
||
| 68 | |||
| 69 | public function issuesHasUsers() |
||
| 70 | { |
||
| 71 | $users = $this->issues->map(function ($issue) { |
||
| 72 | return $issue->users; |
||
| 73 | })->reject(function ($value) { |
||
| 74 | return $value == null; |
||
| 75 | })->flatten(1)->unique('id')->splice(0, 3); |
||
| 76 | |||
| 77 | return $users->all(); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function comments() |
||
| 85 | |||
| 86 | public function attachments() |
||
| 90 | |||
| 91 | public function notes() |
||
| 96 | |||
| 97 | public function favorite() |
||
| 101 | |||
| 102 | public function status() |
||
| 106 | |||
| 107 | public function pullrequests() |
||
| 119 | |||
| 120 | public function getPercentComplete() |
||
| 121 | { |
||
| 122 | $total = $this->issues->count(); |
||
| 123 | $totalClosed = $total - $this->issues->where('closed_at', null)->count(); |
||
| 124 | |||
| 125 | return ($totalClosed) ? ceil(($totalClosed * 100) / $total) : 0; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function notesPercentComplete() |
||
| 129 | { |
||
| 130 | $total = $this->notes->count(); |
||
| 131 | $totalClosed = $total - $this->notes->where('completed_at', null)->count(); |
||
| 132 | |||
| 133 | return ($totalClosed) ? ceil(($totalClosed * 100) / $total) : 0; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function totalAdditions() |
||
| 146 | |||
| 147 | public function totalPullRequests() |
||
| 155 | |||
| 156 | public function workingDays($start = null) |
||
| 178 | |||
| 179 | public function weeks($start = null) |
||
| 186 | |||
| 187 | public function getPSR2Errors() |
||
| 201 | |||
| 202 | public function getEffort() |
||
| 208 | |||
| 209 | public function avgEffort() |
||
| 215 | |||
| 216 | public function burdown() |
||
| 217 | { |
||
| 218 | $helper = new Helper(); |
||
| 219 | $total = $this->issues->count(); |
||
| 220 | $finished = (Carbon::now() > $this->attributes['date_finish']) ? Carbon::now() : $this->attributes['date_finish']; |
||
| 221 | $finished = (is_null($this->attributes['closed_at'])) ? $finished : $this->attributes['closed_at']; |
||
| 222 | |||
| 223 | $dates = $helper->arrayDateRange([$this->attributes['date_start'], $finished], $total); |
||
| 224 | |||
| 225 | $previous = $this->attributes['date_start']; |
||
| 226 | $arr = []; |
||
| 227 | $arr[$previous] = $total; |
||
| 228 | |||
| 229 | foreach ($dates as $date => $value) { |
||
| 230 | $closed = $this->issues()->whereDate('closed_at', '=', $date)->count(); |
||
| 231 | $totalPrevious = $total - $arr[$previous]; |
||
| 232 | $arr[$date] = $total - ($closed + $totalPrevious); |
||
| 233 | $previous = $date; |
||
| 234 | } |
||
| 235 | |||
| 236 | return $arr; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function activities() |
||
| 240 | { |
||
| 241 | $activities = $this->issues() |
||
| 242 | ->with('statuses')->get()->map(function ($issue) { |
||
| 243 | return $issue->statuses; |
||
| 244 | })->flatten(1)->map(function ($statuses) { |
||
| 245 | return $statuses; |
||
| 246 | })->sortByDesc('created_at'); |
||
| 247 | |||
| 248 | $activities->splice(15); |
||
| 249 | |||
| 250 | return $activities->all(); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function issueTypes() |
||
| 268 | |||
| 269 | public function issueStatus() |
||
| 277 | |||
| 278 | public function getVisibilityAttribute() |
||
| 282 | |||
| 283 | public function getSlugAttribute() |
||
| 287 | |||
| 288 | public function getTimeboxAttribute() |
||
| 289 | { |
||
| 290 | $date_start = isset($this->attributes['date_start']) ? |
||
| 291 | Carbon::parse($this->attributes['date_start'])->toDateString() : ''; |
||
| 292 | $date_finish = isset($this->attributes['date_finish']) ? |
||
| 296 | } |
||
| 297 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.