Skip to content

Instantly share code, notes, and snippets.

@PickleBoxer
Last active April 17, 2025 11:32
Show Gist options
  • Save PickleBoxer/9d49911fe3fa1a3f1e6dc9fef3184d35 to your computer and use it in GitHub Desktop.
Save PickleBoxer/9d49911fe3fa1a3f1e6dc9fef3184d35 to your computer and use it in GitHub Desktop.
A concise guide to essential PHP development tools that will enhance your coding experience and maintain high code quality.

πŸ› οΈ PHP Development Tools Guide

A concise guide to essential PHP development tools that will enhance your coding experience and maintain high code quality.

πŸ“‘ Table of Contents

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

🧹 Code Linting - Keep Your Codebase Clean

Laravel Pint

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

βš™οΈ Customizing Pint with pint.json

{
    "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
    }
}

PHP-CS-Fixer (For non-Laravel projects)

# Install PHP-CS-Fixer
composer require friendsofphp/php-cs-fixer --dev

# Run PHP-CS-Fixer
./vendor/bin/php-cs-fixer fix src/

Configuration (.php-cs-fixer.php)

<?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 - Eliminate Embarrassing Typos

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

Configuration (peck.json)

{
    "preset": "laravel",
    "ignore": {
        "words": [
            "config",
            "namespace"
        ],
        "paths": [
            "app/MyFolder",
            "app/MyFile.php"
        ]
    }
}

πŸ” PHPStan - Static Analysis for Type Safety

# Install PHPStan
composer require --dev phpstan/phpstan

# Run PHPStan
./vendor/bin/phpstan analyse

PHPStan Configuration (phpstan.neon)

parameters:
    level: 7
    paths:
        - src
        - tests
    excludePaths:
        - tests/Fixtures/*
    checkMissingIterableValueType: false

Larastan for Laravel Projects

# Install Larastan
composer require --dev larastan/larastan

# Run Larastan
./vendor/bin/phpstan analyse

πŸš€ Rector - Automated Code Refactoring and Modernization

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

Configuration (rector.php)

<?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 - Modern PHP Testing Framework

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

Parallel Testing

./vendor/bin/pest --parallel

Code Coverage

# Install pcov or xdebug first
./vendor/bin/pest --coverage --min=80

Type Coverage

./vendor/bin/pest --type-coverage --min=60

Architecture Testing

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');

Pest Presets

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 expectations
  • security - Security-related expectations
  • laravel - Laravel-specific expectations
  • strict - Strict expectations for your codebase
arch()->preset()->php();
arch()->preset()->security();
arch()->preset()->laravel();
arch()->preset()->strict();

πŸ”„ Composer Scripts - Automate All The Things

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"
        ]
    }
}

πŸš€ Full Workflow Example

# 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!"

πŸ“‹ Quick Reference

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment