solido/phpstan-rules
Custom PHPStan ruleset for Solido-based projects. Adds extra static analysis for enhanced DTOs with simple phpstan.neon config to declare DTO namespaces and excluded interfaces, helping catch type and structure issues across your application.
Install the Package Require the package in your Laravel project via Composer:
composer require --dev solido/phpstan-rules
Ensure phpstan/phpstan is also installed (required).
Configure PHPStan
Update your phpstan.neon configuration file (typically in the project root) to include Solido-specific rules:
includes:
- vendor/solido/phpstan-rules/extension.neon
parameters:
solido:
dto_namespaces:
- App\DTO
- App\Models\DTOs
excluded_interfaces:
- App\DTO\NonDTOInterface
Run PHPStan Execute PHPStan with the new rules to analyze your codebase:
vendor/bin/phpstan analyse src --level=5
Focus on identifying DTO-related issues, such as invalid property access or type mismatches.
DTO Validation Workflow
solido.dto.constructorUsage rule.solido.dto.propertyAccess rule to validate that only allowed properties are accessed on DTO objects.Type Safety Enforcement
solido.dto.typeMismatch rule helps catch type inconsistencies in DTO properties, ensuring that properties are assigned values of the correct type.Custom Rule Integration
extension.neon file to tailor the rules to your project’s specific needs:
services:
- Solido\PhpStanRules\Rules\DtoPropertyAccessRule
- Solido\PhpStanRules\Rules\DtoTypeMismatchRule
CI/CD Pipeline Integration
# Example GitHub Actions workflow
name: PHPStan Analysis
on: [push, pull_request]
jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/phpstan analyse --level=5
Laravel-Specific Patterns
Gradual Adoption
--level=3) to identify issues without blocking development. Gradually increase the severity level as your team becomes more comfortable with the rules.Team Onboarding
Custom Suppression
// phpcs:ignore solido.dto.propertyAccess
$dto->nonExistentProperty;
Rule Prioritization
Incorrect Namespace Configuration
dto_namespaces in phpstan.neon is incomplete or incorrect, the rules may not apply to all DTOs, leading to missed violations.App\DTO\*), as they can lead to unintended matches.False Positives
__get() or __set().excluded_interfaces parameter or suppress the rule for specific lines of code.Performance Issues
vendor/bin/phpstan analyse src --memory-limit=1G
Rule Conflicts
PropertyTypeMismatch vs. solido.dto.typeMismatch) can lead to redundant or conflicting error messages.extension.neon file or disabling conflicting native rules.Outdated Rules
Enable Debug Mode
vendor/bin/phpstan analyse --debug
Generate Reports
vendor/bin/phpstan analyse --generate-report=report.html
Isolate Issues
vendor/bin/phpstan analyse src/DTO --level=5
Custom Rules
Rule Configuration
phpstan.neon. For example, you can exclude specific interfaces or adjust the severity level of certain rules.Integration with Other Tools
Automated Fixes
--fix option (if available) or create custom scripts to automatically fix common issues, such as type mismatches or invalid property access.DTOs in API Resources
Form Request Validation
Service Container Binding
solido.dto.constructorUsage rule.Testing Integration
How can I help you explore Laravel packages today?