PHP 8.6 vyšlo. DressCode na něj váš kód přepíše. Přechod na novější PHP →

Složené závorky

Žádné rozhodnutí o stylu není tak vidět jako to, kam patří {, a v žádném se styly nerozcházejí častěji. Sekce braces staví otevírací závorku na řádek hlavičky, nebo na další, a to zvlášť pro třídu, funkci, closure (anonymní funkci) i řídicí strukturu. Rozhoduje také, kam patří else a catch, jak se píše prázdné tělo a kdy jsou závorky povinné.

Každý druh konstrukce má proto vlastní klíč: třída, funkce, funkce s parametry na několika řádcích, closure, anonymní třída a řídicí struktura. PSR-12 a PER Coding Style 3.1 dávají deklaracím závorku na další řádek a všemu ostatnímu na řádek hlavičky:

braces:
	position:
		class: nextLine
		function: nextLine
		closure: sameLine
		controlStructure: sameLine
		continuingKeyword: sameLine
class Cart {  // Expected a line break before the opening brace.
	public function add(Item $item): void {  // Expected a line break before the opening brace.
		if ($item->isFree())
		{  // Expected no line break before the opening brace.
			return;
		}
		$this->items[] = $item;
	}
}
class Cart
{
	public function add(Item $item): void
	{
		if ($item->isFree()) {
			return;
		}
		$this->items[] = $item;
	}
}

Kromě otevírací závorky hlídá DressCode i to, že tělo za ní začíná na novém řádku a zavírací závorka stojí na řádku sama. Výjimky mají vlastní klíče: prázdné tělo {}, o kterém braces.emptyBody rozhoduje zvlášť pro třídu, funkci, closure, property hook, anonymní třídu a řídicí strukturu, a closure napsaná celá na jednom řádku (braces.singlelineClosure). Odsazení řádků, které tak vzniknou, je věcí sekce indentation, prázdné řádky za závorkou a před ní věcí sekce blankLines.

braces.position.class

Where the { of a class, an interface, a trait and an enum stands, the members starting a line below it and the } taking a line of its own.

  • sameLine: on the line of what comes before
  • nextLine: on the next line
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs nextLine, psr12 nextLine, nette nextLine, symfony nextLine, laravel nextLine · pokrývá braces_position, curly_braces_position, PSR2.Classes.ClassDeclaration

braces:
	position:
		class: sameLine
class Cart
{  // Expected no line break before the opening brace.
	private array $items = [];
}
class Cart {
	private array $items = [];
}

braces.position.function

Where the { of a function and of a method whose parameters stand on one line stands, the { of a property hook and of the list of hooks staying on the line of its head; the body starts a line below it and the } takes a line of its own.

  • sameLine: on the line of what comes before
  • nextLine: on the next line
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs nextLine, psr12 nextLine, nette nextLine, symfony nextLine, laravel nextLine · pokrývá braces_position, curly_braces_position, Squiz.Functions.MultiLineFunctionDeclaration

Všech pět standardů dává závorku funkce na další řádek. Kdo ji chce mít na řádku hlavičky, napíše sameLine; funkce s parametry na několika řádcích se řídí klíčem braces.position.multilineSignature.

braces:
	position:
		function: sameLine
function total(array $items): int
{  // Expected no line break before the opening brace.
	return count($items);
}
function total(array $items): int {
	return count($items);
}

braces.position.multilineSignature

Where the { of a function and of a method whose parameters are spread over lines stands.

  • sameLine: on the line of the )
  • nextLine: on the line below the )
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs sameLine, psr12 sameLine, nette sameLine, symfony sameLine, laravel sameLine · pokrývá braces_position, curly_braces_position, Squiz.Functions.MultiLineFunctionDeclaration

braces.position.typedMultilineSignature

Where the { of a function and of a method whose parameters are spread over lines stands after a return type; where it is kept, braces.position.multilineSignature says it.

  • sameLine: on the line of the return type
  • nextLine: on the line below the return type
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette nextLine, symfony keep, laravel keep

Hodnotu nextLine má Nette Coding Standard: funkce bez návratového typu má ) { jako v PER Coding Style, u funkce s návratovým typem stojí ): void na samostatném řádku a závorka jde pod něj.

braces:
	position:
		multilineSignature: sameLine
		typedMultilineSignature: nextLine
function send(
	string $to,
	string $subject,
): void {  // Expected a line break before the opening brace.
	mail($to, $subject);
}

function log(
	string $message,
)
{  // Expected no line break before the opening brace.
	echo $message;
}
function send(
	string $to,
	string $subject,
): void
{
	mail($to, $subject);
}

function log(
	string $message,
) {
	echo $message;
}

braces.position.closure

Where the { of a closure stands, the body starting a line below it and the } taking a line of its own.

  • sameLine: on the line of what comes before
  • nextLine: on the next line
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs sameLine, psr12 sameLine, nette sameLine, symfony sameLine, laravel sameLine · pokrývá braces_position, curly_braces_position, Squiz.Functions.MultiLineFunctionDeclaration

braces:
	position:
		closure: nextLine
$double = function (int $x) {  // Expected a line break before the opening brace.
	return $x * 2;
};
$double = function (int $x)
{
	return $x * 2;
};

braces.position.anonymousClass

Where the { of an anonymous class stands, on the next line always where its interfaces are spread over lines, the members starting a line below it and the } taking a line of its own.

  • sameLine: on the line of what comes before
  • nextLine: on the next line
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs sameLine, psr12 sameLine, nette sameLine, symfony sameLine, laravel nextLine · pokrývá braces_position, curly_braces_position, PSR12.Classes.AnonClassDeclaration

braces:
	position:
		anonymousClass: nextLine
$logger = new class implements Logger {  // Expected a line break before the opening brace.
	public function log(string $message): void
	{
		echo $message;
	}
};
$logger = new class implements Logger
{
	public function log(string $message): void
	{
		echo $message;
	}
};

braces.position.controlStructure

Where the { of if, else, a loop, declare, try, catch, finally, switch and match stands, the body starting a line below it and the } taking a line of its own.

  • sameLine: on the line of what comes before
  • nextLine: on the next line
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs sameLine, psr12 sameLine, nette sameLine, symfony sameLine, laravel sameLine · pokrývá braces_position, curly_braces_position, PSR12.Files.DeclareStatement, Squiz.ControlStructures.ControlSignature

braces:
	position:
		controlStructure: nextLine
if ($ready) {  // Expected a line break before the opening brace.
	start();
}
if ($ready)
{
	start();
}

braces.position.continuingKeyword

Where the keyword continuing a structure after its } stands: else, elseif, catch, finally and the while of do.

  • sameLine: on the line of what comes before
  • nextLine: on the next line
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs sameLine, psr12 sameLine, nette sameLine, symfony sameLine, laravel sameLine · pokrývá control_structure_continuation_position, Squiz.ControlStructures.ControlSignature

braces:
	position:
		continuingKeyword: sameLine
try {
	connect();
}
catch (ConnectionException $e) {  // Expected no line break before the `catch` keyword.
	retry();
}
try {
	connect();
} catch (ConnectionException $e) {
	retry();
}

Kdo začíná každou větev na novém řádku, napíše nextLine:

braces:
	position:
		continuingKeyword: nextLine
if ($ready) {
	start();
} else {  // Expected a line break before the `else` keyword.
	wait();
}
if ($ready) {
	start();
}
else {
	wait();
}

braces.emptyBody.class

How the empty body of a class, an interface, a trait and an enum is written; a comment inside makes it not empty.

  • collapsed: {} on the line of the head
  • expanded: { and } placed as those of any other body
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs collapsed, psr12 expanded, nette expanded, symfony expanded, laravel collapsed · pokrývá single_line_empty_body

PER Coding Style prázdná těla zkracuje, a proto má preset perCs hodnotu collapsed; PSR-12 a ostatní standardy píšou závorky každou na svůj řádek.

braces:
	emptyBody:
		class: collapsed
		function: collapsed
class NotFound extends Exception
{  // Expected no line break before the opening brace.
}  // Expected no line break before the closing brace.

class Point
{
	public function __construct(private int $x, private int $y)
	{  // Expected no line break before the opening brace.
	}  // Expected no line break before the closing brace.
}
class NotFound extends Exception {}

class Point
{
	public function __construct(private int $x, private int $y) {}
}

braces.emptyBody.function

How the empty body of a function and a method is written; a comment inside makes it not empty.

  • collapsed: {} on the line of the head
  • expanded: { and } placed as those of any other body
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs collapsed, psr12 expanded, nette expanded, symfony expanded, laravel collapsed · pokrývá single_line_empty_body

braces.emptyBody.closure

How the empty body of a closure is written; a comment inside makes it not empty.

  • collapsed: {} on the line of the head
  • expanded: { and } placed as those of any other body
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs collapsed, psr12 expanded, nette expanded, symfony expanded, laravel collapsed · pokrývá single_line_empty_body

braces.emptyBody.propertyHook

How the empty body of a property hook is written; a comment inside makes it not empty.

  • collapsed: {} on the line of the head
  • expanded: { and } placed as those of any other body
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs collapsed, psr12 expanded, nette expanded, symfony expanded, laravel keep

braces.emptyBody.anonymousClass

How the empty body of an anonymous class is written, one written on one line staying where it is kept; a comment inside makes it not empty.

  • collapsed: {} on the line of the head, one holding a comment staying on its line as written
  • expanded: { and } placed as those of any other body
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs collapsed, psr12 keep, nette keep, symfony keep, laravel collapsed · pokrývá braces_position, curly_braces_position, single_line_empty_body

braces.emptyBody.controlStructure

How the empty body of an if, else, a loop, declare, try, catch and finally is written; a comment inside makes it not empty.

  • collapsed: {} on the line of the head
  • expanded: { and } placed as those of any other body
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette keep, symfony keep, laravel keep

Žádný standard prázdnou řídicí strukturu nezkracuje; collapsed je pro projekt, který chce cyklus bez těla vidět na jednom řádku. Cyklus, v jehož těle je komentář, prázdný není a zůstane, jak je.

braces:
	emptyBody:
		controlStructure: collapsed
while ($reader->next()) {
}  // Expected no line break before the closing brace.
while ($reader->next()) {}

braces.singlelineClosure

A closure written whole on one line.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs forbidden, psr12 forbidden, nette keep, symfony keep, laravel forbidden · pokrývá braces_position, curly_braces_position

PSR-12 chce tělo každé closure na vlastních řádcích, a proto mají presety psr12 a perCs hodnotu forbidden.

braces:
	singlelineClosure: forbidden
$ids = array_map(function ($row) { return $row->id; }, $rows);  // Expected a line break after the opening brace. // Expected a line break before the closing brace.
$ids = array_map(function ($row) {
	return $row->id;
}, $rows);

braces.bracelessBody

The body of a control structure written without braces, if ($a) foo();, the braces going on its line where a template writes it before ?>.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · pokrývá control_structure_braces, Generic.ControlStructures.InlineControlStructure

braces:
	bracelessBody: forbidden
if ($item->isFree()) return;  // The body of the `if` must be enclosed in braces.
if ($item->isFree()) {
	return;
}

braces.alternativeSyntax

if: … endif; is written with braces, a structure a template writes around markup excepted.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · pokrývá no_alternative_syntax

braces.bareStatementGroup

{ … } around statements that nothing opens.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · pokrývá no_unneeded_braces, no_unneeded_curly_braces

Zobrazit na GitHubu · Nahlásit problém se stránkou na GitHubu · 9. 11. 2026