boundwize/structarmed
StructArmed is a dev-only PHP architecture guard: define layers and dependency rules, start from presets (PSR-4/1/12, MVC, DDD), then tune or skip checks in PHP. Run it in CI to catch boundary violations before they become conventions.
StructArmed is a static architecture enforcement tool designed to validate PHP/Laravel projects against predefined architectural rules (e.g., layer isolation, PSR standards, DDD, MVC). It aligns well with Laravel’s modular, layered architecture (e.g., App/, Domain/, Infrastructure/), making it ideal for enforcing:
Domain → Application → Infrastructure).Key Strengths:
structarmed.php (e.g., layer(), ruleset(), presets).replaceRule() or rule().Potential Gaps:
--disable-parallel).Laravel Compatibility:
app/, src/, database/).StructArmedExtension.Migration Path:
composer require --dev boundwize/structarmed).structarmed analyse --generate-baseline).PSR4, PSR12).DDD, MVC).layer() or layerPattern() (e.g., Domain, Application).ruleset().Example Laravel Integration:
// structarmed.php
use Boundwize\StructArmed\Architecture;
use Boundwize\StructArmed\Preset\Preset;
return Architecture::define()
->layer('Domain', 'app/Domain/')
->layer('Application', 'app/Application/')
->layer('Infrastructure', 'app/Infrastructure/')
->ruleset([
'Domain' => [], // Domain has no dependencies
'Application' => ['Domain'], // App can depend on Domain
'Infrastructure' => ['Application'], // Infra can depend on App
])
->withPresets(Preset::PSR12(), Preset::DDD())
->skipPaths(['tests/', 'vendor/']);
| Risk Area | Assessment | Mitigation |
|---|---|---|
| False Positives | Custom rules or complex layer patterns may flag legitimate dependencies. | Use skipClassViolation() for exceptions; test rules incrementally. |
| Performance | Parallel analysis may slow CI/CD for large codebases. | Disable parallel mode (--disable-parallel) or limit paths. |
| Rule Maintenance | Custom rules require ongoing updates as architecture evolves. | Document rules in structarmed.php; use presets where possible. |
| Laravel-Specific Quirks | Facades, Blade, or dynamic class loading may trigger false violations. | Exclude paths (e.g., resources/) or use skipPathsForRuleset(). |
| Tooling Ecosystem | Limited adoption (0 dependents) may lack community support. | Leverage GitHub issues/Slack for questions; contribute custom presets. |
Key Questions for TPM:
BaseController")?cacheDirectory)?StructArmed is language-agnostic but optimally fits Laravel’s modular, layered architecture. Key alignments:
app/, src/, or custom namespaces.Laravel-Specific Considerations:
skipPaths() to avoid false positives.resources/views/ from ruleset checks.App\Services\* can be bound to Illuminate\Contracts\*").| Phase | Action | Tools/Commands |
|---|---|---|
| Assessment | Audit current architecture against target (e.g., DDD, Clean Architecture). | Manual review + structarmed analyse --preset=all --report=json. |
| Baseline Creation | Generate a baseline to capture existing violations. | structarmed analyse --generate-baseline=baseline.php. |
| Preset Selection | Start with lightweight presets (e.g., PSR12, PSR4). |
Edit structarmed.php; run structarmed analyse. |
| Layer Definition | Map Laravel directories to StructArmed layers (e.g., Domain, Infrastructure). |
Use layer() or layerPattern() in config. |
| Rule Enforcement | Incrementally add stricter rules (e.g., DDD, MVC). |
Use withPreset(); test with --report=json for CI integration. |
| Customization | Add project-specific rules (e.g., "API layer must not depend on Domain"). | Use rule() or replaceRule() in structarmed.php. |
| CI/CD Integration | Fail builds on violations or require baseline compliance. | Add to phpunit.xml or GitHub Actions workflow. |
| Legacy Handling | Skip known violations or refactor incrementally. | Use skipPaths() or skipClassViolation(). |
Example Migration Timeline:
PSR12.Domain, Application), enforce DDD presets.DB::raw()").| Component | Compatibility | Notes |
|---|---|---|
| PHP Version | 8.1+ (Laravel 9+ compatible). | Check composer.json for PHP version. |
| Laravel Version | 9 |
How can I help you explore Laravel packages today?