Loading...

This is taking longer than expected.

Coding standards

Laravel Traits

These are the standards the analyzer applies. Every rule states what it checks and why it exists.

All standards

Traits & Reuse

  • When logic is reused across classes, place it in a Trait
  • No copy/paste logic, no duplication across components
  • A trait is for behavior that operates on $this. A pure function that only transforms its arguments — formatting or normalizing a value — belongs in a helper, because a trait would make it a method of every class that uses it
  • Trait location by type:
    • Model traits → app/Traits/ (App\Traits)
    • Livewire traits → app/Livewire/Traits/ (App\Livewire\Traits)
  • When a trait encapsulates a feature, keep that feature self-contained: properties, relationships, scopes, methods, and any trait-owned observer registration via bootTraitName(). Model-specific observers keep only behavior unique to that model.
  • Name a feature trait after the feature it encapsulates — HasReferrals for the referral feature — and use it in the model that owns the feature.
  • Extract feature traits only for features a model has but is not. Do not move a model's own domain methods into a trait just to satisfy the count.

Trait Section Order

  • When a trait contains methods from more than one section, the section comments must be present (the division). Section comments are optional only when the trait has a single section. The wording is free, as in models, within the same limit: at most four words naming the group, no closing period. What is checked is that a comment sits above the first method of each section, written in the multi-line /* */ form.
  • The order is relative, not mandatory: Static Functions → Functions → Scopes → Relationships → Attributes. Only fail when methods that actually exist are out of order; never fail because a section is absent.
  • Properties are declared at the top of the trait, before any method.
  • Accessors and mutators in a trait belong in the Attributes section: a mutator such as setPinAttribute() must not live under Functions, even when it is the trait's only attribute method.
  • A section comment applies to all following methods until the next section comment or the end of the trait. Do not require accessors/mutators to be physically inside a comment block; PHP comments are markers, not wrappers.
  • If the response determines a section order is allowed, it must pass that point. Do not list allowed ordering observations as failures.

Relationships Are Pure Queries

A relationship method returns a query the caller keeps shaping; it must not persist anything. Do not call firstOrCreate(), create(), updateOrCreate(), or save() inside a relationship method. When a record must exist before the relation can be read, materialize it in an explicit named method (e.g. ensureBillingAccount()) that callers invoke when they are about to write, and keep the relationship itself a pure query. When a package is absent, return a neutral empty relationship rather than writing a fallback record.

Weblabor Logo Developed by Weblabor