b2KIT

PHP Formatter (PSR-12)

Format PHP code following PSR-12 coding style with configurable indentation and brace placement.

Tested tool guide Tested browser tools Checked August 16, 2026

What PHP Formatter (PSR-12) does, with a checked example

Paste PHP source into this tool and it rewrites only the whitespace to follow PSR-12, the PHP-FIG extended coding style guide: four-space indentation, class and method braces on their own line, control-structure braces on the same line as the keyword, and spaces around operators. Strings and comments pass through unchanged. The usual surprise is how little the tool does: it cannot repair syntax errors, add a missing visibility keyword, or convert double quotes to single quotes, and switching to tabs or same-line braces quietly steps outside PSR-12.

Worked example

A concrete input and expected output from the current implementation.

Input

<?php
namespace App;
class Greeter{
public function greet(string $who):string{
return "Hello, ".$who;
}
public function farewell():string{
return "Goodbye!";
}
}

Expected output

<?php
namespace App;

class Greeter
{
    public function greet(string $who): string
    {
        return "Hello, " . $who;
    }

    public function farewell(): string
    {
        return "Goodbye!";
    }
}

The class and method opening braces move to their own line, the body indents by four spaces, spaces appear around the concatenation operator and after the return-type colon, and blank lines are inserted where PSR-12 requires them: after the namespace declaration and between methods. The strings, the parameter list, and the identifiers are preserved exactly.

How the result is produced

1

Whitespace-only rewrite

The output differs from the input only in indentation, spacing, and blank lines. String literals, comments, and identifiers pass through unchanged, because altering them could change what the program does. If the input does not parse - unbalanced braces, a missing semicolon - the tool cannot reliably tell where statements start and end, so confirm it reports the error instead of emitting shifted output.

2

PSR-12 defaults, configurable exceptions

The defaults implement PSR-12: four-space indentation; class, interface, and method opening braces on the next line; control-structure braces on the same line as the keyword; one space after keywords and around binary operators; one blank line between methods; and a blank line after namespace and use declarations. The indentation and brace-placement options exist for non-PSR-12 tastes - choosing them means the result will not match the standard.

Good uses

  • Restore readability to minified, single-line, or copy-pasted PHP from a forum post, theme file, or email before reading or editing it.
  • Normalize a legacy file - say, one indented with two spaces or mixed tabs - to the project's four-space PSR-12 baseline before committing it.
  • Learn the standard itself: paste deliberately messy snippets and watch which spacing changes PSR-12 requires and which parts of the code the formatter refuses to touch.

Limits and checks

  • Syntax errors are not repaired. If the snippet does not parse, brace and indent decisions become guesswork; fix unbalanced braces or missing semicolons first, and verify the tool reports a parse problem rather than returning silently shifted code.
  • Long lines stay long. PSR-12 sets a 120-character line-length limit, but it does not say where to break a long call or argument list, so the formatter leaves such lines alone. A checker such as phpcs will still flag them; the wrap is yours to choose.
  • Non-default options forfeit conformance. Tabs, two-space indentation, or same-line method braces give clean output that a PSR-12 checker will reject. Keep the defaults if the goal is a passing phpcs PSR12 run.

Common questions

Why is my 150-character line left untouched?

PSR-12 limits lines to 120 characters, but it does not prescribe how to wrap a long argument list, and breaking a line is a judgment call the formatter will not make for you. Overlong lines are therefore left as-is. Wrap them by hand, or run phpcs with the PSR12 standard to get a list of every line that exceeds the limit.

Will the formatter add a missing visibility keyword?

No. PSR-12 requires every method and property to declare public, protected, or private, and a missing visibility is a common violation - but adding a keyword is a code change, not spacing, and the tool cannot know which of the three you intended. Add it yourself; a PSR-12 check will keep flagging the file until you do.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools