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

Typed Laravel Package

spatie/typed

Userland improved PHP type system with type inference and runtime checking: generics, union types, typed collections/lists, tuples, and structs. Proof-of-concept package from Spatie to add stronger type guarantees without language-level support.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Generics & Advanced Typing: The package introduces generics, typed collections (lists, tuples), and structs—features absent in PHP’s native type system. This aligns well with projects requiring strong typing for complex data structures (e.g., DTOs, domain models, or API responses) or those migrating toward stricter type safety.
  • Laravel Compatibility: While Laravel leverages PHP’s native types (e.g., array, mixed), this package could enhance internal tooling (e.g., validation layers, service containers, or event dispatching) where generics improve clarity and reduce runtime errors.
  • Legacy Codebase Risk: If the codebase relies heavily on dynamic typing (e.g., array for everything), adoption may require refactoring to unlock benefits. Best suited for greenfield projects or modular components.

Integration Feasibility

  • Core PHP 7.4+: Requires PHP 7.4+ (no PHP 8.x-specific features). Works alongside Laravel’s existing type system without conflicts.
  • No Laravel-Specific Hooks: Operates at the PHP level, so integration is library-agnostic. Can be used in:
    • Service layers (e.g., typed repositories).
    • Validation (e.g., typed input/output contracts).
    • Testing (e.g., typed mocks or assertions).
  • Build Tooling: May require custom PHPStan/NikicPHP rules to enforce types, as IDE autocompletion won’t natively support generics.

Technical Risk

  • Archived Status: Last release in 2020 raises concerns about:
    • PHP 8.x+ Compatibility: May lack support for newer features (e.g., named arguments, union types).
    • Maintenance: No active updates; bugs or security issues won’t be patched.
  • Performance Overhead: Generics in PHP are emulated at runtime (not native), which could introduce minor overhead for large collections.
  • Adoption Friction:
    • Developers must opt into the syntax (e.g., List<string>, Tuple<int, string>), which may feel verbose.
    • Tooling gaps: Limited IDE support (e.g., PHPStorm’s generic inference is basic).

Key Questions

  1. Why generics? What specific pain points (e.g., runtime type errors, unclear APIs) does this solve vs. native PHP 8.1+ features (e.g., array<key-value>)?
  2. Migration Path: Can this be incrementally adopted (e.g., start with structs for DTOs) or does it require a big-bang refactor?
  3. Alternatives:
    • PHP 8.1+: Native array<mixed> and array-key types may suffice for collections.
    • Other Packages: rector/rector (for type migration) or phpstan/phpstan (static analysis).
  4. Long-Term Viability: If archived, is a fork justified, or should the team wait for a maintained alternative?
  5. Team Buy-In: How will developers react to new syntax in an existing codebase?

Integration Approach

Stack Fit

  • Best For:
    • Laravel + PHP 7.4–8.0: Projects where PHP 8.1+ isn’t an option.
    • Internal Libraries: Where typed contracts (e.g., service responses) improve maintainability.
    • Legacy Systems: Gradual migration toward stricter typing.
  • Poor Fit:
    • PHP 8.1+ Projects: Native generics (array<>) reduce need for this package.
    • Dynamic APIs: If the system relies on runtime flexibility (e.g., array for everything).

Migration Path

  1. Pilot Phase:
    • Start with structs for DTOs (e.g., replace array with Struct{id: int, name: string}).
    • Use typed lists in validation layers (e.g., List<string> for tags).
  2. Incremental Adoption:
    • New Features: Only use generics in new modules.
    • Critical Path: Avoid generics in performance-sensitive code (e.g., bulk operations).
  3. Tooling Setup:
    • Configure PHPStan to enforce types (e.g., parameters.typed_properties: true).
    • Add custom rules to catch misuse (e.g., List<int> with strings).

Compatibility

  • Laravel-Specific:
    • Service Container: Can type-hint generics in bindings (e.g., bind(List<User>::class)).
    • Eloquent: Not directly compatible, but can wrap models in structs for APIs.
    • Blade: Avoid generics in views (PHP 7.4+ may not parse them correctly).
  • Third-Party Packages:
    • Risk: Some libraries (e.g., old array-heavy packages) may conflict.
    • Mitigation: Isolate generic usage behind adapters.

Sequencing

Phase Action Dependencies
Assessment Audit codebase for array/mixed usage; identify high-impact areas. PHPStan report
Pilot Refactor 1–2 modules to use structs/lists. Team agreement on syntax
Tooling Configure PHPStan/NikicPHP for generics. PHPStan 0.12+
Rollout Expand to new features; avoid legacy code. CI pipeline updates
Review Measure reduction in runtime type errors. Error logs, test coverage

Operational Impact

Maintenance

  • Pros:
    • Reduced Runtime Errors: Catches type mismatches early (e.g., List<int> with a string).
    • Self-Documenting Code: Generics make APIs clearer (e.g., processUsers(List<User>)).
  • Cons:
    • Debugging Complexity: Stack traces for generic errors may be harder to read.
    • Tooling Dependencies: Relies on PHPStan/NikicPHP for enforcement; if these fail, types break silently.
  • Documentation:
    • Requires internal docs on generic usage patterns (e.g., "Use Struct for DTOs, not array").

Support

  • Developer Onboarding:
    • Learning Curve: New syntax (List<T>, Tuple) requires training.
    • IDE Limitations: PHPStorm/VSCode may not autocomplete generics perfectly.
  • Troubleshooting:
    • Common Issues:
      • "Type error: Expected List<string>, got array" (fix: use List::fromArray()).
      • PHPStan false positives (requires custom rules).
    • No Official Support: Team must rely on GitHub issues or forks.

Scaling

  • Performance:
    • Minimal Impact: Runtime overhead is negligible for most use cases (generics are emulated).
    • Large Collections: Avoid List<T> for millions of items; use native arrays.
  • Team Scaling:
    • Consistency: Enforces typing discipline across teams.
    • Hiring: May deter PHP devs unfamiliar with generics.

Failure Modes

Risk Impact Mitigation
PHP Version Incompatibility Fails on PHP <7.4 or >8.0. Pin to PHP 7.4–8.0 in Docker.
Tooling Breakage PHPStan/NikicPHP misconfigures. Test generics in CI pre-merge.
Adoption Resistance Team rejects new syntax. Start with opt-in modules.
Archived Package Risks No security updates. Fork or replace with PHP 8.1+.
Runtime Errors Generic violations in prod. Strict PHPStan in CI.

Ramp-Up

  • Timeline:
    • Pilot: 2–4 weeks (1–2 modules).
    • Full Rollout: 3–6 months (depends on codebase size).
  • Success Metrics:
    • Reduction in: Runtime type errors (track via Sentry/Error logs).
    • Improvement in: Code review speed (fewer runtime bugs).
  • Rollback Plan:
    • Partial: Remove generics from problematic modules.
    • Full: Replace with PHP 8.1+ native types if available.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport