Skip to content
| Marketplace
Sign in
Visual Studio Code>Formatters>PHP K&R FormatterNew to Visual Studio Code? Get it now.
PHP K&R Formatter

PHP K&R Formatter

Pirulug

|
2 installs
| (0) | Free
VS Code extension to format PHP code adhering to K&R (Kernighan & Ritchie / 1TBS) style.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

PHP K&R Formatter Logo

PHP K&R Formatter (pirulug.php-formater)

VS Code Marketplace License: MIT

PHP K&R Formatter is a lightweight, fast Visual Studio Code extension that formats PHP code according to the K&R (Kernighan & Ritchie / 1TBS - One True Brace Style) style guide, keeping opening braces { on the same line for classes, methods, functions, and control structures.


Features

  • Standalone Built-in Engine: Formats PHP files out of the box without requiring PHP CLI or external dependencies.
  • K&R / 1TBS Brace Formatting: Moves opening braces { to the same line for:
    • class, interface, trait, enum
    • function, fn()
    • if, else, elseif
    • for, foreach, while, do
    • switch, case, default
    • try, catch, finally
  • Cuddled Braces: Formats } else {, } elseif (...) {, } catch (...) {, } finally {.
  • Vertical Assignment Alignment: Vertically aligns = assignment operators across consecutive assignment statements.
  • Multi-Line Function Signature Collapsing: Collapses multi-line function parameter declarations into single-line signatures.
  • Dynamic Editor Indentation: Automatically detects and respects VS Code's active editor tabSize and insertSpaces (spaces or tabs).
  • php-cs-fixer Integration: Optional runner to use php-cs-fixer if installed in your environment.

Configuration Settings

Customize the extension via your VS Code settings.json:

{
  "[php]": {
    "editor.defaultFormatter": "pirulug.php-formater",
    "editor.formatOnSave": true
  },
  "phpFormater.engine": "builtin",
  "phpFormater.alignAssignments": true,
  "phpFormater.collapseFunctionSignatures": true
}

Configuration Options

Setting Type Default Description
phpFormater.engine string "builtin" Choose "builtin" (standalone TS engine) or "php-cs-fixer"
phpFormater.alignAssignments boolean true Vertically align = assignment operators in consecutive lines
phpFormater.collapseFunctionSignatures boolean true Collapse multi-line function parameter signatures into a single line
phpFormater.indentSize number Auto Number of spaces per indentation level (defaults to active editor tabSize)
phpFormater.insertSpaces boolean Auto Use spaces instead of tabs (defaults to active editor insertSpaces)
phpFormater.phpCsFixerPath string "php-cs-fixer" Executable path for php-cs-fixer

Code Examples

1. K&R Brace Style & Multi-line Function Collapsing

Before

<?php
function complexCalculator(
    int $base,
    int $multiplier,
    bool $useBonus = false
): int
{
    if ($useBonus)
    {
        return $base * $multiplier + 50;
    }
    else
    {
        return $base * $multiplier;
    }
}

After (Shift + Alt + F)

<?php
function complexCalculator(int $base, int $multiplier, bool $useBonus = false): int {
    if ($useBonus) {
        return $base * $multiplier + 50;
    } else {
        return $base * $multiplier;
    }
}

2. Vertical Assignment Alignment (=)

Before

$this->db = $db;
$this->logger = $logger;
$this->config = $config;

After (Shift + Alt + F)

$this->db     = $db;
$this->logger = $logger;
$this->config = $config;

Usage

  1. Open any PHP file (.php, .phtml, etc.).
  2. Right-click inside the editor and select Format Document (Shift + Alt + F / Option + Shift + F).
  3. Or enable editor.formatOnSave: true in VS Code settings to format on file save.

Development & Building with Yeoman (yo code)

If you want to develop, compile, or build this extension using the VS Code extension tooling ecosystem (yo code / vsce):

  1. Install Prerequisites:

    npm install -g yo generator-code @vscode/vsce
    
  2. Compile Source Code:

    npm run compile
    
  3. Watch Mode (Auto-recompile on save):

    npm run watch
    
  4. Package Extension into VSIX:

    vsce package
    

License

MIT License (c) 2026 Pirulug

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2026 Microsoft