Spacing

The spacing section decides on the spaces within a line: around operators, after a comma, inside parentheses and brackets, between a function name and the parenthesis, after a keyword, around -> and ::, in a type declaration and in a comment.

For spaces, the value is a code sample: the construct written with exactly the space you want, in single quotes. This is the easiest form to read, because it shows exactly what you will see in the code:

spacing:
	concatenation: '$a.$b'
	arrowFunction: 'fn ($x) => $x'
	cast: '(int) $x'
	catchType: 'catch (A|B $e)'

DressCode does not parse the sample as code; it only compares it with the samples the key knows. An unknown sample is a configuration error, and the message lists the known ones. Each key controls only the space it names: in 'fn ($x) => $x' it is the space after fn, while the spaces around => belong to another key. A sample is exact, so a wider space does not pass; extra spaces can stay only where they align columns and the group spacing.alignment allows it. The inside of brackets is not set with a sample but with a plain word: the group spacing.padding says for each kind of bracket whether a space must be inside (required) or must not be (forbidden).

$label = 'Item '.$name . ' x'.$count;  // Expected no whitespace before the `.` operator. // Expected no whitespace after the `.` operator.
$double = fn($x) => $x * 2;  // Expected a single space after the `fn` keyword.
$id = (int)$row['id'];  // Expected a single space after the cast.
$label = 'Item '.$name.' x'.$count;
$double = fn ($x) => $x * 2;
$id = (int) $row['id'];

Spacing never decides on line breaks: the space around an operator at a line break is a matter of indentation and of the multiline section, not of this one. Where extra spaces align columns, the parameters of the group spacing.alignment (binary operator, comma, ternary operator and a comment after code) decide whether the alignment may stay; DressCode itself never aligns columns.

spacing.keyword

The space after a keyword followed by code on its line, return, new, if, function, a modifier and the words of the head of a class among them, and around a keyword continuing a statement, else, catch, finally, the while of a do, as, insteadof and the use of a closure, which stay on the line of what is before them.

  • 'return $x': a single space
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'return $x', psr12 'return $x', nette 'return $x', symfony 'return $x', laravel 'return $x' · covers function_declaration, single_space_around_construct, Generic.WhiteSpace.LanguageConstructSpacing, PSR12.Classes.AnonClassDeclaration, PSR12.Traits.UseDeclaration, PSR2.Classes.ClassDeclaration, PSR2.Classes.ClassDeclaration.SpaceBeforeKeyword, PSR2.ControlStructures.SwitchDeclaration, Squiz.ControlStructures.ControlSignature, Squiz.ControlStructures.ForEachLoopDeclaration, Squiz.Functions.FunctionDeclaration

spacing.anonymousClass

The space between class and the arguments of an anonymous class.

  • 'new class ($a)': a single space before the arguments
  • 'new class($a)': no space before the arguments
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'new class ($a)', psr12 'new class ($a)', nette 'new class ($a)', symfony 'new class($a)', laravel 'new class($a)' · covers class_definition, PSR12.Classes.AnonClassDeclaration

spacing.comment.marker

The space after the marker of a comment and before the closing one, unless another / or * follows the marker, as in a //// ruler; doc comments are the matter of the phpDoc rules.

  • '// text': a single space after the marker
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette '// text', symfony '// text', laravel '// text' · covers PhpCsFixerCustomFixers/comment_surrounded_by_spaces, single_line_comment_spacing

spacing.comment.afterCode

The whitespace before a comment that follows code on its line.

  • '$a; // text': at least one space
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette '$a; // text', symfony '$a; // text', laravel keep

spacing.alignment.comment

The whitespace wider than a single space before a comment following code, aligning a column of comments; alignment is never made.

  • forbidden: never there, more whitespace collapsing to a single space
  • optional: there or not, as the author wrote it

Default optional · standards: perCs optional, psr12 optional, nette optional, symfony optional, laravel optional

spacing.switchCase

The whitespace before the colon of a case.

  • 'case 1:': none before the colon
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'case 1:', psr12 'case 1:', nette 'case 1:', symfony 'case 1:', laravel 'case 1:' · covers switch_case_space, PSR2.ControlStructures.SwitchDeclaration, PSR2.ControlStructures.SwitchDeclaration.SpaceBeforeColonCASE

spacing.binaryOperator

The spaces around a binary operator, an assignment, instanceof, => and the = of a default included, . being spacing.concatenation; at a line break an operator takes no space on the side of the break, an assignment and => stay on the line of what is before them, and instanceof and a comparison keep what follows them on their line, unless the line would grow too wide.

  • '$a + $b': a single space around
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '$a + $b', psr12 '$a + $b', nette '$a + $b', symfony '$a + $b', laravel '$a + $b' · covers binary_operator_spaces, PSR12.Operators.OperatorSpacing, Squiz.WhiteSpace.LogicalOperatorSpacing, Squiz.WhiteSpace.OperatorSpacing

It applies to arithmetic, comparison, logical and bitwise operators, ??, assignments including += and ??=, instanceof, => and the = of a default value. The dot has its own key concatenation, because coding styles disagree about it.

spacing:
	binaryOperator: '$a + $b'
$total=$price*$count;  // Expected at least one space before the `=` operator. // Expected at least one space after the `=` operator. // Expected at least one space before the `*` operator. // Expected at least one space after the `*` operator.
$label = $count>1 ? 'items' : 'item';  // Expected at least one space before the `>` operator. // Expected at least one space after the `>` operator.
$map = ['a'=>1, 'b' => 2];  // Expected at least one space before the double arrow. // Expected at least one space after the double arrow.
$aligned   = 1;
$alsoFine  = 2;
$total = $price * $count;
$label = $count > 1 ? 'items' : 'item';
$map = ['a' => 1, 'b' => 2];
$aligned   = 1;
$alsoFine  = 2;

The last two lines stayed: the default of spacing.alignment.binaryOperator allows alignment with spaces.

spacing.concatenation

The space around the concatenation operator, unless it sits at a line break.

  • '$a . $b': a single space around
  • '$a.$b': no space around
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '$a . $b', psr12 '$a . $b', nette '$a . $b', symfony '$a.$b', laravel '$a.$b' · covers concat_space, PSR12.Operators.OperatorSpacing, Squiz.Strings.ConcatenationSpacing

spacing.alignment.binaryOperator

The whitespace wider than a single space around an operator, aligning a column of assignments = or of array items =>; alignment is never made.

  • forbidden: never there, more whitespace collapsing to a single space
  • spaces: spaces aligning a column staying, tabs not
  • tabs: tabs aligning a column staying, spaces not
  • optional: there or not, as the author wrote it

Default spaces · standards: perCs spaces, psr12 spaces, nette spaces, symfony forbidden, laravel forbidden · covers binary_operator_spaces, PSR12.Operators.OperatorSpacing, Squiz.WhiteSpace.LogicalOperatorSpacing, Squiz.WhiteSpace.OperatorSpacing

spacing:
	binaryOperator: '$a + $b'
	alignment:
		binaryOperator: forbidden
$aligned   = 1;  // Expected a single space before the `=` operator.
$alsoFine  = 2;  // Expected a single space before the `=` operator.
$aligned = 1;
$alsoFine = 2;

spacing.objectOperator

The whitespace around the object operator.

  • '$a->b()': none around
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette '$a->b()', symfony '$a->b()', laravel '$a->b()' · covers object_operator_without_whitespace, Squiz.WhiteSpace.ObjectOperatorSpacing

spacing.doubleColon

The whitespace around the double colon.

  • 'Foo::bar()': none around
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette 'Foo::bar()', symfony keep, laravel 'Foo::bar()' · covers no_space_around_double_colon, Squiz.WhiteSpace.ScopeKeywordSpacing

spacing.unaryOperator

The whitespace between a unary operator other than ! and its operand, which stay on one line, and inside a variable variable, $$a and ${'a'}.

  • '-$x': no space after the operator
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '-$x', psr12 '-$x', nette '-$x', symfony '-$x', laravel '-$x' · covers unary_operator_spaces, Generic.WhiteSpace.IncrementDecrementSpacing, Squiz.WhiteSpace.OperatorSpacing.Unary

