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 |
||
| 67 | class PlanSubscription extends Model |
||
| 68 | { |
||
| 69 | use HasSlug; |
||
| 70 | use SoftDeletes; |
||
| 71 | use BelongsToPlan; |
||
| 72 | use HasTranslations; |
||
| 73 | use ValidatingTrait; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | protected $fillable = [ |
||
| 79 | 'subscriber_id', |
||
| 80 | 'subscriber_type', |
||
| 81 | 'plan_id', |
||
| 82 | 'slug', |
||
| 83 | 'name', |
||
| 84 | 'description', |
||
| 85 | 'trial_ends_at', |
||
| 86 | 'starts_at', |
||
| 87 | 'ends_at', |
||
| 88 | 'cancels_at', |
||
| 89 | 'canceled_at', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | protected $casts = [ |
||
| 96 | 'subscriber_id' => 'integer', |
||
| 97 | 'subscriber_type' => 'string', |
||
| 98 | 'plan_id' => 'integer', |
||
| 99 | 'slug' => 'string', |
||
| 100 | 'trial_ends_at' => 'datetime', |
||
| 101 | 'starts_at' => 'datetime', |
||
| 102 | 'ends_at' => 'datetime', |
||
| 103 | 'cancels_at' => 'datetime', |
||
| 104 | 'canceled_at' => 'datetime', |
||
| 105 | 'deleted_at' => 'datetime', |
||
| 106 | ]; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | protected $observables = [ |
||
| 112 | 'validating', |
||
| 113 | 'validated', |
||
| 114 | ]; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The attributes that are translatable. |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | public $translatable = [ |
||
| 122 | 'name', |
||
| 123 | 'description', |
||
| 124 | ]; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The default rules that the model will validate against. |
||
| 128 | * |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | protected $rules = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Whether the model should throw a |
||
| 135 | * ValidationException if it fails validation. |
||
| 136 | * |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | protected $throwValidationExceptions = true; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create a new Eloquent model instance. |
||
| 143 | * |
||
| 144 | * @param array $attributes |
||
| 145 | */ |
||
| 146 | public function __construct(array $attributes = []) |
||
| 147 | { |
||
| 148 | parent::__construct($attributes); |
||
| 149 | |||
| 150 | $this->setTable(config('rinvex.subscriptions.tables.plan_subscriptions')); |
||
| 151 | $this->setRules([ |
||
| 152 | 'name' => 'required|string|strip_tags|max:150', |
||
| 153 | 'description' => 'nullable|string|max:32768', |
||
| 154 | 'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.subscriptions.tables.plan_subscriptions').',slug', |
||
| 155 | 'plan_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plans').',id', |
||
| 156 | 'subscriber_id' => 'required|integer', |
||
| 157 | 'subscriber_type' => 'required|string|strip_tags|max:150', |
||
| 158 | 'trial_ends_at' => 'nullable|date', |
||
| 159 | 'starts_at' => 'required|date', |
||
| 160 | 'ends_at' => 'required|date', |
||
| 161 | 'cancels_at' => 'nullable|date', |
||
| 162 | 'canceled_at' => 'nullable|date', |
||
| 163 | ]); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | protected static function boot() |
||
| 170 | { |
||
| 171 | parent::boot(); |
||
| 172 | |||
| 173 | static::validating(function (self $model) { |
||
| 174 | if (! $model->starts_at || ! $model->ends_at) { |
||
| 175 | $model->setNewPeriod(); |
||
| 176 | } |
||
| 177 | }); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the options for generating the slug. |
||
| 182 | * |
||
| 183 | * @return \Spatie\Sluggable\SlugOptions |
||
| 184 | */ |
||
| 185 | public function getSlugOptions(): SlugOptions |
||
| 186 | { |
||
| 187 | return SlugOptions::create() |
||
| 188 | ->doNotGenerateSlugsOnUpdate() |
||
| 189 | ->generateSlugsFrom('name') |
||
| 190 | ->saveSlugsTo('slug'); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the owning subscriber. |
||
| 195 | * |
||
| 196 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 197 | */ |
||
| 198 | public function subscriber(): MorphTo |
||
| 199 | { |
||
| 200 | return $this->morphTo('subscriber', 'subscriber_type', 'subscriber_id', 'id'); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * The subscription may have many usage. |
||
| 205 | * |
||
| 206 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 207 | */ |
||
| 208 | public function usage(): hasMany |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Check if subscription is active. |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function active(): bool |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if subscription is inactive. |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function inactive(): bool |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check if subscription is currently on trial. |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function onTrial(): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Check if subscription is canceled. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function canceled(): bool |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Check if subscription period has ended. |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function ended(): bool |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Cancel subscription. |
||
| 265 | * |
||
| 266 | * @param bool $immediately |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function cancel($immediately = false) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Change subscription plan. |
||
| 285 | * |
||
| 286 | * @param \Rinvex\Subscriptions\Models\Plan $plan |
||
| 287 | * |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function changePlan(Plan $plan) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Renew subscription period. |
||
| 310 | * |
||
| 311 | * @throws \LogicException |
||
| 312 | * |
||
| 313 | * @return $this |
||
| 314 | */ |
||
| 315 | public function renew() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get bookings of the given subscriber. |
||
| 338 | * |
||
| 339 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 340 | * @param \Illuminate\Database\Eloquent\Model $subscriber |
||
| 341 | * |
||
| 342 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 343 | */ |
||
| 344 | public function scopeOfSubscriber(Builder $builder, Model $subscriber): Builder |
||
| 345 | { |
||
| 346 | return $builder->where('subscriber_type', $subscriber->getMorphClass())->where('subscriber_id', $subscriber->getKey()); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Scope subscriptions with ending trial. |
||
| 351 | * |
||
| 352 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 353 | * @param int $dayRange |
||
| 354 | * |
||
| 355 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 356 | */ |
||
| 357 | public function scopeFindEndingTrial(Builder $builder, int $dayRange = 3): Builder |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Scope subscriptions with ended trial. |
||
| 367 | * |
||
| 368 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 369 | * |
||
| 370 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 371 | */ |
||
| 372 | public function scopeFindEndedTrial(Builder $builder): Builder |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Scope subscriptions with ending periods. |
||
| 379 | * |
||
| 380 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 381 | * @param int $dayRange |
||
| 382 | * |
||
| 383 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 384 | */ |
||
| 385 | public function scopeFindEndingPeriod(Builder $builder, int $dayRange = 3): Builder |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Scope subscriptions with ended periods. |
||
| 395 | * |
||
| 396 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 397 | * |
||
| 398 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 399 | */ |
||
| 400 | public function scopeFindEndedPeriod(Builder $builder): Builder |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set new subscription period. |
||
| 407 | * |
||
| 408 | * @param string $invoice_interval |
||
| 409 | * @param int $invoice_period |
||
|
|
|||
| 410 | * @param string $start |
||
| 411 | * |
||
| 412 | * @return $this |
||
| 413 | */ |
||
| 414 | protected function setNewPeriod($invoice_interval = '', $invoice_period = '', $start = '') |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Record feature usage. |
||
| 434 | * |
||
| 435 | * @param string $featureSlug |
||
| 436 | * @param int $uses |
||
| 437 | * |
||
| 438 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage |
||
| 439 | */ |
||
| 440 | public function recordFeatureUsage(string $featureSlug, int $uses = 1, bool $incremental = true): PlanSubscriptionUsage |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Reduce usage. |
||
| 472 | * |
||
| 473 | * @param string $featureSlug |
||
| 474 | * @param int $uses |
||
| 475 | * |
||
| 476 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage|null |
||
| 477 | */ |
||
| 478 | public function reduceFeatureUsage(string $featureSlug, int $uses = 1): ?PlanSubscriptionUsage |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Determine if the feature can be used. |
||
| 495 | * |
||
| 496 | * @param string $featureSlug |
||
| 497 | * |
||
| 498 | * @return bool |
||
| 499 | */ |
||
| 500 | public function canUseFeature(string $featureSlug): bool |
||
| 501 | { |
||
| 502 | $featureValue = $this->getFeatureValue($featureSlug); |
||
| 503 | $usage = $this->usage()->byFeatureSlug($featureSlug)->first(); |
||
| 504 | |||
| 505 | if ($featureValue === 'true') { |
||
| 506 | return true; |
||
| 507 | } |
||
| 508 | |||
| 509 | // If the feature value is zero, let's return false since |
||
| 510 | // there's no uses available. (useful to disable countable features) |
||
| 511 | if (! $usage || $usage->expired() || is_null($featureValue) || $featureValue === '0' || $featureValue === 'false') { |
||
| 512 | return false; |
||
| 513 | } |
||
| 514 | |||
| 515 | // Check for available uses |
||
| 516 | return $this->getFeatureRemainings($featureSlug) > 0; |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get how many times the feature has been used. |
||
| 521 | * |
||
| 522 | * @param string $featureSlug |
||
| 523 | * |
||
| 524 | * @return int |
||
| 525 | */ |
||
| 526 | public function getFeatureUsage(string $featureSlug): int |
||
| 527 | { |
||
| 528 | $usage = $this->usage()->byFeatureSlug($featureSlug)->first(); |
||
| 529 | |||
| 530 | return (! $usage || $usage->expired()) ? 0 : $usage->used; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get the available uses. |
||
| 535 | * |
||
| 536 | * @param string $featureSlug |
||
| 537 | * |
||
| 538 | * @return int |
||
| 539 | */ |
||
| 540 | public function getFeatureRemainings(string $featureSlug): int |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get feature value. |
||
| 547 | * |
||
| 548 | * @param string $featureSlug |
||
| 549 | * |
||
| 550 | * @return mixed |
||
| 551 | */ |
||
| 552 | public function getFeatureValue(string $featureSlug) |
||
| 558 | } |
||
| 559 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.