Customize formatting

Many companies have coding guidelines when creating source code. Our PHP formatter can be highly customized to follow those guidelines.

Since version 1.24 customizable formatter is available for PREMIUM users. Community version users can still influence the formatter behavior through php.format.codestyle settings.

php.format.codestyle setting serves as a base and each formatting option you specify will override the particular behaviour.

Note: The list of options grows each release, please let us know what option are you missing.

General

Keep control statements on one line

Comming soon...

Setting: php.format.rules.keepControlStatementsOnOneLine

Control statements are kept on one line.

Enabled

if ($x) return;

Disabled

if ($x)
    return;

Braces

Open brace on a new line for functions

Setting: php.format.rules.openBraceOnNewLineForFunctions

Place open brace { on a new line for methods, functions and constructors.

Enabled

function foo()
{
}

Disabled

function foo() {
}

Open brace on a new line for lambda functions

Setting: php.format.rules.openBraceOnNewLineForLambdas

Place open brace { on a new line for lambda functions.

Enabled

$lambda = function ()
{
}

Disabled

$lambda = function () {
}

Open brace on a new line for anonymous classes

Setting: php.format.rules.openBraceOnNewLineForAnonymousClasses

Place open brace { on a new line for anonymous classes.

Enabled

$c = new class extends \Foo implements
    \ArrayAccess
{
};

Disabled

$c = new class extends \Foo implements
    \ArrayAccess {
};

Open brace on a new line for types

Setting: php.format.rules.openBraceOnNewLineForTypes

Place open brace { on a new line for types.

Enabled

class X extends \Foo implements
    \ArrayAccess 
{
}

Disabled

class X extends \Foo implements
    \ArrayAccess {
}

Indentation

Indent braces

Setting: php.format.rules.indentBraces

Indent curly braces ({ and }).

Enabled

function foo()
    {
    echo "Hello";
    }

Disabled

function foo()
{
    echo "Hello";
}

Spacing

Space after cast

Setting: php.format.rules.spaceAfterCast

Insert a space after a cast operator.

Enabled

$x = (int) $obj;

Disabled

$x = (int)$obj;

Space after unary not !

Setting: php.format.rules.spaceAfterUnaryNot

Insert a space after a unary not operator.

Enabled

$x = ! $obj;

Disabled

$x = !$obj;

Space around concatenation .

Setting: php.format.rules.spaceAroundConcatenation

Insert a space around concatenation operator.

Enabled

$x = $str1 . $str2;

Disabled

$x = $str1.$str2;

Space before parentheses in arrow functions

Setting: php.format.rules.spaceBeforeParenthesesInArrowFunctions

Insert a space before arrow function parentheses.

Enabled

$x = fn () => 1;

Disabled

$x = fn() => 1;

Space before colon : in control flow statements

Setting: php.format.rules.spaceBeforeColonInControlStatements

Insert a space before colon in control flow blocks.

Enabled

if (true) :
endif;

Disabled

if (true):
endif;

Space before colon : in return type

Setting: php.format.rules.spaceBeforeColonInReturnType

Insert a space before colon in a return type

Enabled

function foo() : int {
}

Disabled

function foo(): int {
}

Space within call parentheses

Setting: php.format.rules.spaceWithinCallParens

Insert a space within method, function and constructor call parentheses.

Enabled

foo( 1, 2 );

Disabled

foo(1, 2);

Space within declaration parentheses

Setting: php.format.rules.spaceWithinDeclParens

Insert a space within method, function and constructor declaration parentheses.

Enabled

function foo( $x, $y ) { }

Disabled

function foo($x, $y) { }

Space within array initializer parentheses

Setting: php.format.rules.spaceWithinArrayInitilizersParens

Insert a space within array initializer parentheses.

Enabled

$x = array( 1 => "first", 2 => "second" );

Disabled

$x = array(1 => "first", 2 => "second");

Space within if parentheses

Comming soon...

Setting: php.format.rules.spaceWithinIfParens

Insert a space within if statement header parentheses.

Enabled

if ( true ) { }

Disabled

if (true) { }

Space within while parentheses

Comming soon...

Setting: php.format.rules.spaceWithinWhileParens

Insert a space within while statement header parentheses.

Enabled

while ( $a != $b ) { }

Disabled

while ($a != $b) { }

Space within for parentheses

Setting: php.format.rules.spaceWithinForParens

Insert a space within for statement header parentheses.

Enabled

for ( $i = 0; $i < 100; $i++ ) { }

Disabled

for ($i = 0; $i < 100; $i++) { }

Space within foreach parentheses

Setting: php.format.rules.spaceWithinForeachParens

Insert a space within foreach statement header parentheses.

Enabled

foreach ( $expr as $key => $value ) { }

Disabled

foreach ($expr as $key => $value) { }

Space within switch parentheses

Setting: php.format.rules.spaceWithinSwitchParens

Insert a space within switch statement header parentheses.

Enabled

switch ( $x ) { }

Disabled

switch ($x) { }

Space within catch parentheses

Setting: php.format.rules.spaceWithinCatchParens

Insert a space within catch statement header parentheses.

Enabled

try { } catch ( Exception ) { }

Disabled

try { } catch (Exception) { }

Space within brackets []

Setting: php.format.rules.spaceWithinBrackets

Insert a space within brackets.

Enabled

$y = $x[ 1 ];

Disabled

$y = $x[1];

Space within brackets [] around expression

Setting: php.format.rules.spaceWithinBracketsAroundExpression

Insert a space within brackets around expression.

Enabled

$y = $x[ $i + 1 ];

Disabled

$y = $x[$i + 1];

Space within parentheses around expression

Setting: php.format.rules.spaceWithinExpressionParens

Insert a space within parentheses around expression.

Enabled

$x = ( 1 + 2 );

Disabled

$x = (1 + 2);

Wrapping

Array Initilizers

Wrap

Setting: php.format.rules.arrayInitializersWrap

Defines wrapping behavior for array initializers.

  • Off - No wrapping is applied
  • On every item - When this option is selected each item is placed on a new line and properly indented if the array initializer is split across multiple lines.

New line before first element

Setting: php.format.rules.arrayInitializersNewLineBeforeFirstElement

Place a new line before the first array element when wrapping.

Enabled

$x = [
    1,
    2
];

Disabled

$x = [1, 
    2
];

New line after last element

Setting: php.format.rules.arrayInitializersNewLineAfterLastElement

Place a new line after the last element when wrapping.

Enabled

$x = [
    1,
    2
];

Disabled

$x = [
    1, 
    2];

Align key-value pairs

Setting: php.format.rules.arrayInitializersAlignKeyValuePairs

Automatically align => operators.

Enabled

$x = [
    1    => 'foo',
    1234 => 'bar'
];

Disabled

$x = [
    1 => 'foo',
    1234 => 'bar'
];

Call parameters

Wrap

Setting: php.format.rules.callParametersWrap

Defines wrapping behavior for method, function and constructor call parameters.

  • Off - No wrapping is applied
  • On every item - When this option is selected each item is placed on a new line and properly indented if the call arguments are split across multiple lines.

New line after (

Setting: php.format.rules.callParametersNewLineAfterLeftParen

Place a new line after the left opening parenthesis ( when wrapping.

Enabled

foo(
    1,
    2
);

Disabled

foo(1,
    2
);

New line before )

Setting: php.format.rules.callParametersNewLineBeforeRightParen

Place a new line before the right closing parenthesis ) when wrapping.

Enabled

foo(
    1,
    2
);

Disabled

foo(
    1,
    2);

Declaration parameters

Wrap

Setting: php.format.rules.declParametersWrap

Defines wrapping behavior for method or function declaration parameters.

  • Off - No wrapping is applied
  • On every item - When this option is selected each declaration parameter is placed on a new line and properly indented if the arguments are split across multiple lines.

New line after (

Setting: php.format.rules.declParametersNewLineAfterLeftParen

Place a new line after the left opening parenthesis ( of function or method declaration header when wrapping.

Enabled

function foo(
    $x,
    $y
) {
}

Disabled

function foo($x,
    $y
) {
}

New line before )

Setting: php.format.rules.declParametersNewLineBeforeRightParen

Place a new line before the right closing parenthesis ) of a function or method declaration header when wrapping.

Enabled

function foo(
    $x,
    $y
) {
}

Disabled

function foo(
    $x,
    $y) {
}

While statement

Wrap

Setting: php.format.rules.whileStatementNewLineAfterLeftParen

Place a new line after the left opening parenthesis ( in a while statement header if it's split across multiple lines.

Enabled

while(
    $x == 2
) {
}

Disabled

while($x == 2
) {
}

New line before )

Setting: php.format.rules.whileStatementNewLineBeforeRightParen

Place a new line after the right closing parenthesis ) in a while statement header if it's split across multiple lines.

Enabled

while(
    $x == 2
) {
}

Disabled

while(
    $x == 2) {
}

for statement

Wrap

Setting: php.format.rules.forStatementWrap

Defines wrapping behavior for for statement.

  • Off - No wrapping is applied
  • On every item - When this option is selected each item in for statement is placed on a new line and properly indented if they are split across multiple lines.

New line after (

Setting: php.format.rules.forStatementNewLineAfterLeftParen

Place a new line after the left opening parenthesis ( in a for statement header if it's split across multiple lines.

Enabled

for(
    $i = 0; 
    $i < 100;
    $i++
) {
}

Disabled

for($i = 0; 
    $i < 100;
    $i++
) {
}

New line before )

Setting: php.format.rules.forStatementNewLineBeforeRightParen

Place a new line after the right closing parenthesis ) in a for statement header if it's split across multiple lines.

Enabled

for(
    $i = 0; 
    $i < 100;
    $i++
) {
}

Disabled

for($i = 0; 
    $i < 100;
    $i++
) {
}

if statement

New line after (

Setting: php.format.rules.ifStatementNewLineAfterLeftParen

Place a new line after the left opening parenthesis ( in a if or elseif statement header if it's split across multiple lines.

Enabled

if(
    true
) {
}

Disabled

if(true
) {
}

New line before )

Setting: php.format.rules.ifStatementNewLineBeforeRightParen

Place a new line after the right closing parenthesis ) in a if or elseif statement header if it's split across multiple lines.

Enabled

if(
    true
) {
}

Disabled

if(
    true) {
}

else on a new line

Setting: php.format.rules.elseOnNewLine

Place else on a new line.

Enabled

if (true)
{
}
else
{
}

Disabled

if (true)
{
} else
{
}

switch statement

New line after (

Setting: php.format.rules.switchStatementNewLineAfterLeftParen

Place a new line after the left opening parenthesis ( in a switch statement header if it's split across multiple lines.

Enabled

switch(
    $x
) {
}

Disabled

switch($x
) {
}

New line before )

Setting: php.format.rules.switchStatementNewLineBeforeRightParen

Place a new line after the right closing parenthesis ) in a switch statement header if it's split across multiple lines.

Enabled

switch(
    $x
) {
}

Disabled

switch(
    $x) {
}

try statement

catch on a new line

Setting: php.format.rules.catchOnNewLine

Place catch on a new line.

Enabled

try
{
}
catch
{
}
finally
{
}

Disabled

try
{
} catch
{
}
finally
{
}

finally on a new line

Setting: php.format.rules.finallyOnNewLine

Place finally on a new line.

Enabled

try
{
}
catch
{
}
finally
{
}

Disabled

try
{
} 
catch
{
} finally
{
}

Implements list

Wrap

Setting: php.format.rules.implementsListWrap

Defines wrapping behavior for implements list in type declarations.

  • Off - No wrapping is applied
  • On every item - When this option is selected each interface in implements list is placed on a new line and properly indented if they are split across multiple lines.

New line after implements

Setting: php.format.rules.newLineAfterImplements

Place a new line after implements in type declaration if the list of interfaces is split across multiple lines.

Enabled

class X implements
    A,
    B,
    C
{
}

Disabled

class X implements A, 
    B, 
    C
{
}

Group use list

Wrap

Setting: php.format.rules.groupUseWrap

Defines wrapping behavior for group use list in type declarations.

  • Off - No wrapping is applied
  • On every item - When this option is selected each declaration in group use list is placed on a new line and properly indented if they are split across multiple lines.

New line before first group use declaration

Setting: php.format.rules.groupUseNewLineBeforeFirstDeclaration

Place a new line before first group use declaration if the list of declarations is split across multiple lines.

Enabled

use Vendor\pkg\ns\{
    ns1\A,
    ns2\B,
};

Disabled

use Vendor\pkg\ns\{ns1\A,
    ns2\B,
};

Assignment statement

Setting: php.format.rules.alignConsecutiveAssignments

Automatically align consecutive = assignments

Enabled

$a     = 1;
$bbb   = 2;
$ccccc = 3;

Disabled

$a = 1;
$bbb = 2;
$ccccc = 3;