spacing.logicalNot

The whitespace between ! and its operand, which stay on one line.

  • '!$x': no space after the operator
  • '! $x': a single space after the operator
  • a list of these samples, where each one passes and code matching none of them is written like the first
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs ['!$x', '! $x'], psr12 ['!$x', '! $x'], nette '!$x', symfony '!$x', laravel '! $x' · covers not_operator_with_successor_space, not_operator_without_successor_space, unary_operator_spaces, SlevomatCodingStandard.Operators.NegationOperatorSpacing, Squiz.WhiteSpace.OperatorSpacing.Unary

spacing.cast

The space between a cast and its operand, which stays on the line of the cast.

  • '(int) $x': a single space after the cast
  • '(int)$x': no space after the cast
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '(int) $x', psr12 '(int) $x', nette '(int) $x', symfony '(int) $x', laravel '(int) $x' · covers cast_spaces

spacing.reference

The whitespace between & and its operand.

  • '&$x': none between
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '&$x', psr12 '&$x', nette '&$x', symfony '&$x', laravel keep · covers SlevomatCodingStandard.PHP.ReferenceSpacing

spacing.spread

The whitespace between ... and its operand.

  • '...$x': none between
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '...$x', psr12 '...$x', nette '...$x', symfony '...$x', laravel keep · covers SlevomatCodingStandard.Operators.SpreadOperatorSpacing

spacing.ternary

The whitespace around ? and : of a ternary, and around ?: as a whole, unless the operator sits at a line break.

  • '$a ? $b : $c': whitespace around ? and :
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '$a ? $b : $c', psr12 '$a ? $b : $c', nette '$a ? $b : $c', symfony '$a ? $b : $c', laravel '$a ? $b : $c' · covers ternary_operator_spaces, PSR12.Operators.OperatorSpacing

spacing.alignment.ternary

The whitespace wider than a single space around ? and :, aligning a column; alignment is never made.

  • forbidden: never there, more whitespace collapsing to a single space
  • optional: there or not, as the author wrote it

Default optional · standards: perCs optional, psr12 optional, nette forbidden, symfony optional, laravel optional · covers ternary_operator_spaces, PSR12.Operators.OperatorSpacing

spacing.declare

The whitespace inside a declare statement.

  • 'declare(strict_types=1)': none inside
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'declare(strict_types=1)', psr12 'declare(strict_types=1)', nette 'declare(strict_types=1)', symfony 'declare(strict_types=1)', laravel 'declare(strict_types=1)' · covers declare_equal_normalize, declare_parentheses, PSR12.Files.DeclareStatement

spacing.call

The whitespace between the name of a function and its parenthesis, of a call and of a declaration alike.

  • 'foo($a, $b)': no space before the parenthesis
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'foo($a, $b)', psr12 'foo($a, $b)', nette 'foo($a, $b)', symfony 'foo($a, $b)', laravel 'foo($a, $b)' · covers function_declaration, no_spaces_after_function_name, Generic.Functions.FunctionCallArgumentSpacing.SpaceBeforeOpenBracket, PSR2.Methods.FunctionCallSignature, Squiz.Functions.FunctionDeclaration

spacing.namedArgument

The whitespace around the colon of a named argument.

  • 'foo(name: $x)': none before the colon, a single space after
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'foo(name: $x)', psr12 keep, nette 'foo(name: $x)', symfony 'foo(name: $x)', laravel keep · covers SlevomatCodingStandard.Functions.NamedArgumentSpacing

spacing.typeDeclaration

The whitespace of a type declaration, which stays on one line with what it describes: none inside a type, a single space between a type and the name it declares, ): int for a return type and enum Suit: string for a backing type.

  • '?int $x': none inside a type, a single space before the name
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '?int $x', psr12 '?int $x', nette '?int $x', symfony '?int $x', laravel '?int $x' · covers compact_nullable_type_declaration, return_type_declaration, type_declaration_spaces, types_spaces, PSR12.Functions.NullableTypeDeclaration, PSR12.Functions.ReturnTypeDeclaration, SlevomatCodingStandard.Classes.BackedEnumTypeSpacing, SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing, SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing

