Skip to content
On this page

brace-stylefixable

Ensure that opening and closing braces have consistent style

Options

style

The brace style to enforce

Type: string

Default: psr

Possible values: 1tbs, psr{data-desc="psr style. See https://www.php-fig.org/psr/psr-12/ for more information"}

Examples

  • Examples of correct code for this rule using default options

Objects with correct brace style

php
<?php

class Base
{
	protected $anonymous;
	public function __construct() 
	{
		$this->anonymous = new class extends ArrayObject
			{
				public function __construct()
				{
					parent::__construct(['a' => 1, 'b' => 2]);
				}
			};
	}
}

trait Message1
{
	public function msg1() 
	{
		echo "OOP is fun! ";
	}
}

class ClassName extends Directory implements \Countable
{
	private $items = [];
	public function __construct()
	{
	}
	
	public function count(): int
	{
		return 0;
	}
}

class A extends B
implements C
{
}

enum Suit 
{
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}

If statements with correct brace style

php
<?php
if (true) {
	echo "True";
}

// if elseif else
if (true) {
	echo "True";
}  elseif (false) {
	echo "False";
}  elseif (true) {
	echo "middle test";
} else {
	echo "nested if";
}


// If else
if (true){	
	echo "True";
} else {
	echo "one level data";
}

if ($event === true):
	echo "even is {$event})";
endif;

Loop statements with correct brace style

php
<?php

foreach ($array as $key => $value) {
	echo "True";
}

for( $i = 0; $i < 10; $i++) {
	echo "True";
}

do {
	echo "The number is: $x <br>";
	$x++;
}   while ($x <= 5);

Try catch statements with correct brace style

php
<?php

function inverse($x)
{
	if (!$x) {
		throw new Exception('Division by zero.');
	}
	return 1/$x;
}

function connect(
	$server,
	$username,
	$password,
	$port,
	$database,
	$socket = array()
) {
	// Connect
}

function login(
	$username,
	$password,
) :boolean {
	// Connect
}

Function declarations with correct brace style

php
<?php

try {
	echo inverse(5) . "\n";
}  catch (Exception $e) {
	echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
	echo "First finally.\n";
}

try {
	echo inverse(0) . "\n";
} catch (Exception $e) {
	echo 'Caught exception: ',  $e->getMessage(), "\n";
}


try {
	echo inverse(0) . "\n";
} catch (ExceptionType1 $e) 	{
	echo inverse(5) . "\n";
} catch (ExceptionType2 $e) {
	error_log($e->getMessage());
} finally {
echo "Final Step";
}

Switch statement and match expressions with correct brace style

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];
                } else if ($else) {
                    switch ($else) {
                        default:
                    }
            }
        }
    break;
}

switch ($name) {
    case "1":
    case "2":
    case "3":
    return true;
}

switch ($name) {
    case "1":
    case "2":
    case "3":
    default:
    return true;
}

switch ($foo) {
    case 1:
    switch ($bar) {
            default:
            if ($something) {
                    echo $string[1];
                } else if ($else) {
                    switch ($else) {
                        case 1:
                            // Do something.
                        break;
                        default:
                            // Do something.
                        break;
                    }
            }
        }
    break;
    case 2:
        // Do something;
    break;
}

switch ($http_response_code) {
    case 100:
    case 101:
    case 102:
    exit;
    default:
    exit;
}

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

Control structure with incorrect brace style

php
<?php
function inverse($x) {
	if (!$x) {
		throw new Exception('Division by zero.');
	}
	return 1/$x;
}

function connect(
	$server,
	$username,
	$password,
	$port,
	$database,
	$socket ) {
	// Connect
}

if (true) {
	echo "True"; }

// if elseif else
if (true) 
{
echo "True";} 
 elseif (false) {
	echo "False";
}  elseif
 (true) {	echo "middle test";
} else {
	echo "nested if"; }

try 
{
	echo inverse(0) . "\n";
} 
catch (ExceptionType1 $e) {
	echo inverse(5) . "\n";
} catch (ExceptionType2 $e) {
	error_log($e->getMessage());
}
finally {
echo "Final Step";
}

do {
	echo "The number is: $x <br>";
	$x++;
}
while (
	$x <= 5);

Switch statement and match expressions with incorrect brace style

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 ($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($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")};

style

  • Examples of correct code for this rule using style option

Class and methods with correct brace style

php
<?php
/* taqwim psr/brace-style: {style: "1tbs"} */

class Testing {
	public function test() {
		if (true) {
			echo "Hello World";
		} else {
			echo "Hello World";
		}
	}
	
	private function multipleParameters($parameter1, $parameter2, $parameter3) {
		echo "Hello World";
	}
	
	private function multipleParamsOverMultipleLines(
		$parameter1,
		$parameter2,
		$parameter3
	) {
		echo "Hello World";
	}
}
  • Examples of incorrect code for this rule using style option

Class and methods with incorrect brace style

php
<?php
/* taqwim psr/brace-style: {style: "1tbs"} */

class Testing
{
	public function test()
	{
		if (true) {
			echo "Hello World";
		} else {
			echo "Hello World";
		}
	}
	
	private function multipleParameters($parameter1, $parameter2, $parameter3)
	{
		echo "Hello World";
	}
	
	private function multipleParamsOverMultipleLines(
		$parameter1,
		$parameter2,
		$parameter3
	)
	{
		echo "Hello World";
	}
	
	private function multipleParamsOverMultipleLinesWithType(
		$parameter1,
		$parameter2,
		$parameter3
	) :boolean
	{
		echo "Hello World";
	}
}

Released under the MIT License.