lkrms / pretty-php
The opinionated PHP code formatter
Fund package maintenance!
lkrms
Installs: 1 842
Dependents: 1
Suggesters: 0
Security: 0
Stars: 47
Watchers: 4
Forks: 0
Open Issues: 11
Requires
- php: >=7.4
- composer-runtime-api: ^2.2
- ext-json: *
- ext-mbstring: *
- ext-tokenizer: *
- salient/cli: ^0.99
- salient/collections: ^0.99
- salient/console: ^0.99
- salient/contracts: ^0.99
- salient/core: ^0.99
- salient/phpdoc: ^0.99
- salient/polyfills: ^0.99
- salient/utils: ^0.99
- sebastian/diff: ^4 || ^5
Requires (Dev)
- dev-main
- v0.4.87
- v0.4.86
- v0.4.85
- v0.4.84
- v0.4.83
- v0.4.82
- v0.4.81
- v0.4.80
- v0.4.79
- v0.4.78
- v0.4.77
- v0.4.76
- v0.4.75
- v0.4.74
- v0.4.73
- v0.4.72
- v0.4.71
- v0.4.70
- v0.4.69
- v0.4.68
- v0.4.67
- v0.4.66
- v0.4.65
- v0.4.64
- v0.4.63
- v0.4.62
- v0.4.61
- v0.4.60
- v0.4.59
- v0.4.58
- v0.4.57
- v0.4.56
- v0.4.55
- v0.4.54
- v0.4.53
- v0.4.52
- v0.4.51
- v0.4.50
- v0.4.49
- v0.4.48
- v0.4.47
- v0.4.46
- v0.4.45
- v0.4.44
- v0.4.43
- v0.4.42
- v0.4.41
- v0.4.40
- v0.4.39
- v0.4.38
- v0.4.37
- v0.4.36
- v0.4.35
- v0.4.34
- v0.4.33
- v0.4.32
- v0.4.31
- v0.4.30
- v0.4.29
- v0.4.28
- v0.4.27
- v0.4.26
- v0.4.25
- v0.4.24
- v0.4.23
- v0.4.22
- v0.4.21
- v0.4.20
- v0.4.19
- v0.4.18
- v0.4.17
- v0.4.16
- v0.4.15
- v0.4.14
- v0.4.13
- v0.4.12
- v0.4.11
- v0.4.10
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.24
- v0.3.23
- v0.3.22
- v0.3.21
- v0.3.20
- v0.3.19
- v0.3.18
- v0.3.17
- v0.3.16
- v0.3.15
- v0.3.14
- v0.3.13
- v0.3.12
- v0.3.11
- v0.3.10
- v0.3.9
- v0.3.8
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-improve-declaration-spacing
- dev-improve-anonymous-fn
- dev-cleanup
This package is auto-updated.
Last update: 2025-01-18 06:41:17 UTC
README
pretty-php
is a fast, deterministic, minimally configurable code formatter for
PHP, written in PHP.
It looks after the whitespace in your code so you have more time and energy for the content.
Inspired by Black, pretty-php
aims to produce the smallest diffs possible
and for code to look the same regardless of the project you're working on,
eliminating visual dissonance and improving the effectiveness of code review.
You can run pretty-php
from the command line, use it in your editor, add
it to your CI workflows, pair it with your preferred linter, and more.
It has sensible defaults and runs without configuration.
If you have questions or feedback, I'd love to hear from you.
Features
- Formats code written for PHP 8.4 and below (when running on a compatible version of PHP), including property hooks introduced in PHP 8.4
- Code is formatted for readability, consistency, and small diffs
- Previous formatting is ignored, and nothing other than whitespace is changed (see Pragmatism for exceptions)
- Entire files are formatted in place
- Formatting options are deliberately limited (
pretty-php
is opinionated so you don't have to be) - Configuration via a simple JSON file is supported but not required
- Formatted and original code are compared for equivalence
- Compliant with PSR-12 and PER (see PSR-12 compliance for details)
- Supports Symfony, Drupal, Laravel and WordPress code styles via presets
Installation
Requirements
- Linux, macOS or Windows
- PHP 8.4, 8.3, 8.2, 8.1, 8.0 or 7.4 with the standard
tokenizer
,mbstring
andjson
extensions enabled
PHP archive (PHAR)
pretty-php
is distributed as a PHP archive you can download and run:
wget -O pretty-php.phar https://github.com/lkrms/pretty-php/releases/latest/download/pretty-php.phar
php pretty-php.phar --version
The PHAR can be made executable:
chmod +x pretty-php.phar
./pretty-php.phar --version
Official releases distributed via GitHub are signed and can be verified as follows:
wget -O pretty-php.phar https://github.com/lkrms/pretty-php/releases/latest/download/pretty-php.phar wget -O pretty-php.phar.asc https://github.com/lkrms/pretty-php/releases/latest/download/pretty-php.phar.asc gpg --recv-keys 0xE8CC5BC780B581F2 gpg --verify pretty-php.phar.asc pretty-php.phar
Installation with PHIVE, which verifies PHAR releases automatically, is also supported:
phive install lkrms/pretty-php
./tools/pretty-php --version
Adding lkrms/pretty-php
to your project as a Composer dependency is not
recommended. A separate API package will be provided in the future.
Arch Linux
Arch Linux users can install pretty-php
from the AUR. For example, if your
preferred AUR helper is yay
:
yay -S pretty-php
macOS
Homebrew users on macOS can install pretty-php
using the following command,
which automatically taps lkrms/misc
if necessary:
brew install lkrms/misc/pretty-php
Usage
Once installed, getting started with pretty-php
is as simple as giving it
something to format. For example, to format bootstrap.php
and any PHP files in
the src
directory:
pretty-php bootstrap.php src
To see what would change without actually replacing any files, add the --diff
option:
pretty-php --diff bootstrap.php src
For detailed usage information, see usage or run:
pretty-php --help
Editor integrations
- pretty-php for Visual Studio Code
Official VS Code extension
Visual Studio Marketplace | Open VSX Registry | Repository
Pragmatism
pretty-php
generally abides by its own rules ("previous formatting is ignored,
and nothing other than whitespace is changed"), but exceptions are occasionally
made and documented here.
-
Some newlines are preserved
Line breaks adjacent to most operators, delimiters and brackets are copied from the input to the output (see Newlines for details).Use
-N/--ignore-newlines
,-O/--operators-first
or-L/--operators-last
to disable or modify this behaviour. -
Strings and numbers are normalised
Single-quoted strings are preferred unless the alternative is shorter or backslash escapes are required.Use
-S/--no-simplify-strings
and-n/--no-simplify-numbers
to disable or modify this behaviour. -
Imports are grouped and sorted by name, depth-first
See Import sorting for details.Use
-M/--no-sort-imports
or-m/--sort-imports-by
to disable or modify this behaviour. -
Comments are moved if necessary for correct placement of adjacent tokens
Use--disable=move-comments
to disable this behaviour. -
Comments beside code are not moved to the next line
-
Comments are trimmed and aligned
-
Empty DocBlocks are removed
License
This project is licensed under the MIT License.