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

Better Types Laravel Package

spatie/better-types

Reflection-powered type checking for PHP: verify whether a ReflectionType or method signature accepts given arguments (including unions/nullables and named params). Useful for dispatching/overload-like method selection and safer dynamic calls.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Type Safety & Developer Experience (DX):

    • Enables stricter type validation in Laravel/PHP applications, reducing runtime errors and improving maintainability.
    • Supports union types (e.g., string|int) and named types (e.g., custom classes), aligning with modern PHP 8+ features.
    • Useful for APIs, form validation, and data processing where input validation is critical.
  • Build vs. Buy:

    • Buy: Avoid reinventing type-checking logic (e.g., manual instanceof checks or regex validation).
    • Build: Only if needing highly customized type logic (e.g., domain-specific constraints).
  • Roadmap Priorities:

    • Phase 1: Integrate into core validation layers (e.g., Laravel’s FormRequest or API gateways).
    • Phase 2: Extend to serialization/deserialization (e.g., JSON API payloads) or event dispatching.
    • Phase 3: Explore IDE integration (e.g., PhpStorm hints) for proactive type safety.
  • Use Cases:

    • APIs: Validate incoming requests (e.g., Accepts: User|Admin).
    • Forms: Enforce type constraints (e.g., Email|PhoneNumber).
    • Legacy Systems: Gradually introduce type safety without full refactoring.

When to Consider This Package

  • Adopt if:

    • Your team uses PHP 8+ and wants to leverage union/named types without verbose checks.
    • You’re building scalable APIs or data pipelines where input validation is critical.
    • You prioritize developer productivity (reduces boilerplate for type validation).
    • Your stack includes Laravel (seamless integration with existing validation tools).
  • Look elsewhere if:

    • You’re on PHP < 8.0 (package requires PHP 8+ features).
    • Your type needs are trivial (e.g., only basic string|int checks).
    • You need runtime performance optimization (this adds minimal overhead but isn’t a micro-optimization tool).
    • Your team lacks PHP 8+ adoption or type-hinting culture.

How to Pitch It (Stakeholders)

For Executives: "This package lets us enforce stricter type rules in our PHP/Laravel apps—catching bugs early and reducing runtime errors. It’s like adding a ‘compile-time guardrail’ for data validation, which cuts debugging time and improves API/form reliability. Low risk (MIT license, battle-tested by Spatie), high reward for developer productivity."

For Engineers: *"Spatie’s better-types gives us a clean way to validate union/named types (e.g., User|Admin or custom classes) without manual instanceof spaghetti. Works seamlessly with Laravel’s validation, and the syntax is intuitive:

if (BetterType::isType($input, User::class | Admin::class)) { ... }

It’s a drop-in upgrade for type safety—no refactoring needed. Perfect for APIs, forms, or legacy systems where we want to modernize validation."*

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
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
twbs/bootstrap4