Skip to content

InteractionDesignFoundation/coding-standard

Repository files navigation

IxDF PHP Coding Standard

Tests PHP Psalm PHP Psalm Level PHP Psalm Type Coverage Total Downloads Latest Stable Version

IxDF PHP Coding Standard

An opinionated coding standard for PHP and Laravel projects, built on PER-CS3.0. Focuses on:

  • High signal-to-noise ratio (concise but informative PHPDoc, e.g. array shapes)
  • Strict, explicit code: strict_types, strict comparisons, final by default, no magic
  • Harmony with static analysis tools (PHPStan, Psalm, Rector, etc.)
  • Auto-fixing over reporting (violations are not only reported but also fixed automatically, when possible)
  • PHP and Laravel best practices (enforced, not just suggested)

Two tools, two roles, meant to run together:

  • PHP-CS-Fixer (primary): fast auto-formatting and modernization rules.
  • PHP_CodeSniffer (supplementary): structural and semantic checks PHP-CS-Fixer cannot express (complexity, Laravel conventions, naming).

Tip

This repository is a part of the IxDF toolchain for PHP: Rector ➝ Coding Standard Fixers ➝ PHPStan + Psalm.

1. Installation

composer require --dev interaction-design-foundation/coding-standard

2. Configuration

PHP-CS-Fixer

Primary formatter.

Create .php-cs-fixer.php in your project root:

<?php declare(strict_types=1);

use IxDFCodingStandard\PhpCsFixer\Config;

// @see https://mlocati.github.io/php-cs-fixer-configurator/ for ruleOverrides options.
return Config::create(__DIR__, ruleOverrides: []);
Customization (optional) Config::create() ships a sensible default Finder and enables parallel runs, risky rules, and caching. You can customize it by using your Finder instance and by overriding individual rules:
use PhpCsFixer\Finder;

$finder = Finder::create()->in(__DIR__)->name('*.php');

// Override individual rules, your own Finder and use your cache path
return Config::create(__DIR__, finder: $finder, ruleOverrides: [
    'final_class' => true,
])->setCacheFile('./.cache/.php-cs-fixer.cache');

Need only the rule array (e.g. to compose your own config)?

$rules = \IxDFCodingStandard\PhpCsFixer\Rules::get();

Run it:

vendor/bin/php-cs-fixer fix

PHP_CodeSniffer

Supplementary (optional) formatter.

Create phpcs.xml in your project root:

<?xml version="1.0"?>
<ruleset name="My Coding Standard">
    <file>app</file>
    <file>config</file>
    <file>database</file>
    <file>routes</file>
    <file>tests</file>

    <rule ref="IxDFCodingStandard"/>
</ruleset>

On top of the Generic, PSR and Slevomat rulesets, IxDFCodingStandard ships its own sniffs. Some run by default; some are opt-in. See docs/README.md for the full list and configuration.

Run it:

vendor/bin/phpcbf   # fix
vendor/bin/phpcs    # check (dry-run)

Composer scripts (recommended)

Wire both tools into composer.json so the whole team runs them the same way:

composer cs

cs fixes files in place. Edit composer.json:

{
    "scripts": {
        "cs": ["@php-cs-fixer", "@phpcbf"],
        "phpcbf": "phpcbf -p",
        "phpcs": "phpcs -p -s --report-full --report-summary",
        "php-cs-fixer": "php-cs-fixer fix --no-interaction --ansi --quiet"
    }
}

3. Continuous integration

Run the fixer in CI and commit the result back, so the branch is always formatted without blocking the build. This repository dogfoods that pattern; copy its workflow as a starting point: .github/workflows/coding-standard.yml.

About

🪷 PHPCS coding-standard for Laravel applications and packages

Topics

Resources

Stars

7 stars

Watchers

7 watching

Forks

Contributors