Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Traitor Laravel Package

kkszymanowski/traitor

Traitor brings traits to Eloquent like first-class relations. Define reusable, composable model fragments and access them via $model->traitName, eager load with with('traitName'), and query through them—keeping models slim while sharing behavior and attributes.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require kkszymanowski/traitor. Once installed, import the Traitor facade or inject Traitor\TraitResolver directly. The most common first use case is inspecting all traits used by a class—including those brought in via nested traits—e.g., Traitor::forClass(MyService::class)->getAllTraits(). This replaces fragile regex searches or manual class_uses_recursive() calls, especially useful when auditing services for domain-specific traits (e.g., SoftDeletes, Timestamps, or custom domain traits like 可发布()). Start by running a quick sanity check in Tinker: Traitor::forClass(App\Models\User::class)->getAllTraitNames().

Implementation Patterns

  • Audit Scripts & Artisan Commands: Wrap trait discovery in custom commands (e.g., php artisan audit:traits) to flag classes missing expected traits or violating architectural rules (e.g., “no service may use Eventable trait without also using Validatable”).
  • Test Assertions: In feature/unit tests, assert that domain classes must include specific traits: $this->assertTrue(Traitor::forClass($subject)->hasTrait(WithPermissions::class)).
  • Dynamic Behavior Injection: Use trait resolution in factories or service providers to conditionally enable behavior based on trait presence (e.g., if (Traitor::forClass($model)->hasTrait(Translatable::class)) { ... }).
  • Documentation Generators: Feed resolved traits into tools like Laravel-zero or Laravel Dusk reports to auto-document domain model capabilities without parsing docblocks.
  • Refactoring Guards: In CI, run a trait-compliance check before deployment to ensure critical traits (e.g., AuditLoggable) aren’t accidentally removed during refactors.

Gotchas and Tips

  • Inheritance quirks: getAllTraits() returns all traits from the hierarchy (including inherited traits), but getDirectTraits() returns only those used directly in the class—ensure you choose the right method based on whether inheritance matters.
  • Trait name resolution: Trait names are returned as fully qualified class names (FQCN). Don’t assume short names; always use use or \Traitor\... aliases to avoid ambiguity.
  • Nested trait loops: While the package guards against infinite recursion, ensure your traits aren’t circularly dependent—e.g., Trait A uses Trait B and Trait B uses Trait A—as this is undefined behavior in PHP and may cause silent failures or crashes.
  • Performance: Avoid repeated instantiation for the same class; cache TraitResolver instances per class in long-running processes (e.g., CLI commands or queue workers). Use Traitor::forClassCached() if available in later versions.
  • Static vs runtime use: Though powerful, trait resolution is runtime-only—it won’t catch missing traits in compilation phase. Combine with PHPStan/Psalm rules if enforcing strict trait contracts.
  • Laravel facades: If using Traitor::forClass(...), ensure the facade is registered in config/app.php under 'Traitor' => Traitor\Facades\Traitor::class. Misregistration is a common first-time pitfall.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata