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 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | protected $fillable = [ |
||
| 77 | 'subscriber_id', |
||
| 78 | 'subscriber_type', |
||
| 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 | 'subscriber_id' => 'integer', |
||
| 95 | 'subscriber_type' => 'string', |
||
| 96 | 'plan_id' => 'integer', |
||
| 97 | 'slug' => 'string', |
||
| 98 | 'trial_ends_at' => 'datetime', |
||
| 99 | 'starts_at' => 'datetime', |
||
| 100 | 'ends_at' => 'datetime', |
||
| 101 | 'cancels_at' => 'datetime', |
||
| 102 | 'canceled_at' => 'datetime', |
||
| 103 | 'deleted_at' => 'datetime', |
||
| 104 | ]; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | protected $observables = [ |
||
| 110 | 'validating', |
||
| 111 | 'validated', |
||
| 112 | ]; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The attributes that are translatable. |
||
| 116 | * |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | public $translatable = [ |
||
| 120 | 'name', |
||
| 121 | 'description', |
||
| 122 | ]; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The default rules that the model will validate against. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $rules = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Whether the model should throw a |
||
| 133 | * ValidationException if it fails validation. |
||
| 134 | * |
||
| 135 | * @var bool |
||
| 136 | */ |
||
| 137 | protected $throwValidationExceptions = true; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Create a new Eloquent model instance. |
||
| 141 | * |
||
| 142 | * @param array $attributes |
||
| 143 | */ |
||
| 144 | public function __construct(array $attributes = []) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | protected static function boot() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the options for generating the slug. |
||
| 180 | * |
||
| 181 | * @return \Spatie\Sluggable\SlugOptions |
||
| 182 | */ |
||
| 183 | public function getSlugOptions(): SlugOptions |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the owning subscriber. |
||
| 193 | * |
||
| 194 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 195 | */ |
||
| 196 | public function subscriber(): MorphTo |
||
| 197 | { |
||
| 198 | return $this->morphTo('subscriber', 'subscriber_type', 'subscriber_id', 'id'); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The subscription may have many usage. |
||
| 203 | * |
||
| 204 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 205 | */ |
||
| 206 | public function usage(): hasMany |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Check if subscription is active. |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function active(): bool |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Check if subscription is inactive. |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function inactive(): bool |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Check if subscription is currently on trial. |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function onTrial(): bool |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Check if subscription is canceled. |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function canceled(): bool |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Check if subscription period has ended. |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function ended(): bool |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Cancel subscription. |
||
| 263 | * |
||
| 264 | * @param bool $immediately |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function cancel($immediately = false) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Change subscription plan. |
||
| 283 | * |
||
| 284 | * @param \Rinvex\Subscriptions\Models\Plan $plan |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function changePlan(Plan $plan) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Renew subscription period. |
||
| 308 | * |
||
| 309 | * @throws \LogicException |
||
| 310 | * |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function renew() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get bookings of the given subscriber. |
||
| 336 | * |
||
| 337 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 338 | * @param \Illuminate\Database\Eloquent\Model $subscriber |
||
| 339 | * |
||
| 340 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 341 | */ |
||
| 342 | public function scopeOfSubscriber(Builder $builder, Model $subscriber): Builder |
||
| 343 | { |
||
| 344 | return $builder->where('subscriber_type', $subscriber->getMorphClass())->where('subscriber_id', $subscriber->getKey()); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Scope subscriptions with ending trial. |
||
| 349 | * |
||
| 350 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 351 | * @param int $dayRange |
||
| 352 | * |
||
| 353 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 354 | */ |
||
| 355 | public function scopeFindEndingTrial(Builder $builder, int $dayRange = 3): Builder |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Scope subscriptions with ended trial. |
||
| 365 | * |
||
| 366 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 367 | * |
||
| 368 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 369 | */ |
||
| 370 | public function scopeFindEndedTrial(Builder $builder): Builder |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Scope subscriptions with ending periods. |
||
| 377 | * |
||
| 378 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 379 | * @param int $dayRange |
||
| 380 | * |
||
| 381 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 382 | */ |
||
| 383 | public function scopeFindEndingPeriod(Builder $builder, int $dayRange = 3): Builder |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Scope subscriptions with ended periods. |
||
| 393 | * |
||
| 394 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 395 | * |
||
| 396 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 397 | */ |
||
| 398 | public function scopeFindEndedPeriod(Builder $builder): Builder |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set new subscription period. |
||
| 405 | * |
||
| 406 | * @param string $invoice_interval |
||
| 407 | * @param int $invoice_period |
||
|
|
|||
| 408 | * @param string $start |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | protected function setNewPeriod($invoice_interval = '', $invoice_period = '', $start = '') |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Record feature usage. |
||
| 432 | * |
||
| 433 | * @param string $featureSlug |
||
| 434 | * @param int $uses |
||
| 435 | * |
||
| 436 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage |
||
| 437 | */ |
||
| 438 | public function recordFeatureUsage(string $featureSlug, int $uses = 1, bool $incremental = true): PlanSubscriptionUsage |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Reduce usage. |
||
| 470 | * |
||
| 471 | * @param string $featureSlug |
||
| 472 | * @param int $uses |
||
| 473 | * |
||
| 474 | * @return \Rinvex\Subscriptions\Models\PlanSubscriptionUsage|null |
||
| 475 | */ |
||
| 476 | public function reduceFeatureUsage(string $featureSlug, int $uses = 1): ?PlanSubscriptionUsage |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Determine if the feature can be used. |
||
| 493 | * |
||
| 494 | * @param string $featureSlug |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | public function canUseFeature(string $featureSlug): bool |
||
| 499 | { |
||
| 500 | $featureValue = $this->getFeatureValue($featureSlug); |
||
| 501 | $usage = $this->usage()->byFeatureSlug($featureSlug)->first(); |
||
| 502 | |||
| 503 | if ($featureValue === 'true') { |
||
| 504 | return true; |
||
| 505 | } |
||
| 506 | |||
| 507 | // If the feature value is zero, let's return false since |
||
| 508 | // there's no uses available. (useful to disable countable features) |
||
| 509 | if (! $usage || $usage->expired() || is_null($featureValue) || $featureValue === '0' || $featureValue === 'false') { |
||
| 510 | return false; |
||
| 511 | } |
||
| 512 | |||
| 513 | // Check for available uses |
||
| 514 | return $this->getFeatureRemainings($featureSlug) > 0; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Get how many times the feature has been used. |
||
| 519 | * |
||
| 520 | * @param string $featureSlug |
||
| 521 | * |
||
| 522 | * @return int |
||
| 523 | */ |
||
| 524 | public function getFeatureUsage(string $featureSlug): int |
||
| 525 | { |
||
| 526 | $usage = $this->usage()->byFeatureSlug($featureSlug)->first(); |
||
| 527 | |||
| 528 | return (! $usage || $usage->expired()) ? 0 : $usage->used; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get the available uses. |
||
| 533 | * |
||
| 534 | * @param string $featureSlug |
||
| 535 | * |
||
| 536 | * @return int |
||
| 537 | */ |
||
| 538 | public function getFeatureRemainings(string $featureSlug): int |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Get feature value. |
||
| 545 | * |
||
| 546 | * @param string $featureSlug |
||
| 547 | * |
||
| 548 | * @return mixed |
||
| 549 | */ |
||
| 550 | public function getFeatureValue(string $featureSlug) |
||
| 556 | } |
||
| 557 |
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.