sllh/composer-lint
Composer plugin that extends composer validate with extra linting rules for composer.json. Installs globally or per project, auto-enables on install, and can be configured via COMPOSER_HOME/config.json.
Installation:
composer require --dev sllh/composer-lint
or globally:
composer global require sllh/composer-lint
First Use Case: Run the extended validation command:
composer validate
This will enforce default rules (php, type, minimum-stability, version-constraints) and fail if any are violated.
Configuration:
Edit ~/.config/composer/config.json (global) or .composer/config.json (project-specific) to enable/disable rules:
{
"config": {
"sllh-composer-lint": {
"php": true,
"type": false, // Disable if not needed
"minimum-stability": false, // Disable for dev environments
"version-constraints": true
}
}
}
Pre-Commit Hooks:
Integrate with tools like Husky or Laravel Git Hooks to run composer validate before commits:
composer validate --no-check-publish
--no-check-publish to skip publishing packages (irrelevant for validation).CI/CD Pipeline: Add to Laravel’s CI (e.g., GitHub Actions) as a pre-deployment step:
- name: Validate composer.json
run: composer validate --strict
--strict ensures CI fails on violations (recommended for production pipelines).Team Onboarding:
Use as a developer checklist in CONTRIBUTING.md:
"Run
composer validatebefore submitting PRs to ensurecomposer.jsonadheres to project standards."
Project-Specific Rules:
Override global config in .composer/config.json for team-specific needs:
{
"config": {
"sllh-composer-lint": {
"version-constraints": {
"allowed": ["^", "~", "==", ">=", "<="] // Customize allowed operators
}
}
}
}
Fixing Violations:
php: ^8.1 to require.~2.0 with ^2.0 (or vice versa if configured)."type": "library" to composer.json.minimum-stability or set to dev for local dev.Debugging:
Run with --verbose for detailed error messages:
composer validate --verbose
Partial Validation:
Validate only specific sections (e.g., require):
composer validate --section=require
Laravel-Specific:
laravel-zero-config or laravel-mix projects to ensure PHP version constraints align with Laravel’s requirements.composer normalize for additional formatting (e.g., sorting packages).Monorepos:
sort-packages if using tools like Laravel Sail or Bedrock with complex dependency structures.Legacy Projects:
version-constraints, then php).--no-dev to skip dev-dependency checks during migration.Composer Version Mismatch:
Class not found or Invalid argument often indicate compatibility issues.False Positives:
minimum-stability Rule: May flag legitimate uses (e.g., dev dependencies or local projects).
Fix: Disable globally or per-project:
"minimum-stability": false
type Rule: Might reject custom package types (e.g., metapackage).
Fix: Whitelist types in config or disable the rule.Performance:
composer.json: Validation slows down with >500 dependencies.
Fix: Run in CI only or disable non-critical rules (e.g., sort-packages).Config Overrides:
.composer/config.json) and document overrides.CI Flakiness:
Error Messages:
[Error] PHP requirement missing in "require" section.
[Error] Invalid version constraint "~2.0" in "require": use "^2.0" instead.
--verbose to see full rule details.Rule-Specific Debugging:
php: ^8.1 exists in require."type" is set to library, project, etc.Config Validation:
composer config --list | grep sllh-composer-lint
Custom Rules:
composer-normalize for additional checks).Post-Validation Hooks:
post-validate-cmd script to run custom logic:
{
"scripts": {
"post-validate-cmd": [
"@php artisan optimize:clear",
"@php scripts/post-validate.php"
]
}
}
Integration with PHPStan/Laravel Pint:
composer validate to your phpstan.neon or pint.json workflows for a unified linting pipeline.Laravel-Specific:
composer.json PHP constraints with Laravel’s supported versions.minimum-stability in CI but enable it locally for dev dependencies.Team Adoption:
DEPENDENCIES.md file explaining why each rule exists (e.g., "We use ^ for version constraints to avoid breaking changes").composer why-not or composer why to debug constraint issues.Performance:
Fallbacks:
composer-normalize as a temporary replacement until a fork is available.How can I help you explore Laravel packages today?