spacing.catchType

The space around the bar between the types of a catch.

  • 'catch (A | B $e)': a single space around the bar
  • 'catch (A|B $e)': no space around the bar
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'catch (A|B $e)', psr12 'catch (A | B $e)', nette 'catch (A|B $e)', symfony 'catch (A|B $e)', laravel 'catch (A|B $e)'

spacing.attribute

The whitespace inside the brackets of an attribute group and before the parenthesis of its arguments, and the single space between two groups on a line and after the last one.

  • '#[Foo($a)]': none inside the brackets, none before the parenthesis
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '#[Foo($a)]', psr12 keep, nette '#[Foo($a)]', symfony '#[Foo($a)]', laravel keep · covers attribute_block_no_spaces

spacing.comma

The whitespace around a comma, which stays on the line of what is before it, a line ending after it being free.

  • '$a, $b': none before, a single space after
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '$a, $b', psr12 '$a, $b', nette '$a, $b', symfony '$a, $b', laravel '$a, $b' · covers no_whitespace_before_comma_in_array, whitespace_after_comma_in_array, Generic.Functions.FunctionCallArgumentSpacing, Squiz.Functions.FunctionDeclarationArgumentSpacing

spacing.alignment.comma

The whitespace wider than a single space after a comma, aligning the columns of a table; alignment is never made.

  • forbidden: never there, more whitespace collapsing to a single space
  • spaces: spaces aligning a column staying, tabs not
  • tabs: tabs aligning a column staying, spaces not
  • optional: there or not, as the author wrote it

Default tabs · standards: perCs forbidden, psr12 forbidden, nette tabs, symfony optional, laravel optional · covers no_whitespace_before_comma_in_array, whitespace_after_comma_in_array, Generic.Functions.FunctionCallArgumentSpacing, Squiz.Functions.FunctionDeclarationArgumentSpacing

spacing.openBrace

The space before the brace or the body a structure opens on the line of its head.

  • 'if ($a) {': a single space
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'if ($a) {', psr12 'if ($a) {', nette 'if ($a) {', symfony 'if ($a) {', laravel 'if ($a) {' · covers Squiz.ControlStructures.ControlSignature

spacing.alternativeSyntax

The space before the colon of the alternative syntax and of a label, and the single space between it and the end keyword of an empty body, if ($a): endif;.

  • 'if ($a):': no space before the colon
  • 'if ($a) :': a single space before the colon
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'if ($a):', psr12 'if ($a):', nette 'if ($a):', symfony 'if ($a):', laravel keep

spacing.body

The space inside the braces of a body whose code stands on their line, of a function, a closure, a property hook and an anonymous class, and of abbreviated property hooks, { get; set; }; none inside a pair of braces with nothing between them whose empty form braces.emptyBody does not decide.

  • 'function () { return 1; }': a single space inside the braces
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'function () { return 1; }', psr12 'function () { return 1; }', nette 'function () { return 1; }', symfony 'function () { return 1; }', laravel keep

spacing.import

The space around the braces of a group use, which hug the names.

  • 'use Acme\{Foo, Bar};': no space around the braces
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'use Acme\{Foo, Bar};', psr12 'use Acme\{Foo, Bar};', nette 'use Acme\{Foo, Bar};', symfony 'use Acme\{Foo, Bar};', laravel keep

spacing.arrowFunction

The space between fn and its parenthesis.

  • 'fn($x) => $x': no space before the parenthesis
  • 'fn ($x) => $x': a single space
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs 'fn($x) => $x', psr12 keep, nette 'fn($x) => $x', symfony 'fn ($x) => $x', laravel 'fn ($x) => $x' · covers function_declaration, SlevomatCodingStandard.Functions.ArrowFunctionDeclaration

spacing.padding.argument

