PHPackages                             multicoin/laravel-code-standards - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. multicoin/laravel-code-standards

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

multicoin/laravel-code-standards
================================

Strict OOP code standards for Laravel with automated linting and review tools

03PHP

Since Nov 18Pushed 5mo agoCompare

[ Source](https://github.com/minulislam/laravel-code-standards)[ Packagist](https://packagist.org/packages/multicoin/laravel-code-standards)[ RSS](/packages/multicoin-laravel-code-standards/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Code Standards
======================

[](#laravel-code-standards)

[![Latest Version](https://camo.githubusercontent.com/1143a5cbded3da2aedc92cf5d466cf2efbca7846f997d93f45c7c6051569f06e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d756c7469636f696e2f6c61726176656c2d636f64652d7374616e64617264732e737667)](https://packagist.org/packages/multicoin/laravel-code-standards)[![License](https://camo.githubusercontent.com/ddd781a9ba7404c5a31deb09352be6964e08621aea3b950af23e9daad5f59088/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d756c7469636f696e2f6c61726176656c2d636f64652d7374616e64617264732e737667)](https://packagist.org/packages/multicoin/laravel-code-standards)

**Strict OOP code standards for Laravel with automated linting and review tools.**

This package enforces strict object-oriented programming standards while being Laravel-aware, allowing idiomatic patterns like facades. It bundles PHPStan, Pint, and PHPCS with pre-configured rules optimized for Laravel projects.

Features
--------

[](#features)

- ✅ **Strict OOP Standards** - SOLID principles, dependency injection, type safety
- ✅ **Laravel-Aware** - Understands and allows Laravel facades and patterns
- ✅ **Automated Tools** - PHPStan (Level 8), Laravel Pint, PHP\_CodeSniffer
- ✅ **Artisan Commands** - Easy-to-use CLI commands
- ✅ **CI/CD Ready** - GitHub Actions workflow included
- ✅ **Customizable** - Publish and modify all configuration files

What Gets Checked
-----------------

[](#what-gets-checked)

### 🚨 Critical Issues (Auto-Fail)

[](#-critical-issues-auto-fail)

- Missing `declare(strict_types=1);`
- Static methods in business logic (facades are OK)
- Direct `new` instantiation in constructors
- Missing type hints on parameters/returns
- Missing visibility modifiers
- Potential SQL injection vulnerabilities
- Business logic in controllers

### ✅ Laravel Patterns Allowed

[](#-laravel-patterns-allowed)

- **Facades explicitly allowed**: `Cache::`, `DB::`, `Log::`, etc.
- Form Request validation
- API Resources for responses
- Jobs for async operations
- Eloquent best practices

Installation
------------

[](#installation)

### 1. Install via Composer

[](#1-install-via-composer)

```
composer require multicoin/laravel-code-standards --dev
```

### 2. Run Installation Command

[](#2-run-installation-command)

```
php artisan standards:install
```

This command will:

- Install PHPStan, Pint, PHPCS, and related packages
- Publish configuration files (`phpstan.neon`, `pint.json`, `phpcs.xml`)
- Update `composer.json` with helpful scripts
- Display available commands

### 3. (Optional) Publish Individual Components

[](#3-optional-publish-individual-components)

If you want to publish specific configurations only:

```
# Publish config file
php artisan vendor:publish --tag=code-standards-config

# Publish PHPStan config
php artisan vendor:publish --tag=code-standards-phpstan

# Publish Pint config
php artisan vendor:publish --tag=code-standards-pint

# Publish PHPCS config
php artisan vendor:publish --tag=code-standards-phpcs

# Publish GitHub Actions workflow
php artisan vendor:publish --tag=code-standards-github

# Publish everything at once
php artisan vendor:publish --tag=code-standards
```

Usage
-----

[](#usage)

### Artisan Commands

[](#artisan-commands)

```
# Run full code review (Pint + PHPStan + PHPCS)
php artisan review

# Auto-fix issues where possible
php artisan review --fix

# Run only Pint (code style)
php artisan review:lint

# Auto-fix code style
php artisan review:lint --fix

# Check only uncommitted files
php artisan review:lint --dirty

# Run only PHPStan (static analysis)
php artisan review:analyse

# Analyze specific path
php artisan review:analyse app/Services

# Use different PHPStan level
php artisan review:analyse --level=6
```

### Composer Scripts

[](#composer-scripts)

After installation, these scripts are available:

```
# Check code style
composer lint

# Auto-fix code style
composer lint:fix

# Run static analysis
composer analyse

# Run PHPCS
composer phpcs

# Run all checks
composer review
```

### Pre-commit Hook (Optional)

[](#pre-commit-hook-optional)

Create `.git/hooks/pre-commit`:

```
#!/bin/bash

echo "Running code review..."

php artisan review

if [ $? -ne 0 ]; then
    echo "❌ Code review failed. Commit aborted."
    echo "Run 'php artisan review --fix' to auto-fix issues."
    exit 1
fi

echo "✅ Code review passed!"
exit 0
```

Make it executable:

```
chmod +x .git/hooks/pre-commit
```

Configuration
-------------

[](#configuration)

### Main Configuration File

[](#main-configuration-file)

After installation, edit `config/code-standards.php`:

```
return [
    'phpstan' => [
        'enabled' => true,
        'level' => 8,
        'paths' => ['app', 'config', 'database', 'routes'],
        'memory_limit' => '2G',
    ],

    'pint' => [
        'enabled' => true,
        'preset' => 'laravel',
    ],

    'rules' => [
        'strict_types' => true,
        'type_hints' => true,
        'visibility_modifiers' => true,
        'no_static_business_logic' => true,
        'dependency_injection' => true,
        'laravel_facades_allowed' => true,
    ],

    'allowed_facades' => [
        'Cache', 'DB', 'Log', 'Auth', // ... etc
    ],

    'exclude' => [
        'vendor',
        'storage',
        'bootstrap/cache',
    ],
];
```

### Tool-Specific Configuration

[](#tool-specific-configuration)

Each tool has its own configuration file that you can customize:

- **phpstan.neon** - PHPStan static analysis rules
- **pint.json** - Laravel Pint code style rules
- **phpcs.xml** - PHP\_CodeSniffer standards

Standards Enforced
------------------

[](#standards-enforced)

### OOP Standards

[](#oop-standards)

#### Dependency Injection

[](#dependency-injection)

```
// ❌ Bad
class OrderService {
    public function __construct() {
        $this->gateway = new PaymentGateway(); // Hardcoded
    }
}

// ✅ Good
class OrderService {
    public function __construct(
        private readonly PaymentGateway $gateway
    ) {}
}
```

#### Type Safety

[](#type-safety)

```
// ❌ Bad
function process($data) {
    return $data->value;
}

// ✅ Good
function process(UserData $data): int {
    return $data->value;
}
```

#### Strict Types

[](#strict-types)

```
// ✅ Required at top of every file
