A concise guide to essential PHP development tools that will enhance your coding experience and maintain high code quality.
Tool | Description | Jump to Section |
---|---|---|
π§Ή | Code Linting - Keep your code clean and consistent | Go |
βοΈ | PECK - Eliminate embarrassing typos | Go |
π | PHPStan - Static analysis for type safety | Go |
π | Rector - Automated code refactoring | Go |
β | Pest - Modern PHP testing framework | Go |
π | Composer Scripts - Automate all the things | Go |
π | Quick Reference - Commands at a glance | Go |
Laravel Pint is an elegant PHP code style fixer built on top of PHP-CS-Fixer with sensible defaults.
# Install Pint
composer require laravel/pint --dev
# Run Pint
./vendor/bin/pint
{
"preset": "laravel",
"rules": {
"array_push": true,
"backtick_to_shell_exec": true,
"date_time_immutable": true,
"declare_strict_types": true,
"lowercase_keywords": true,
"lowercase_static_reference": true,
"final_class": true,
"final_internal_class": true,
"final_public_method_for_abstract_class": true,
"fully_qualified_strict_types": true,
"global_namespace_import": {
"import_classes": true,
"import_constants": true,
"import_functions": true
},
"mb_str_functions": true,
"modernize_types_casting": true,
"new_with_parentheses": false,
"no_superfluous_elseif": true,
"no_useless_else": true,
"no_multiple_statements_per_line": true,
"ordered_class_elements": {
"order": [
"use_trait",
"case",
"constant",
"constant_public",
"constant_protected",
"constant_private",
"property_public",
"property_protected",
"property_private",
"construct",
"destruct",
"magic",
"phpunit",
"method_abstract",
"method_public_static",
"method_public",
"method_protected_static",
"method_protected",
"method_private_static",
"method_private"
],
"sort_algorithm": "none"
},
"ordered_interfaces": true,
"ordered_traits": true,
"protected_to_private": true,
"self_accessor": true,
"self_static_accessor": true,
"strict_comparison": true,
"visibility_required": true
}
}
# Install PHP-CS-Fixer
composer require friendsofphp/php-cs-fixer --dev
# Run PHP-CS-Fixer
./vendor/bin/php-cs-fixer fix src/
<?php
$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
])
->setFinder($finder);
PECK (PHP Extension for Code Korrection) helps catch typos in variable names, methods, and comments.
# Install PECK
composer require --dev peck/peck
# Run PECK
./vendor/bin/peck
{
"preset": "laravel",
"ignore": {
"words": [
"config",
"namespace"
],
"paths": [
"app/MyFolder",
"app/MyFile.php"
]
}
}
# Install PHPStan
composer require --dev phpstan/phpstan
# Run PHPStan
./vendor/bin/phpstan analyse
parameters:
level: 7
paths:
- src
- tests
excludePaths:
- tests/Fixtures/*
checkMissingIterableValueType: false
# Install Larastan
composer require --dev larastan/larastan
# Run Larastan
./vendor/bin/phpstan analyse
Rector helps upgrade your code to newer PHP versions and apply modern coding standards.
# Install Rector
composer require --dev rector/rector
# Run Rector (dry-run)
./vendor/bin/rector process --dry-run
# Apply changes
./vendor/bin/rector process
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
// Target PHP 8.1 features
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
]);
};
Pest is an elegant PHP testing framework with a focus on simplicity.
# Install Pest
composer remove phpunit/phpunit
composer require pestphp/pest --dev --with-all-dependencies
# Initialize Pest
./vendor/bin/pest --init
# Run Pest tests
./vendor/bin/pest
./vendor/bin/pest --parallel
# Install pcov or xdebug first
./vendor/bin/pest --coverage --min=80
./vendor/bin/pest --type-coverage --min=60
arch()
->expect('App')
->toUseStrictTypes()
->not->toUse(['die', 'dd', 'dump']);
arch()
->expect('App\Models')
->toBeClasses()
->toExtend('Illuminate\Database\Eloquent\Model')
->toOnlyBeUsedIn('App\Repositories')
->ignoring('App\Models\User');
arch()
->expect('App\Http')
->toOnlyBeUsedIn('App\Http');
arch()
->expect('App\*\Traits')
->toBeTraits();
arch()->preset()->php();
arch()->preset()->security()->ignoring('md5');
Sometimes, writing arch expectations from scratch can be time-consuming, specifically when working on a new project, and you just want to ensure that the basic architectural rules are met.
Presets are predefined sets of granular expectations that you can use to test your application's architecture.
php
- Basic PHP expectationssecurity
- Security-related expectationslaravel
- Laravel-specific expectationsstrict
- Strict expectations for your codebase
arch()->preset()->php();
arch()->preset()->security();
arch()->preset()->laravel();
arch()->preset()->strict();
Add these scripts to your composer.json:
{
"scripts": {
"lint": "pint",
"lint:check": "pint --test",
"refactor": "rector process",
"refactor:dry": "rector process --dry-run",
"analyse": "phpstan analyse",
"typos": "peck",
"test": "pest",
"test:parallel": "pest --parallel",
"test:coverage": "pest --coverage",
"test:type-coverage": "pest --type-coverage",
"check": [
"@lint:check",
"@refactor:dry",
"@analyse",
"@typos",
"@test:type-coverage",
"@test:parallel"
],
"fix": [
"@lint",
"@refactor"
]
}
}
# Before committing:
composer check # Run all checks to verify code quality
composer fix # Apply all automatic fixes
git add .
git commit -m "Clean, type-safe, and modern code!"
Tool | Purpose | Run With |
---|---|---|
Pint | Code style | composer lint |
PECK | Typo checking | composer typos |
PHPStan | Type checking | composer analyse |
Rector | Code modernization | composer refactor |
Pest | Testing | composer test |
All | Complete check | composer check |
All | Fix issues | composer fix |