PHPackages                             pmjones/php-styler - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. pmjones/php-styler

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

pmjones/php-styler
==================

Companion to PHP-Parser that rebuilds PHP code from AST.

0.19.1(1mo ago)3974129BSD-3-ClausePHPPHP &gt;=8.4CI passing

Since Aug 14Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/pmjones/php-styler)[ Packagist](https://packagist.org/packages/pmjones/php-styler)[ RSS](/packages/pmjones-php-styler/feed)WikiDiscussions 0.x Synced 1mo ago

READMEChangelog (10)Dependencies (11)Versions (22)Used By (9)

PHP Styler
==========

[](#php-styler)

**WARNING!!!**

PHP-Styler will **completely reformat** your PHP code, discarding any previous formatting entirely.

> McCoy: What if this thing were used where \[formatting\] already exists?
>
> Spock: It would destroy such \[formatting\] in favor of its new matrix.
>
> McCoy: Its new matrix? Do you have any idea what you're saying?
>
> Spock: I was not attempting to evaulate its \[aesthetic\] implications.
>
> \-- *Star Trek II: The Wrath of Khan* (paraphrased)

You can try an online demonstration of PHP-Styler at .

---

Introduction
------------

[](#introduction)

PHP-Styler is a PHP code formatter. It parses PHP source files into tokens, applies configurable formatting rules and styles, and reconstructs the code with consistent horizontal spacing, vertical spacing, and automatic line splitting.

PHP-Styler has no dependencies beyond PHP 8.4 itself (plus [AutoShell](https://github.com/pmjones/AutoShell) for the CLI).

### Design Goals

[](#design-goals)

- **Logic Preservation.** Reformatted PHP code will continue to operate as before.
- **Horizontal and Vertical Spacing.** Automatic indenting and blank-line placement.
- **Line Length Control.** Automatic splitting across multiple lines when a single line is too long.
- **Diff-Friendly.** Default output should aid noise-reduction in diffs.
- **Customizable Formats.** Change the output format using *Styles* (token-level spacing, line breaks, and casing), *Rules* (structural transformations), and *Parses* (token replacements).
- **Comment Preservation.** End-of-line comments stay on their original lines; block comments are preserved in place.

### How It Works

[](#how-it-works)

PHP-Styler uses a multi-stage pipeline to reformat PHP code:

1. The *Parser* tokenizes PHP source code using PHP's built-in `PhpToken`lexer, then maps each token to a context-specific `AToken` subclass and applies *Styles* (spacing, line breaks, casing) from the *Format*.
2. *Token Rules* transform the token stream — reordering modifiers, expanding imports, adding braces to control structures, normalizing trailing commas, etc.
3. The *Assembler* converts the styled token stream into a list of *Line*objects, tracking indentation depth.
4. The *Splitter* checks each line against the configured maximum line length, splitting lines that are too long by applying split points in priority order.
5. *Line Rules* make final adjustments (e.g., removing trailing blank lines).
6. The lines are rendered back to text with the configured end-of-line string and indentation.

### Styling Examples

[](#styling-examples)

See the [Examples](./tests/Examples) directory for a nearly-exhaustive series of styling examples, or try the safe `preview` command on one of your own source files.

Usage
-----

[](#usage)

### Installation

[](#installation)

Use `composer` to add PHP-Styler as a dev requirement:

```
composer require --dev pmjones/php-styler 0.x@dev

```

Copy the default `php-styler.php` config file to your package root:

```
cp ./vendor/pmjones/php-styler/resources/php-styler.php .

```

### Commands

[](#commands)

#### `preview`

[](#preview)

Safely preview how PHP-Styler will reformat a source file (does not modify anything):

```
./vendor/bin/php-styler preview ./src/My/Source/File.php

```

#### `apply`

[](#apply)

Apply formatting to all files identified in the config file, overwriting them in place:

```
./vendor/bin/php-styler apply

```

PHP-Styler will only apply formatting to files with a modification time *later*than the cache file. Use `--force` to format all files regardless:

```
./vendor/bin/php-styler apply --force

```

To apply styling to specific paths instead of those in the config file, pass them as arguments:

```
./vendor/bin/php-styler apply ./src/File.php ./resources/

```

When paths are given explicitly, the cache time is not honored.

#### `check`

[](#check)

Check all configured files to see if they need formatting, without changing anything:

```
./vendor/bin/php-styler check

```

Returns exit code `0` if all files are OK, `1` if any need styling.

#### `diff`

[](#diff)

Show a unified diff of source files vs. their styled versions:

```
./vendor/bin/php-styler diff

```

#### Options

[](#options)

##### Config

[](#config)

All commands accept `-c` or `--config` to specify an alternative config file:

```
./vendor/bin/php-styler preview -c /path/to/other/php-styler.php ./src/File.php

```

##### Parallel Execution

[](#parallel-execution)

The `apply`, `check`, and `diff` commands accept `-w` or `--workers` to process files in parallel using multiple child processes:

```
./vendor/bin/php-styler apply --workers=4
./vendor/bin/php-styler check --workers=auto

```

- `--workers=1` (the default) processes each file in sequence, one by one.
- `--workers=N` splits the file list into `N` chunks and processes them in parallel across `N` workers.
- `--workers=auto` detects the number of CPU cores and uses that as the worker count.

Parallel execution requires no additional PHP extensions — it uses `proc_open`and works on both Linux and Windows. For small file counts (fewer than 8), the sequential path is used regardless of the `--workers` setting.

### Configuration

[](#configuration)

The `php-styler.php` config file returns a `Config` object:

```
