Complex classes like BookableBooking 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 BookableBooking, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class BookableBooking extends Model |
||
| 15 | { |
||
| 16 | use ValidatingTrait; |
||
| 17 | use CacheableEloquent; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * {@inheritdoc} |
||
| 21 | */ |
||
| 22 | protected $fillable = [ |
||
| 23 | 'bookable_id', |
||
| 24 | 'bookable_type', |
||
| 25 | 'customer_id', |
||
| 26 | 'customer_type', |
||
| 27 | 'starts_at', |
||
| 28 | 'ends_at', |
||
| 29 | 'price', |
||
| 30 | 'currency', |
||
| 31 | 'price_equation', |
||
| 32 | 'cancelled_at', |
||
| 33 | 'notes', |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | protected $casts = [ |
||
| 40 | 'bookable_id' => 'integer', |
||
| 41 | 'bookable_type' => 'string', |
||
| 42 | 'customer_id' => 'integer', |
||
| 43 | 'customer_type' => 'string', |
||
| 44 | 'starts_at' => 'datetime', |
||
| 45 | 'ends_at' => 'datetime', |
||
| 46 | 'price' => 'float', |
||
| 47 | 'currency' => 'string', |
||
| 48 | 'price_equation' => 'json', |
||
| 49 | 'cancelled_at' => 'datetime', |
||
| 50 | 'notes' => 'string', |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | protected $observables = [ |
||
| 57 | 'validating', |
||
| 58 | 'validated', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The default rules that the model will validate against. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $rules = [ |
||
| 67 | 'bookable_id' => 'required|integer', |
||
| 68 | 'bookable_type' => 'required|string', |
||
| 69 | 'customer_id' => 'required|integer', |
||
| 70 | 'customer_type' => 'required|string', |
||
| 71 | 'starts_at' => 'required|date', |
||
| 72 | 'ends_at' => 'required|date', |
||
| 73 | 'price' => 'required|numeric', |
||
| 74 | 'currency' => 'required|alpha|size:3', |
||
| 75 | 'price_equation' => 'nullable|array', |
||
| 76 | 'cancelled_at' => 'nullable|date', |
||
| 77 | 'notes' => 'nullable|string|max:10000', |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Whether the model should throw a |
||
| 82 | * ValidationException if it fails validation. |
||
| 83 | * |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $throwValidationExceptions = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Create a new Eloquent model instance. |
||
| 90 | * |
||
| 91 | * @param array $attributes |
||
| 92 | */ |
||
| 93 | public function __construct(array $attributes = []) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | protected static function boot() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Calculate the booking price. |
||
| 119 | * |
||
| 120 | * @param \Illuminate\Database\Eloquent\Model $bookable |
||
| 121 | * @param \Carbon\Carbon $startsAt |
||
| 122 | * @param \Carbon\Carbon $endsAt |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function calculatePrice(Model $bookable, Carbon $startsAt, Carbon $endsAt = null): array |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the owning resource model. |
||
| 199 | * |
||
| 200 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 201 | */ |
||
| 202 | public function bookable(): MorphTo |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get the booking customer. |
||
| 209 | * |
||
| 210 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 211 | */ |
||
| 212 | public function customer(): MorphTo |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get bookings of the given resource. |
||
| 219 | * |
||
| 220 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 221 | * @param \Illuminate\Database\Eloquent\Model $bookable |
||
| 222 | * |
||
| 223 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 224 | */ |
||
| 225 | public function scopeOfBookable(Builder $builder, Model $bookable): Builder |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get bookings of the given customer. |
||
| 232 | * |
||
| 233 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 234 | * @param \Illuminate\Database\Eloquent\Model $customer |
||
| 235 | * |
||
| 236 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 237 | */ |
||
| 238 | public function scopeOfCustomer(Builder $builder, Model $customer): Builder |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the past bookings. |
||
| 245 | * |
||
| 246 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 247 | * |
||
| 248 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 249 | */ |
||
| 250 | public function scopePast(Builder $builder): Builder |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get the future bookings. |
||
| 259 | * |
||
| 260 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 261 | * |
||
| 262 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 263 | */ |
||
| 264 | public function scopeFuture(Builder $builder): Builder |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the current bookings. |
||
| 273 | * |
||
| 274 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 275 | * |
||
| 276 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 277 | */ |
||
| 278 | public function scopeCurrent(Builder $builder): Builder |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get the cancelled bookings. |
||
| 289 | * |
||
| 290 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 291 | * |
||
| 292 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 293 | */ |
||
| 294 | public function scopeCancelled(Builder $builder): Builder |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get bookings starts before the given date. |
||
| 301 | * |
||
| 302 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 303 | * @param string $date |
||
| 304 | * |
||
| 305 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 306 | */ |
||
| 307 | public function scopeStartsBefore(Builder $builder, string $date): Builder |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get bookings starts after the given date. |
||
| 316 | * |
||
| 317 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 318 | * @param string $date |
||
| 319 | * |
||
| 320 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 321 | */ |
||
| 322 | public function scopeStartsAfter(Builder $builder, string $date): Builder |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get bookings starts between the given dates. |
||
| 331 | * |
||
| 332 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 333 | * @param string $startsAt |
||
| 334 | * @param string $endsAt |
||
| 335 | * |
||
| 336 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 337 | */ |
||
| 338 | public function scopeStartsBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get bookings ends before the given date. |
||
| 348 | * |
||
| 349 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 350 | * @param string $date |
||
| 351 | * |
||
| 352 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 353 | */ |
||
| 354 | public function scopeEndsBefore(Builder $builder, string $date): Builder |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get bookings ends after the given date. |
||
| 363 | * |
||
| 364 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 365 | * @param string $date |
||
| 366 | * |
||
| 367 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 368 | */ |
||
| 369 | public function scopeEndsAfter(Builder $builder, string $date): Builder |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get bookings ends between the given dates. |
||
| 378 | * |
||
| 379 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 380 | * @param string $startsAt |
||
| 381 | * @param string $endsAt |
||
| 382 | * |
||
| 383 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 384 | */ |
||
| 385 | public function scopeEndsBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get bookings cancelled before the given date. |
||
| 395 | * |
||
| 396 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 397 | * @param string $date |
||
| 398 | * |
||
| 399 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 400 | */ |
||
| 401 | public function scopeCancelledBefore(Builder $builder, string $date): Builder |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get bookings cancelled after the given date. |
||
| 409 | * |
||
| 410 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 411 | * @param string $date |
||
| 412 | * |
||
| 413 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 414 | */ |
||
| 415 | public function scopeCancelledAfter(Builder $builder, string $date): Builder |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get bookings cancelled between the given dates. |
||
| 423 | * |
||
| 424 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 425 | * @param string $startsAt |
||
| 426 | * @param string $endsAt |
||
| 427 | * |
||
| 428 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 429 | */ |
||
| 430 | public function scopeCancelledBetween(Builder $builder, string $startsAt, string $endsAt): Builder |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get bookings between the given dates. |
||
| 439 | * |
||
| 440 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 441 | * @param string $startsAt |
||
| 442 | * @param string $endsAt |
||
| 443 | * |
||
| 444 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 445 | */ |
||
| 446 | public function scopeRange(Builder $builder, string $startsAt, string $endsAt): Builder |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Check if the booking is cancelled. |
||
| 462 | * |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | public function isCancelled(): bool |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check if the booking is past. |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | public function isPast(): bool |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Check if the booking is future. |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | public function isFuture(): bool |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Check if the booking is current. |
||
| 492 | * |
||
| 493 | * @return bool |
||
| 494 | */ |
||
| 495 | public function isCurrent(): bool |
||
| 499 | } |
||
| 500 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.