Customize formatting

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

Fully customizable formatter is available for PREMIUM users. Community version users can setup the formatter 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.

Indentation

Setting: php.format.rules.indentBraces

IndentBraces: Gets value indicating whether to indent braces

true false
function foo()
    {
    echo "Hello";
    }
function foo()
{
    echo "Hello";
}

Spacing

Setting: php.format.rules.spaceAfterCast

Space after cast: Insert a space after a cast operator.

true false
$x = (int) $obj;
$x = (int)$obj;

Setting: php.format.rules.spaceAfterUnaryNot

Space after unary not !: Insert a space after a unary not operator.

true false
$x = ! $obj;
$x = !$obj;

Setting: php.format.rules.spaceAroundConcatenation

Space around concatenation .: Insert a space around concatenation operator.

true false
$x = $str1 . $str2;
$x = $str1.$str2;

Setting: php.format.rules.spaceBeforeColonInControlStatements

Space before colon : in control flow statements: Insert a space before colon in control flow blocks.

true false
if (true) :
endif;
if (true):
endif;

Setting: php.format.rules.spaceBeforeColonInReturnType

Space before colon : in return type: Insert a space before colon in a return type

true false
function foo() : int {
}
function foo(): int {
}

Setting: php.format.rules.spaceBeforeParenthesesInArrowFunctions

Space before parentheses in arrow functions: Insert a space before arrow function parentheses.

true false
$x = fn () => 1;
$x = fn() => 1;

Setting: php.format.rules.spaceBeforeParenthesesInCalls

Space before parentheses in function calls: Insert a space before parentheses in method, function and constructor call parentheses.

true false
foo ();
foo();

Setting: php.format.rules.spaceBeforeParenthesesInControlStatements

Space before parentheses in control statements: Insert a space before parentheses in control statements.

true false
if (true)
{
}
if(true)
{
}

Setting: php.format.rules.spaceBeforeParenthesesInDeclarations

Space before parentheses in declarations: Insert a space before parentheses in method, function and constructor declaration parentheses.

true false
function foo ()
{
}
function foo()
{
}

Setting: php.format.rules.spaceWithinArrayInitilizersParens

Space within array initializer parentheses: Insert a space within array initializer parentheses.

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

Setting: php.format.rules.spaceWithinBrackets

Space within brackets []: Insert a space within brackets.

true false
$y = $x[ 1 ];
$y = $x[1];

Setting: php.format.rules.spaceWithinBracketsAroundExpression

Space within brackets [] around expression: Insert a space within brackets around expression.

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

Setting: php.format.rules.spaceWithinCallParens

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

true false
foo( 1, 2 );
foo(1, 2);

Setting: php.format.rules.spaceWithinCatchParens

Space within catch parentheses: Insert a space within catch statement header parentheses.

true false
try { } catch ( Exception ) { }
try { } catch (Exception) { }

Setting: php.format.rules.spaceWithinDeclParens

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

true false
function foo( $x, $y ) { }
function foo($x, $y) { }

Setting: php.format.rules.spaceWithinExpressionParens

Space within parentheses around expression: Insert a space within parentheses around expression.

true false
$x = ( 1 + 2 );
$x = (1 + 2);

Setting: php.format.rules.spaceWithinForParens

Space within for parentheses: Insert a space within for statement header parentheses.

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

Setting: php.format.rules.spaceWithinForeachParens

Space within foreach parentheses: Insert a space within foreach statement header parentheses.

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

Setting: php.format.rules.spaceWithinIfParens

Space within if parentheses: Insert a space within if statement header parentheses.

true false
if ( true ) { }
if (true) { }

Setting: php.format.rules.spaceWithinSwitchParens

Space within switch parentheses: Insert a space within switch statement header parentheses.

true false
switch ( $x ) { }
switch ($x) { }

Setting: php.format.rules.spaceWithinWhileParens

Space within while parentheses: Insert a space within while statement header parentheses.

true false
while ( $a != $b ) { }
while ($a != $b) { }

NewLines

Setting: php.format.rules.catchOnNewLine

catch on a new line: Place catch on a new line.

true false
try
{
}
catch
{
}
finally
{
}
try
{
} catch
{
}
finally
{
}

