Installation:
composer require ajgon/lint-pack:1.2.*@dev
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
Ajgon\LintPackBundle\LintPackBundle::class => ['all' => true],
Basic Configuration:
Add minimal config to config/packages/lint_pack.yaml (Symfony 4+) or app/config/config.yml (Symfony 2/3):
lint_pack:
phpcs:
locations:
- "%kernel.project_dir%/src"
First Use Case: Run a linter via console:
php bin/console lint:phpcs
php bin/console lint.%kernel.root_dir% and %kernel.root_dir%/../src. Override via locations.Integrate into CI/CD: Add linting to your pipeline (e.g., GitHub Actions, GitLab CI):
# Example GitHub Actions step
- name: Run PHPCS
run: php bin/console lint:phpcs --format=json > phpcs-results.json
Customize Linter Output:
Use --format flags (e.g., --format=json) for machine-readable output. Parse results in PHP:
$output = shell_exec('php bin/console lint:phpcs --format=json');
$results = json_decode($output, true);
Combine Linters: Run multiple linters sequentially in a script:
#!/bin/bash
php bin/console lint:phpcs && php bin/console lint:phpmd
Dynamic Configuration: Load configurations from environment variables or services:
# config/packages/lint_pack.yaml
lint_pack:
phpcs:
standard: "%env(CODE_STANDARD)%"
Pre-Commit Hooks: Use tools like Husky to run linters before commits:
# .husky/pre-commit
#!/bin/sh
php bin/console lint:phpcs --dry-run || exit 1
config/packages for modular configurations.csslint) into your container if not globally installed.parallel --jobs 4 php bin/console lint:{} ::: phpcs phpmd phpcpd jshint
src/Command/ in the bundle).Binary Paths:
csslint or jshint may not be globally installed. Specify full paths in config:
lint_pack:
jshint:
bin: "/usr/local/bin/jshint"
phpcs), use vendor/bin/ paths:
lint_pack:
phpcs:
bin: "vendor/bin/phpcs"
Ignores vs. Ignore Files:
ignores config (regex patterns) and jshintignore/phpcs.ignore files serve different purposes. Avoid mixing them unless intentional.jshintignore disables the ignores config entirely.Performance:
--extensions to limit file types:
lint_pack:
phpcs:
extensions: ["php", "inc"]
lint_pack:
phpcs:
ignores:
- "vendor/"
Symfony 4+ Compatibility:
config/packages/ is used instead of app/config/.Twig Linter:
twigviews linter is basic (regex-based). For advanced Twig linting, consider twig-lint.Dry Runs:
Use --dry-run to test configurations without executing linters:
php bin/console lint:phpcs --dry-run
Verbose Output: Enable debug mode for detailed logs:
php bin/console lint:phpcs -vvv
Check Binary Availability: Validate linter binaries are accessible:
which phpcs jshint csslint || echo "Binaries not found"
Configuration Validation: Test config snippets in isolation:
# config/packages/test_lint_pack.yaml
lint_pack:
phpcs:
locations: ["%kernel.project_dir%/src"]
standard: "PSR12"
Then run:
php bin/console lint:phpcs --env=test
Custom Commands:
Extend the bundle by creating a new command in src/Command/:
namespace App\Command;
use Ajgon\LintPackBundle\Command\AbstractLintCommand;
class CustomLintCommand extends AbstractLintCommand {
protected function configure() {
$this->setName('lint:custom');
}
protected function execute(InputInterface $input, OutputInterface $output) {
// Custom logic
}
}
Event Listeners:
Hook into linter execution via Symfony events (e.g., console.command):
// src/EventListener/LintListener.php
public function onLintCommand(ConsoleCommandEvent $event) {
if (strpos($event->getCommand()->getName(), 'lint:') === 0) {
// Pre/post-processing
}
}
Override Templates:
Customize output formats by overriding the bundle’s Twig templates (located in Resources/views/).
Add New Linters:
Implement a new linter by extending AbstractLintCommand and registering it as a service:
services:
App\Command\NewLintCommand:
tags: ['console.command']
How can I help you explore Laravel packages today?