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 |
||
| 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 | 'user_type', |
||
| 80 | 'plan_id', |
||
| 81 | 'slug', |
||
| 82 | 'name', |
||
| 83 | 'description', |
||
| 84 | 'trial_ends_at', |
||
| 85 | 'starts_at', |
||
| 86 | 'ends_at', |
||
| 87 | 'cancels_at', |
||
| 88 | 'canceled_at', |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | protected $casts = [ |
||
| 95 | 'user_id' => 'integer', |
||
| 96 | 'user_type' => 'string', |
||
| 97 | 'plan_id' => 'integer', |
||
| 98 | 'slug' => 'string', |
||
| 99 | 'trial_ends_at' => 'datetime', |
||
| 100 | 'starts_at' => 'datetime', |
||
| 101 | 'ends_at' => 'datetime', |
||
| 102 | 'cancels_at' => 'datetime', |
||
| 103 | 'canceled_at' => 'datetime', |
||
| 104 | 'deleted_at' => 'datetime', |
||
| 105 | ]; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | protected $observables = [ |
||
| 111 | 'validating', |
||
| 112 | 'validated', |
||
| 113 | ]; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The attributes that are translatable. |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | public $translatable = [ |
||
| 121 | 'name', |
||
| 122 | 'description', |
||
| 123 | ]; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The default rules that the model will validate against. |
||
| 127 | * |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | protected $rules = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Whether the model should throw a |
||
| 134 | * ValidationException if it fails validation. |
||
| 135 | * |
||
| 136 | * @var bool |
||
| 137 | */ |
||
| 138 | protected $throwValidationExceptions = true; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Create a new Eloquent model instance. |
||
| 142 | * |
||
| 143 | * @param array $attributes |
||
| 144 | */ |
||
| 145 | public function __construct(array $attributes = []) |
||
| 146 | { |
||
| 147 | parent::__construct($attributes); |
||
| 148 | |||
| 149 | $this->setTable(config('rinvex.subscriptions.tables.plan_subscriptions')); |
||
| 150 | $this->setRules([ |
||
| 151 | 'name' => 'required|string|max:150', |
||
| 152 | 'description' => 'nullable|string|max:10000', |
||
| 153 | 'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.subscriptions.tables.plan_subscriptions').',slug', |
||
| 154 | 'plan_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plans').',id', |
||
| 155 | 'user_id' => 'required|integer', |
||
| 156 | 'user_type' => 'required|string', |
||
| 157 | 'trial_ends_at' => 'nullable|date', |
||
| 158 | 'starts_at' => 'required|date', |
||
| 159 | 'ends_at' => 'required|date', |
||
| 160 | 'cancels_at' => 'nullable|date', |
||
| 161 | 'canceled_at' => 'nullable|date', |
||
| 162 | ]); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | protected static function boot() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the options for generating the slug. |
||
| 181 | * |
||
| 182 | * @return \Spatie\Sluggable\SlugOptions |
||
| 183 | */ |
||
| 184 | public function getSlugOptions(): SlugOptions |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the owning user. |
||
| 194 | * |
||
| 195 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 196 | */ |
||
| 197 | public function user(): MorphTo |
||
| 201 | |||
| 202 | /** |
||
| 203 | * The subscription may have many usage. |
||
| 204 | * |
||
| 205 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 206 | */ |
||
| 207 | public function usage(): hasMany |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Check if subscription is active. |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function active(): bool |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if subscription is inactive. |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function inactive(): bool |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Check if subscription is currently on trial. |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function onTrial(): bool |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Check if subscription is canceled. |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public function canceled(): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Check if subscription period has ended. |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function ended(): bool |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Cancel subscription. |
||
| 264 | * |
||
| 265 | * @param bool $immediately |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function cancel($immediately = false) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Change subscription plan. |
||
| 284 | * |
||
| 285 | * @param \Rinvex\Subscriptions\Models\Plan $plan |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function changePlan(Plan $plan) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Renew subscription period. |
||
| 309 | * |
||
| 310 | * @throws \LogicException |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function renew() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get bookings of the given user. |
||
| 337 | * |
||
| 338 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 339 | * @param \Illuminate\Database\Eloquent\Model $user |
||
| 340 | * |
||
| 341 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 342 | */ |
||
| 343 | public function scopeOfUser(Builder $builder, Model $user): Builder |
||
| 344 | { |
||
| 345 | return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey()); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Scope subscriptions with ending trial. |
||
| 350 | * |
||
| 351 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 352 | * @param int $dayRange |
||
| 353 | * |
||
| 354 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 355 | */ |
||
| 356 | public function scopeFindEndingTrial(Builder $builder, int $dayRange = 3): Builder |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Scope subscriptions with ended trial. |
||
| 366 | * |
||
| 367 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 368 | * |
||
| 369 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 370 | */ |
||
| 371 | public function scopeFindEndedTrial(Builder $builder): Builder |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Scope subscriptions with ending periods. |
||
| 378 | * |
||
| 379 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 380 | * @param int $dayRange |
||
| 381 | * |
||
| 382 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 383 | */ |
||
| 384 | public function scopeFindEndingPeriod(Builder $builder, int $dayRange = 3): Builder |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Scope subscriptions with ended periods. |
||
| 394 | * |
||
| 395 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 396 | * |
||
| 397 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 398 | */ |
||
| 399 | public function scopeFindEndedPeriod(Builder $builder): Builder |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set new subscription period. |
||
| 406 | * |
||
| 407 | * @param string $invoice_interval |
||
| 408 | * @param int $invoice_period |
||
| 409 | * @param string $start |
||
| 410 | * |
||
| 411 | * @return $this |
||
| 412 | */ |
||
| 413 | protected function setNewPeriod($invoice_interval = '', $invoice_period = '', $start = '') |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Record feature usage. |
||
| 433 | * |
||
| 434 | * @param string $featureSlug |
||
| 435 | * @param int $uses |
||
| 436 | * |
||
| 437 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage |
||
| 438 | */ |
||
| 439 | public function recordFeatureUsage(string $featureSlug, int $uses = 1, bool $incremental = true): PlanSubscriptionUsage |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Reduce usage. |
||
| 471 | * |
||
| 472 | * @param string $featureSlug |
||
| 473 | * @param int $uses |
||
| 474 | * |
||
| 475 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage|null |
||
| 476 | */ |
||
| 477 | public function reduceFeatureUsage(string $featureSlug, int $uses = 1): ?PlanSubscriptionUsage |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Determine if the feature can be used. |
||
| 494 | * |
||
| 495 | * @param string $featureSlug |
||
| 496 | * |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | public function canUseFeature(string $featureSlug): bool |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get how many times the feature has been used. |
||
| 520 | * |
||
| 521 | * @param string $featureSlug |
||
| 522 | * |
||
| 523 | * @return int |
||
| 524 | */ |
||
| 525 | public function getFeatureUsage(string $featureSlug): int |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get the available uses. |
||
| 534 | * |
||
| 535 | * @param string $featureSlug |
||
| 536 | * |
||
| 537 | * @return int |
||
| 538 | */ |
||
| 539 | public function getFeatureRemainings(string $featureSlug): int |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get feature value. |
||
| 546 | * |
||
| 547 | * @param string $featureSlug |
||
| 548 | * |
||
| 549 | * @return mixed |
||
| 550 | */ |
||
| 551 | public function getFeatureValue(string $featureSlug) |
||
| 557 | } |
||
| 558 |
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.