Setting: php.format.rules.elseOnNewLine

else on a new line: Place else on a new line.

true false
if (true)
{
}
else
{
}
if (true)
{
} else
{
}

Setting: php.format.rules.endWithNewLine

Insert final newline: Inserts final newline at the end of a file.

Setting: php.format.rules.finallyOnNewLine

finally on a new line: Place finally on a new line.

true false
try
{
}
catch
{
}
finally
{
}
try
{
} 
catch
{
} finally
{
}

Setting: php.format.rules.openBraceOnNewLineForAnonymousClasses

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

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

Setting: php.format.rules.openBraceOnNewLineForBlocks

Open brace on a new line for blocks: Place open brace { on a new line for all types of code blocks, except for those controlled by other formatting rules.

true false
if (true)
{
}
if (true) {
}

Setting: php.format.rules.openBraceOnNewLineForFunctions

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

true false
function foo()
{
}
function foo() {
}

Setting: php.format.rules.openBraceOnNewLineForLambdas

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

true false
$lambda = function ()
{
}
$lambda = function () {
}

Setting: php.format.rules.openBraceOnNewLineForNamespaces

Open brace on a new line for namespaces: Place open brace { on a new line for namespace declarations.

true false
namespace A
{
}
namespace A {
}

Setting: php.format.rules.openBraceOnNewLineForTypes

Open brace on a new line for types: Place open brace { on a new line for types.

true false
class X extends \Foo implements
    \ArrayAccess 
{
}
class X extends \Foo implements
    \ArrayAccess {
}

Wrapping

Setting: php.format.rules.alignConsecutiveAssignments

Align consecutive = assignments.: Automatically align consecutive = assignments.

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

Setting: php.format.rules.alignConstants

Align constants: Automatically align constant declarations

true false
class X {
    const a   = 1;
    const bb  = 2;
    const ccc = 3;
}
class X {
    const a = 1;
    const bb = 2;
    const ccc = 3;
}

Setting: php.format.rules.alignEnumCases

Align enum cases: Automatically align assigned values in backed enumerations

true false
enum X {
    case a   = 1;
    case bb  = 2;
    case ccc = 3;
}
enum X {
    case a = 1;
    case bb = 2;
    case ccc = 3;
}

Setting: php.format.rules.alignMatchArmBodies

Align match arm bodies: Automatically align the bodies (results) within match expression arms

true false
match ($day) {
    'Monday'    => 'Work',
    'Tuesday'   => 'Tacos',
    'Wednesday' => 'Waffles'
};
match ($day) {
    'Monday' => 'Work',
    'Tuesday' => 'Tacos',
    'Wednesday' => 'Waffles'
};

Setting: php.format.rules.arrayInitializersAlignKeyValuePairs

Align key-value pairs: Automatically align => operators.

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

Setting: php.format.rules.arrayInitializersNewLineAfterLastElement

New line after last element: Place a new line after the last element when wrapping.

true false
$x = [
    1,
    2
];
$x = [
    1, 
    2];

Setting: php.format.rules.arrayInitializersNewLineBeforeFirstElement

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

true false
$x = [
    1,
    2
];
$x = [1, 
    2
];

Setting: php.format.rules.arrayInitializersWrap

Wrap: 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.

Setting: php.format.rules.callParametersNewLineAfterLeftParen

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

true false
foo(
    1,
    2
);
foo(1,
    2
);

Setting: php.format.rules.callParametersNewLineBeforeRightParen

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

true false
foo(
    1,
    2
);
foo(
    1,
    2);

Setting: php.format.rules.callParametersWrap

Wrap: 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.

Setting: php.format.rules.chainedMethodCallsSemicolonOnNewLine

; on a new line: Place ; on a new line after the last element in the chained method call when wrapping.

true false
$x->aaa()
    ->bbb()
;
$x->aaa()
    ->bbb();

Setting: php.format.rules.chainedMethodCallsWrap

Wrap: Defines wrapping behavior for chained method calls.

  • Off - No wrapping is applied
  • Always - When this option is selected each method call is placed on a new line and properly indented.

Setting: php.format.rules.declCompactEmptyBody

Compact empty body into {}: Transform empty body into {} and keep it at the same line as a previous symbol, separated by single space.

true false
function foo(
    $x,
    $y
) {}
function foo(
    $x,
    $y
)
{
}

Setting: php.format.rules.declKeepRightParenAndOpenBraceOnOneLine

Keep ) and { on one line: Keep the right closing parenthesis ) of a function or method declaration header on the one line as opening brace of the body {.

true false
function foo(
    $x,
    $y
) {
}
function foo(
    $x,
    $y
)
{
}

Setting: php.format.rules.declParametersNewLineAfterLeftParen

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

true false
function foo(
    $x,
    $y
) {
}
function foo($x,
    $y
) {
}

Setting: php.format.rules.declParametersNewLineBeforeRightParen

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

true false
function foo(
    $x,
    $y
) {
}
function foo(
    $x,
    $y) {
}

Setting: php.format.rules.declParametersWrap

Wrap: 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.

Setting: php.format.rules.forStatementNewLineAfterLeftParen

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

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

Setting: php.format.rules.forStatementNewLineBeforeRightParen

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

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

Setting: php.format.rules.forStatementWrap

Wrap: 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.

Setting: php.format.rules.groupUseNewLineBeforeFirstDeclaration

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

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

Setting: php.format.rules.groupUseWrap

Wrap: 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.

Setting: php.format.rules.ifStatementNewLineAfterLeftParen

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

true false
if(
    true
) {
}
if(true
) {
}

Setting: php.format.rules.ifStatementNewLineBeforeRightParen

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

true false
if(
    true
) {
}
if(
    true) {
}

Setting: php.format.rules.implementsListWrap

Wrap: 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.

Setting: php.format.rules.keepClassesOnOneLine

Keep classes on one line: Clasess are kept on one line.

true false
class x { }
class x {
}

Setting: php.format.rules.keepControlStatementsOnOneLine

Keep control statements on one line: Control statements are kept on one line.

true false
if ($x) return;
if ($x)
    return;

Setting: php.format.rules.keepFunctionsOnOneLine

Keep functions on one line: Functions and methods are kept on one line.

true false
function foo() { return 1; }
function foo() {
    return 1;
}

Setting: php.format.rules.newLineAfterImplements

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

true false
class X implements
    A,
    B,
    C
{
}
class X implements A, 
    B, 
    C
{
}

Setting: php.format.rules.switchStatementNewLineAfterLeftParen

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

true false
switch(
    $x
) {
}
switch($x
) {
}

Setting: php.format.rules.switchStatementNewLineBeforeRightParen

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

true false
switch(
    $x
) {
}
switch(
    $x) {
}

Setting: php.format.rules.whileStatementNewLineAfterLeftParen

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

true false
while(
    $x == 2
) {
}
while($x == 2
) {
}

Setting: php.format.rules.whileStatementNewLineBeforeRightParen

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

true false
while(
    $x == 2
) {
}
while(
    $x == 2) {
}

Conversions

Setting: php.format.rules.addCommaAfterLastArrayElement

Add a comma after the last array element: Automatically adds a comma after the last array element if it's not on a single line.

Setting: php.format.rules.addCommaAfterLastCallParameter

Add a comma after the last function call parameters: Automatically adds a comma after the last function call parameter if it's not on a single line.

Setting: php.format.rules.addCommaAfterLastDeclParameter

Add a comma after the last function declaration parameters: Automatically adds a comma after the last function or method declaration parameter if it's not on a single line.

Setting: php.format.rules.booleanConstantCasing

Boolean constant casing: Defines casing for true and false constants.

  • uppercase - Changes the casing to upper case
  • lowercase - Changes the casing to lower case
  • keep - Keeps the original casing
lowercase uppercase
true, false
TRUE, FALSE

Setting: php.format.rules.nullConstantCasing

Null constant casing: Defines casing for null constant.

  • uppercase - Changes the casing to upper case
  • lowercase - Changes the casing to lower case
  • keep - Keeps the original casing
lowercase uppercase
null
NULL

Setting: php.format.rules.removeReduntandClosingTag

Remove Redundant Closing Tag: The closing ?> tag should be omitted from files containing only PHP.

true false
<?php
// some code
<?php
// some code
?>