PHP FormatterPHP Formatter formats your PHP files using a native Rust binary — no Node.js dependencies, no PHP runtime required. Before / After
Features
InstallationFrom the VS Code MarketplaceSearch for PHP Formatter in the Extensions panel ( Or from the command line:
From a VSIX fileDownload the
Or: Extensions panel →
UsageFormat document
Format on saveEnable in VS Code settings:
Or per project in
Skip a region
Both ConfigurationVS Code settingsAll settings live under the
|
| OS | Architecture | Status |
|---|---|---|
| Windows 10 / 11 | x86-64 | ✅ Bundled |
| macOS 12+ | x86-64 | 🔧 Build from source |
| macOS 12+ | Apple Silicon (arm64) | 🔧 Build from source |
| Linux (glibc 2.17+) | x86-64 | 🔧 Build from source |
The published .vsix bundles the Windows x86-64 binary. For other platforms, build the binary from source (see Contributing) and point phpFormatter.binaryPath to it.
Contributing
Requirements
Clone and build
git clone https://github.com/eder-rimarachinr/vscode-extension-php-formater-rust.git
cd php-formatter
Build the Rust binary:
cd php-formatter # Rust crate directory
cargo build --release
# Output: target/release/php_formatter (or .exe on Windows)
Copy the binary into the extension:
# Windows
copy target\release\php_formatter.exe ..\vscode-extension\bin\
# macOS / Linux
cp target/release/php_formatter ../vscode-extension/bin/
Build the VS Code extension:
cd ../vscode-extension
npm install
npm run compile
npx vsce package # produces php-formatter-x.y.z.vsix
Project structure
php-formatter/ Rust binary (cargo project)
├── src/
│ ├── main.rs Entry point — legacy + --json dispatch
│ ├── formatter.rs Line struct, format pipeline
│ ├── config.rs Config struct, .phpfmt.toml discovery
│ ├── protocol.rs BinaryRequest / BinaryResponse types
│ └── rules/
│ ├── align.rs = alignment
│ ├── align_arrows.rs => alignment
│ ├── align_comments.rs Inline comment alignment
│ ├── frozen.rs @fmt-off / @fmt-on regions
│ ├── indent.rs Indentation normalisation
│ └── spacing.rs Operator and comma spacing
├── Cargo.toml
└── .cargo/config.toml Linker config (uses rust-lld)
vscode-extension/ VS Code extension (TypeScript)
├── src/
│ └── extension.ts Activation, commands, providers
├── bin/
│ └── php_formatter.exe Bundled release binary (Windows)
├── package.json
└── tsconfig.json
Running the binary directly
The binary accepts PHP via stdin (legacy mode) or a JSON request on stdin (--json mode):
# Legacy: pipe source, get formatted source on stdout
echo '<?php $a = 1; $foo = 2;' | php_formatter
# JSON mode: send a request object, receive a response object
echo '{"command":"format","source":"<?php $a=1;\n$foo=2;\n"}' | php_formatter --json
# Check mode: returns diagnostics without modifying source
echo '{"command":"check","source":"<?php $a=1;\n"}' | php_formatter --json
Changelog
v0.4.0
Rust engine
- HTML structural indentation — mixed PHP/HTML view files (CodeIgniter, Laravel plain views, Symfony templates) are re-indented based on HTML tag depth; auto-detected, zero config; can be disabled with
format_html = false - UTF-8 fix — accented characters (á, é, ñ…) and other multi-byte sequences no longer corrupted during formatting
- PHP 8.3 typed class constants — alignment support for
public const string FOO = 'bar' - PHP 8.4 property hooks — all forms: inline
{ get => expr; }, multi-line, full-bodyset(Type $v) { … }, abstract{ get; }, promoted in constructor - PHP 8.4 asymmetric visibility —
public private(set)pass-through (no corruption) - PHP 8.5 pipe operator —
|>pass-through - Typed parameter / property alignment —
string $name = 'default'andpublic string $prop = ''align as a separate group from plain$varassignments - Presets —
psr12,per-cs,laravelactivate curated rule groups with a single line - Individual config toggles — all rules now settable per-project in
[style]:normalize_keywords,lowercase_casts,no_extra_blank_lines,strip_import_slash,format_html --init-config— generates a fully-commented.phpfmt.tomltemplate--format-dir— parallel multi-file formatting via rayon; incremental SHA-256 cache skips unchanged files;--no-cache,--dry-run,--jobs Nflags--check— CI mode: exit 0 (clean) / 1 (needs formatting) / 2 (error)--print-config— shows the active config as JSON for any file- Diagnostic classification — Problems panel shows rule codes:
FMT-INDENT,FMT-SPACING,FMT-ALIGN,FMT-KW,FMT-CAST,FMT-IMPORT - Hexagonal architecture: domain / adapters / infrastructure separation
VS Code extension
PHP Formatter: Create .phpfmt.tomlcommand — generates commented config template, offers to open the file immediately- Workspace/folder formatting now uses
--format-dir(single binary call, rayon-parallel) instead of per-file spawning - Dynamic workspace roots: new folders added during a session are picked up automatically
build-localnpm script to compile and install the Rust binary in one step
v0.3.3
- Fixed binary integrity check (checksums.txt had placeholder hashes)
v0.3.2
- Updated README with correct repository links
v0.3.1
- Added extension icon
v0.3.0
- JSON protocol between extension and binary (
--jsonmode) =>alignment for arrays andmatchexpressions- Inline
//comment alignment in consecutive blocks @fmt-off/@fmt-onregion exclusion (PhpStorm@formatter:offalso supported)- Format on save (
phpFormatter.formatOnSave) - Preview diff before applying (
PHP Formatter: Preview Format) - Format selection
- Per-project
.phpfmt.tomlconfiguration with full validation - Problems panel diagnostics with Quick Fix (
phpFormatter.diagnostics) - Bulk format: workspace and folder (
PHP Formatter: Format All PHP Files in Workspace) - Status bar timing (
⌚ PHP fmt: Nms)
v0.1.0 — Initial release
=alignment for consecutive assignment blocks- Indentation normalisation (tabs → spaces)
- Spacing normalisation around operators and commas
- Bundled Windows x86-64 binary
- VS Code formatter provider (
Shift+Alt+F)
Upcoming
- macOS and Linux bundled binaries
License
MIT License.