php coding standard

Combines various code quality tools into a single command with shared output for easier CI integration

Latest Version on Packagist
Total Downloads

php-coding-standard (phpcstd)

diagram of the project workflow

phpcstd combines various code quality tools (e.g. linting and static analysis)
into one, easy to use package which can be shared across teams and code bases.

There are two parts to this:

  1. phpcstd executes all the enabled tools and returns a single per-file error output
  2. In your projects, you depend on a single repository (e.g. acme/coding-standard)
    which depends on phpcstd and includes the various base configurations
    (e.g. phpmd.xml, ecs.yaml, …). Your own projects then depend on your own coding standard.

phpcstd itself does not come with any tools preinstalled.
You can take a look at my own coding standards as an example.

Tools supported

Tool Lint Fix Source list Description
composer-normalize Validates and rearranges composer.json files
EasyCodingStandard Combination of PHP_CodeSniffer and PHP-CS-Fixer
PHP Mess Detector Code complexity checker
php-parallel-lint Checks for PHP (syntax) errors (using php -l)
PHP_CodeSniffer Style linter + fixer
phpstan Static analyzer, source list is optional, but not recommended
psalm Static analyzer
phan Static analyzer

⭐ = recommended

Getting started

composer require-dev spaceemotion/php-coding-standard

This will install the phpcstd binary to your vendor folder.

Configuration via .phpcstd(.dist).ini

To minimize dependencies, phpcstd used ini-files for its configuration. If no .phpcstd.ini file can be found in your project folder, a .phpcstd.dist.ini file will be used as fallback (if possible).

Command options

$ phpcstd [options] <files or folders>

--disable
  Disables the list of tools during the run (comma-separated list)

--ansi
  Forces the output to be colorized

--ci
  Changes the output format to checkstyle.xml for better CI integration

--fix
  Try to fix any linting errors (disables other tools)

--continue
  Just run the next check if the previous one failed

--hide-source
  Hides the "source" lines from console output

--no-fail
  Only returns with exit code 0, regardless of any errors/warnings

--lint-staged
  Uses "git diff" to determine staged files to lint

--help
  Displays this help message

Instead of defining the files/folders directly (in either the config or as arguments), you can also pipe a list into it:

$ ls -A1 | vendor/bin/phpcstd

Git Hooks

To not have to wait for CI pipelines to finish, you can use git hooks to run over the changed files before committing.

vendor/bin/phpcstd --lint-staged

CI-Support

Github Actions

The --ci flag returns a format that can be used by GithubActions to annotate commits and PRs
(see their documentation on how this works).

example file change with an error

Development

Using Docker

  1. Spin up the container using GITHUB_PERSONAL_ACCESS_TOKEN=<token> docker-compose up -d --build
  2. Run all commands using docker-compose exec php <command here>

Using XDebug

This project uses composer/xdebug-handler to improve performance
by disabling xdebug upon startup. To enable XDebug during development you need to set the following env variable:
PHPCSTD_ALLOW_XDEBUG=1 (as written in their README).