spacing.bracefixable
Ensure consistent brace spacing
Examples
- Examples of correct code for this rule using default options
Classes, interfaces, traits, enums and methods
php
<?php
class ReturnType {}
class ReturnTypeVariations {
public function functionName (int $arg1, $arg2): string {
return 'foo';
}
public function secondFunction() {
return 'foo';
}
function functionWithMultipleTypes(int $arg1, $arg2): int {
return 'foo';
}
public function anotherFunction(
string $foo,
string $bar,
int $baz
): string {
return 'foo';
}
}
new class {
public function functionName (int $arg1, $arg2): string {
return 'foo';
}
};
trait TraitName {
public function functionName (int $arg1, $arg2): string {
return 'foo';
}
public function secondFunction() {
return 'foo';
}
}
interface InterfaceName {
public function functionName (int $arg1, $arg2): string;
}
enum Seasons {
case Winter;
case Spring;
case Summer;
case Autumn;
}
Loop and if statements
php
<?php
while ($expr) {
// structure body
}
while ($expr) {
// structure body
}
do {
echo "do while body";
} while ($expr);
for ($i = 0; $i < 10; $i++) {
// for body
}
for ($i = 0; $i < 10; $i++) :
// for short form
endfor;
foreach ($iterable as $key => $value) {
// foreach body
}
foreach (
$iterable as $key => $value) {
// foreach body
}
foreach ($iterable as $key => $value) :
// foreach short form
endforeach;
if ($expr1
) {
// if body
} elseif ($expr2) {
// elseif body
} else {
// else body;
}
if (
$expr1
&& $expr2
) {
// if body
} elseif ($expr3 && $expr4) {
// elseif body
} else {
echo "else";
}
if($expr1):
// if shortform
endif;
Try/catch statements
php
<?php
try {
// try body
} catch (FirstThrowableType $e) {
// catch body
} catch (OtherThrowableType | AnotherThrowableType $e) {
// catch body
} finally {
// finally body
}
try {
// try body
} catch (FirstThrowableType $e) {
// catch body
}
try {
// try body
} catch (FirstThrowableType $e) {
// catch body
} catch (OtherThrowableType | AnotherThrowableType $e) {
// catch body
} finally {
// finally body
}
try {
// try body
} finally {
// finally body
}
try{}
Functions and methods
php
<?php
function hello_world($hello) {
echo "HELLO world";
}
$closure_with_args = function($arg1, $arg2) {
// body
};
$closure_with_args = function($arg1, $arg2): ReturnType {
// body
};
$closure_with_args_and_vars = function($arg1, $arg2)
use ($var1, $var2) {
// body
};
$closure_with_args_vars_and_return = function($arg1, $arg2) use
($var1, $var2): bool {
// body
};
array_map(function($arg) {
// body
}, $array);
array_walk($array, function($value, $key) use ($outer_value): bool {
// body
});
Switch and match statements
php
<?php
switch ($foo) {
case 1:
switch ($bar) {
default:
echo $string[1];
}
break;
}
function temp($foo, $bar) {
switch ($foo) {
case 1:
switch ($bar) {
default:
return $foo;
}
break;
}
}
switch ($foo) {
case 1:
switch ($bar) {
default:
if ($something) {
echo $string[1];
} elseif ($else) {
switch ($else) {
default:
}
}
}
break;
}
switch ($name) {
case "1":
case "2":
case "3":
return true;
}
switch ($name
) {
case "1":
case "2":
case "3":
return true;
}
switch ($id):
case 1:
case 2:
break;
endswitch;
match ($food) {
'apple' => 'This food is an apple',
'orange' => 'This food is a orange',
'cake' => 'This food is a cake',
};
match ($food
) {
'apple' => 'This food is an apple',
'orange' => 'This food is a orange',
'cake' => 'This food is a cake',
};
match($month_name) {
'jan' => 31,
'feb' => is_leap_year($year) ? 29 : 28,
'mar' => 31,
'apr' => 30,
'may' => 31,
'jun' => 30,
'jul' => 31,
'aug' => 31,
'sep' => 30,
'oct' => 31,
'nov' => 30,
'dec' => 31,
default => throw new InvalidArgumentException("Invalid month")
};
- Examples of incorrect code for this rule using default options
Classes, interfaces, traits, enums and methods
php
<?php
class ReturnType { }
class ReturnTypeVariations{
public function functionName (int $arg1, $arg2): string{
return 'foo';
}
public function secondFunction() {
return 'foo';
}
function functionWithMultipleTypes(int $arg1, $arg2): int{
return 'foo';
}
public function anotherFunction(
string $foo,
string $bar,
int $baz
): string {
return 'foo';
}
}
new class{
public function functionName (int $arg1, $arg2): string {
return 'foo';
}
};
trait TraitName{
public function functionName (int $arg1, $arg2): string{
return 'foo';
}
public function secondFunction() {
return 'foo';
}
}
interface InterfaceName{
public function functionName (int $arg1, $arg2): string;
}
enum Seasons{
case Winter;
case Spring;
case Summer;
case Autumn;
}
Loop and if statements
php
<?php
while ($expr){
// structure body
}
while ($expr) {
// structure body
}
do {
echo "do while body";
} while ($expr);
for ($i = 0; $i < 10; $i++) {
// for body
}
foreach ($iterable as $key => $value) {
// foreach body
}
foreach (
$iterable as $key => $value){
// foreach body
}
if ($expr1
) {
// if body
} elseif ($expr2) {
// elseif body
}else{
// else body;
}
if (
$expr1
&& $expr2
){
// if body
} elseif ($expr3 && $expr4) {
// elseif body
}else {
echo "else";
}
Try/catch statements
php
<?php
try {
// try body
}catch (FirstThrowableType $e) {
// catch body
} catch (OtherThrowableType | AnotherThrowableType $e) {
// catch body
}finally{
// finally body
}
try{
// try body
} catch (FirstThrowableType $e){
// catch body
}
try {
// try body
}catch (FirstThrowableType $e){
// catch body
} catch (OtherThrowableType | AnotherThrowableType $e) {
// catch body
} finally {
// finally body
}
try {
// try body
}finally {
// finally body
}
try{ }
Functions and methods
php
<?php
function hello_world($hello) {
echo "HELLO world";
}
$closure_with_args = function($arg1, $arg2) {
// body
};
$closure_with_args = function($arg1, $arg2): ReturnType{
// body
};
$closure_with_args_and_vars = function($arg1, $arg2)
use ($var1, $var2){
// body
};
$closure_with_args_vars_and_return = function($arg1, $arg2) use
($var1, $var2): bool {
// body
};
array_map(function($arg) {
// body
}, $array);
array_walk($array, function($value, $key) use ($outer_value): bool{
// body
});
Switch and match statements
php
<?php
switch ($foo) {
case 1:
switch ($bar){
default:
echo $string[1];
}
break;
}
function temp($foo, $bar) {
switch ($foo){
case 1:
switch ($bar) {
default:
return $foo;
}
break;
}
}
switch ($foo){
case 1:
switch ($bar) {
default:
if ($something) {
echo $string[1];
} elseif ($else) {
switch ($else) {
default:
}
}
}
break;
}
switch ($name){
case "1":
case "2":
case "3":
return true;
}
switch ($name
){
case "1":
case "2":
case "3":
return true;
}
match ($food){
'apple' => 'This food is an apple',
'orange' => 'This food is a orange',
'cake' => 'This food is a cake',
};
match ($food
) {
'apple' => 'This food is an apple',
'orange' => 'This food is a orange',
'cake' => 'This food is a cake',
};
match($month_name) {
'jan' => 31,
'feb' => is_leap_year($year) ? 29 : 28,
'mar' => 31,
'apr' => 30,
'may' => 31,
'jun' => 30,
'jul' => 31,
'aug' => 31,
'sep' => 30,
'oct' => 31,
'nov' => 30,
'dec' => 31,
default => throw new InvalidArgumentException("Invalid month")
};