Complex classes like PlanSubscription 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 PlanSubscription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 66 | class PlanSubscription extends Model implements PlanSubscriptionContract |
||
| 67 | { |
||
| 68 | use HasSlug; |
||
| 69 | use BelongsToPlan; |
||
| 70 | use HasTranslations; |
||
| 71 | use ValidatingTrait; |
||
| 72 | use CacheableEloquent; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | protected $fillable = [ |
||
| 78 | 'user_id', |
||
| 79 | 'plan_id', |
||
| 80 | 'slug', |
||
| 81 | 'name', |
||
| 82 | 'description', |
||
| 83 | 'trial_ends_at', |
||
| 84 | 'starts_at', |
||
| 85 | 'ends_at', |
||
| 86 | 'cancels_at', |
||
| 87 | 'canceled_at', |
||
| 88 | ]; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | protected $casts = [ |
||
| 94 | 'user_id' => 'integer', |
||
| 95 | 'plan_id' => 'integer', |
||
| 96 | 'slug' => 'string', |
||
| 97 | 'trial_ends_at' => 'datetime', |
||
| 98 | 'starts_at' => 'datetime', |
||
| 99 | 'ends_at' => 'datetime', |
||
| 100 | 'cancels_at' => 'datetime', |
||
| 101 | 'canceled_at' => 'datetime', |
||
| 102 | 'deleted_at' => 'datetime', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | protected $observables = [ |
||
| 109 | 'validating', |
||
| 110 | 'validated', |
||
| 111 | ]; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The attributes that are translatable. |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | public $translatable = [ |
||
| 119 | 'name', |
||
| 120 | 'description', |
||
| 121 | ]; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The default rules that the model will validate against. |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $rules = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Whether the model should throw a |
||
| 132 | * ValidationException if it fails validation. |
||
| 133 | * |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | protected $throwValidationExceptions = true; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Create a new Eloquent model instance. |
||
| 140 | * |
||
| 141 | * @param array $attributes |
||
| 142 | */ |
||
| 143 | public function __construct(array $attributes = []) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Boot function for using with User Events. |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | protected static function boot() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the options for generating the slug. |
||
| 183 | * |
||
| 184 | * @return \Spatie\Sluggable\SlugOptions |
||
| 185 | */ |
||
| 186 | public function getSlugOptions(): SlugOptions |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The subscription must always belongs to a user. |
||
| 196 | * |
||
| 197 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 198 | */ |
||
| 199 | public function user(): belongsTo |
||
| 203 | |||
| 204 | /** |
||
| 205 | * The subscription may have many usage. |
||
| 206 | * |
||
| 207 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 208 | */ |
||
| 209 | public function usage(): hasMany |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Check if subscription is active. |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function active() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Check if subscription is inactive. |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function inactive() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Check if subscription is currently on trial. |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | public function onTrial(): bool |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Check if subscription is canceled. |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function canceled(): bool |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Check if subscription period has ended. |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function ended(): bool |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Cancel subscription. |
||
| 266 | * |
||
| 267 | * @param bool $immediately |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function cancel($immediately = false) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Change subscription plan. |
||
| 286 | * |
||
| 287 | * @param \Rinvex\Subscriptions\Models\Plan $plan |
||
| 288 | * |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function changePlan(Plan $plan) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Renew subscription period. |
||
| 311 | * |
||
| 312 | * @throws \LogicException |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function renew() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Scope subscriptions by user id. |
||
| 339 | * |
||
| 340 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 341 | * @param int $userId |
||
| 342 | * |
||
| 343 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 344 | */ |
||
| 345 | public function scopeByUserId(Builder $builder, int $userId): Builder |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Scope subscriptions with ending trial. |
||
| 352 | * |
||
| 353 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 354 | * @param int $dayRange |
||
| 355 | * |
||
| 356 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 357 | */ |
||
| 358 | public function scopeFindEndingTrial(Builder $builder, int $dayRange = 3): Builder |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Scope subscriptions with ended trial. |
||
| 368 | * |
||
| 369 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 370 | * |
||
| 371 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 372 | */ |
||
| 373 | public function scopeFindEndedTrial(Builder $builder): Builder |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Scope subscriptions with ending periods. |
||
| 380 | * |
||
| 381 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 382 | * @param int $dayRange |
||
| 383 | * |
||
| 384 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 385 | */ |
||
| 386 | public function scopeFindEndingPeriod(Builder $builder, int $dayRange = 3): Builder |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Scope subscriptions with ended periods. |
||
| 396 | * |
||
| 397 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 398 | * |
||
| 399 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 400 | */ |
||
| 401 | public function scopeFindEndedPeriod(Builder $builder): Builder |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Set new subscription period. |
||
| 408 | * |
||
| 409 | * @param string $invoice_interval |
||
| 410 | * @param int $invoice_period |
||
| 411 | * @param string $start |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | protected function setNewPeriod($invoice_interval = '', $invoice_period = '', $start = '') |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Record feature usage. |
||
| 435 | * |
||
| 436 | * @param string $featureSlug |
||
| 437 | * @param int $uses |
||
| 438 | * |
||
| 439 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage |
||
| 440 | */ |
||
| 441 | public function recordFeatureUsage(string $featureSlug, int $uses = 1, bool $incremental = true): PlanSubscriptionUsage |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Reduce usage. |
||
| 473 | * |
||
| 474 | * @param string $featureSlug |
||
| 475 | * @param int $uses |
||
| 476 | * |
||
| 477 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage|null |
||
| 478 | */ |
||
| 479 | public function reduceFeatureUsage(string $featureSlug, int $uses = 1) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Determine if the feature can be used. |
||
| 496 | * |
||
| 497 | * @param string $featureSlug |
||
| 498 | * |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | public function canUseFeature(string $featureSlug): bool |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get how many times the feature has been used. |
||
| 522 | * |
||
| 523 | * @param string $featureSlug |
||
| 524 | * |
||
| 525 | * @return int |
||
| 526 | */ |
||
| 527 | public function getFeatureUsage(string $featureSlug): int |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get the available uses. |
||
| 536 | * |
||
| 537 | * @param string $featureSlug |
||
| 538 | * |
||
| 539 | * @return int |
||
| 540 | */ |
||
| 541 | public function getFeatureRemainings(string $featureSlug): int |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Get feature value. |
||
| 548 | * |
||
| 549 | * @param string $featureSlug |
||
| 550 | * |
||
| 551 | * @return mixed |
||
| 552 | */ |
||
| 553 | public function getFeatureValue(string $featureSlug) |
||
| 559 | } |
||
| 560 |
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.