The whitespace inside the parentheses of the arguments of a call, of new and of an attribute, and of isset(), unset(), empty() and eval().

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · covers function_declaration, no_spaces_inside_parenthesis, spaces_inside_parentheses, PSR12.Classes.AnonClassDeclaration, PSR12.ControlStructures.ControlStructureSpacing, PSR2.ControlStructures.ControlStructureSpacing, PSR2.Methods.FunctionCallSignature, Squiz.ControlStructures.ForEachLoopDeclaration, Squiz.ControlStructures.ForLoopDeclaration

spacing.padding.parameter

The whitespace inside the parentheses of the parameters of a function, a method, a closure, an arrow function and a property hook.

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · covers function_declaration, no_spaces_inside_parenthesis, spaces_inside_parentheses, PSR12.Classes.AnonClassDeclaration, PSR12.ControlStructures.ControlStructureSpacing, PSR2.ControlStructures.ControlStructureSpacing, PSR2.Methods.FunctionCallSignature, Squiz.ControlStructures.ForEachLoopDeclaration, Squiz.ControlStructures.ForLoopDeclaration

spacing.padding.closureUse

The whitespace inside the parentheses of the use of a closure.

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · covers function_declaration, no_spaces_inside_parenthesis, spaces_inside_parentheses, PSR12.Classes.AnonClassDeclaration, PSR12.ControlStructures.ControlStructureSpacing, PSR2.ControlStructures.ControlStructureSpacing, PSR2.Methods.FunctionCallSignature, Squiz.ControlStructures.ForEachLoopDeclaration, Squiz.ControlStructures.ForLoopDeclaration

spacing.padding.controlStructure

The whitespace inside the parentheses of if, elseif, while, for, foreach, switch, match and catch.

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · covers function_declaration, no_spaces_inside_parenthesis, spaces_inside_parentheses, PSR12.Classes.AnonClassDeclaration, PSR12.ControlStructures.ControlStructureSpacing, PSR2.ControlStructures.ControlStructureSpacing, PSR2.Methods.FunctionCallSignature, Squiz.ControlStructures.ForEachLoopDeclaration, Squiz.ControlStructures.ForLoopDeclaration, Squiz.WhiteSpace.ControlStructureSpacing

spacing.padding.grouping

The whitespace inside the parentheses grouping an expression, ($a + $b) * $c.

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · covers function_declaration, no_spaces_inside_parenthesis, spaces_inside_parentheses, PSR12.Classes.AnonClassDeclaration, PSR12.ControlStructures.ControlStructureSpacing, PSR2.ControlStructures.ControlStructureSpacing, PSR2.Methods.FunctionCallSignature, Squiz.ControlStructures.ForEachLoopDeclaration, Squiz.ControlStructures.ForLoopDeclaration

spacing.padding.array

The whitespace inside the brackets or the parentheses of an array.

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · covers trim_array_spaces, SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace

spacing.padding.list

The whitespace inside the brackets or the parentheses of a destructuring.

  • forbidden: never there
  • required: always there
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · covers trim_array_spaces, SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace

spacing.padding.offset

The whitespace inside the brackets of an offset access and before them.

  • forbidden: never there
  • required: always there
  • exceptLiterals: a single space around an index other than a string or an integer literal, $a[ $i ] but $a[0]
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · covers no_spaces_around_offset, Squiz.Arrays.ArrayBracketSpacing

spacing.semicolon.before

The whitespace before a semicolon, which stays on the line of what it ends.

  • '$a;': no space before a semicolon
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs '$a;', psr12 keep, nette '$a;', symfony '$a;', laravel '$a;' · covers no_singleline_whitespace_before_semicolons, PSR12.Traits.UseDeclaration, Squiz.ControlStructures.ForLoopDeclaration, Squiz.WhiteSpace.SemicolonSpacing

spacing.semicolon.after

The whitespace after a semicolon that more code follows on its line.

  • 'for ($i = 0; $i < 5; $i++)': a single space after a semicolon
  • keep: nothing is enforced, the code stays as it is

Default keep · standards: perCs keep, psr12 keep, nette 'for ($i = 0; $i < 5; $i++)', symfony 'for ($i = 0; $i < 5; $i++)', laravel 'for ($i = 0; $i < 5; $i++)' · covers space_after_semicolon, Squiz.ControlStructures.ForLoopDeclaration