rector/type-perfect
Type Perfect adds 10 PHPStan rules to tighten type declarations: replace empty/isset checks with instanceof for precise narrowing, prefer explicit getters over magic array access, and enforce interface signatures. Easy to enable even on legacy code; improves reliability fast.
Architecture fit: Perfect alignment with Laravel's PHPStan-based static analysis ecosystem. The package extends PHPStan rules without modifying core framework behavior, making it ideal for Laravel's common legacy codebases where strict typing is desired but not fully implemented. It complements Laravel's existing tooling (e.g., Eloquent, Pest) by targeting type safety gaps that Laravel's default configuration misses.
Integration feasibility: High. Requires only composer require and minimal PHPStan config adjustments (via extension-installer or manual neon inclusion). Existing Laravel projects with PHPStan already configured can integrate in <15 minutes with zero code changes. Compatibility with Laravel 8+ (PHP 7.4+) and PHPStan 1.0+ is proven.
Technical risk: Moderate. Initial rule activation may surface hundreds of violations in legacy code, potentially breaking CI pipelines. Third-party dependencies (e.g., Laravel core, vendor packages) may trigger false positives due to dynamic patterns like magic methods or union returns. Requires careful exclusion configuration to avoid noise from external code.
Key questions:
mixed, null/false ambiguity, or untyped signatures?phpstan.neon)?Stack fit: Ideal for Laravel 8+ (PHP 7.4+) and Laravel 9+ (PHP 8.0+). Fully compatible with PHPStan 1.0+, Pest, PHPUnit, and existing phpstan.neon configurations. Works seamlessly with Laravel's service container and Eloquent models when configured to exclude magic property/method usage in specific contexts.
Migration path:
null_over_false and no_mixed_property rules. Fixes common false/null confusion and eliminates PHPStan blind spots for properties.no_mixed_caller to catch method typos and signature mismatches. Requires validation of dynamic method calls (e.g., Eloquent __call usage).narrow_param and narrow_return after validating concrete type usage in codebase. Start with private methods before public APIs.Compatibility: Works with existing PHPStan rule sets (e.g., larastan, symfony rules) without conflicts. Requires phpstan/extension-installer for automatic config loading or manual inclusion of vendor/rector/type-perfect/config/extension.neon in phpstan.neon.
Sequencing: Prioritize rules by business impact: null_over_false (prevents runtime errors from type confusion) → no_mixed_property (fixes PHPStan blind spots) → narrow_param (reduces parameter-related bugs) → narrow_return (improves API contract clarity). Test each phase in staging before CI enforcement.
Maintenance: Low ongoing effort after initial setup. Rules run automatically in CI pipelines with minimal configuration. Requires periodic review of exclusion lists for third-party code and validation of new rule violations. Updates to the package are infrequent and backward-compatible.
Support: Requires basic PHPStan knowledge to interpret violation reports (e.g., distinguishing false positives in magic methods). Documentation is clear but lacks Laravel-specific examples—team may need to create internal guides for common Laravel patterns (e.g., Eloquent model property typing). Community support via GitHub issues is responsive but limited due to small user base (101 stars).
Scaling: Rules scale linearly with codebase size. For large monorepos, use PHPStan's ignoreErrors to exclude vendor directories and legacy modules. Performance impact is negligible (PHPStan runs in milliseconds per file). Avoid enabling all rules simultaneously in codebases >50k LOC to prevent CI timeouts during initial runs.
Failure modes:
__call for Eloquent relationships) requiring exclusionsphpstan.neon leading to rule conflicts (e.g., duplicate parameters.type_perfect declarations)Ramp-up: Developers with PHPStan experience can onboard in <1 hour. New developers may need 2–4 hours to understand violation reports and fix strategies (e.g., converting false returns to null). Provide quick-reference guides for common fixes (e.g., "How to handle mixed property in a Model class"). Team training should focus on interpreting no_mixed_caller errors for method typos and narrow_return for factory methods.
How can I help you explore Laravel packages today?