Skip to content

spacing.parenfixable

Ensure consistent parentheses spacing

This rule enures:

  • Single space after control structure keywords (if, for, foreach, while, do, switch, catch)
  • No space before the opening parenthesis of functions, methods, closures, calls and arrays
  • No space after the opening parenthesis
  • No space before the closing parenthesis
  • A single space after the closing parenthesis (except when followed by a semicolon)

Examples

  • Examples of correct code for this rule using default options

If and switch statements and typecasting with correct parentheses spacing

php
<?php
if ($expr1) {
    // if body
}elseif ($expr2) {
    // elseif body
} else {
    // else body;
}

if (         
    $expr1
    && $expr2
       ) {
    // if body
} elseif (
    $expr3
    && $expr4
    ) {
    // elseif body
}

switch ($expr) {
    case 0:
        echo 'First case, with a break';
        break;
    default:
        echo 'Default case';
        break;
}

$value = (array) new Test();
$count = (int) $string_data;
$sum = (float) $string_data;
$sum = (double) $string_data;

Loop statements with correct parentheses spacing

php
<?php

while ($expr) {
    // structure body
}

do {
    // structure body;
} while ($expr);

while ($expr) {
    // structure body
}

for ($i = 0; $i < 10; $i++) {
    // for body
}

foreach ($iterable as $key => $value) {
    // foreach body
}

foreach (
    $iterable as $key => $value) {
    // foreach body
}

do {
    // structure body;
} 
while ($expr);

do {
    // structure body;
} 

while
(
    $expr ||
    $expr2 ||
    $expr3
    );

Try/catch statements with correct parentheses spacing

php
<?php

try {
    // try body
} catch (FirstThrowableType $e) {
    // catch body
} catch (OtherThrowableType | AnotherThrowableType $e) {
    // catch body
} finally {
    // finally body
}

Classes, methods, functions and function calls with correct parentheses spacing

php
<?php
class ReturnTypeVariations
{
    public function functionName(int $arg1, $arg2): string
    {
        return 'foo';
    }


	public function secondFunction()
    {
        return 'foo';
    }

    public function anotherFunction(
        string $foo,
        string $bar,
        int $baz
    ): string {
        return 'foo';
    }

    public function getFoo(#[FooClassAttrib(20  ) ] $a): string
    {
    }
}

interface FooInterface {
    public function functionName(int $arg1, $arg2);
}

function testingSpace($Hello) {
	echo "HELLO";
}

$closureWithArgs = function($arg1, $arg2) {
    // body
};

$closureWithArgsAndVars = function($arg1, $arg2)
use ($var1, $var2) {
    // body
};

$closureWithArgsVarsAndReturn = function($arg1, $arg2) use
 ($var1, $var2): bool {
    // body
};


callFunction("HI", "HELLO");
$this->goAhead("one", "two");
$this->callFirst("one", "two")->callSecond("three", "four") ->callThird("five", "six");

uses()->beforeAll(function() {
    // ...
})->beforeEach(function() {
    // ...
})->afterEach(function() {
    // ...
})->afterAll(
    function() {
    // ...
});

$test = new TestClass();
$foo = new Foo($test);
$nested = new Foo(new Bar($test));
$new_class = new NewClass($test, $foo, $this->foo());

Multidimensional array with correct parentheses spacing

php
<?php
$foo = array(
	'apple' => array(
		'green' => 'green apple',
		'red' => 'red apple',
	),
	'orange' => array(
		'big' => 'big orange',
		'small' => 'small orange',
		'outher' => array(
			'yellow' => 'yellow orange',
			'green' => 'green orange',
		)  ,
		'more' => array('test')
	),
	'banana' => array(
		'yellow' => 'yellow banana',
		'green' => implode(',', $this->getData()),
	),
	'plum' => array(
		'purple' => 'purple plum',
		'green' => 'green plum',
	)
);

Arithmetic operations with correct parentheses spacing

php
<?php
$calc = (1 + 2) * 3;
$calc = (344 - 2) / 2;
$calc = (344 - 2) / (2 + 3);
$calc = (344 - 2) / (2 + 3) * 2;
$calc = 2 * (344 - 2) / (2 + 3);
$calc = (1 + 2)- 3;
$calc = (1 + 2)- (3 + 4);
  • Examples of incorrect code for this rule using default options

If and switch statements and typecasting with incorrect parentheses spacing

php
<?php

if    (     $expr1    )      {
    // if body
}elseif     (        $expr2) {
    // elseif body
} else {
    // else body;
}

if (
    $expr1
    && $expr2
       ) {
    // if body
} elseif (
    $expr3
    && $expr4
    ) {
    // elseif body
}

switch (    $expr  ) {
    case 0:
        echo 'First case, with a break';
        break;
    default:
        echo 'Default case';
        break;
}

if ( ! defined(  'ABSPATH' )  ){
	exit;
}

if (   isset( $args['name']) || isset($args['value'])   ) {
	throw new ErrorException('Remove name or value from args keys');
}

if( (   $test > 1  ) && (  $test < 10   ) ){
	echo 'The result is 9';
}

if ((   1 + 2 ) *   3 ) {
    echo 'The result is 9';
}


if (  isset(  $var  )  ){
    echo 'The result is 1';
}

if ( call_function( $id ) || ! in_array( call_another( $id ), array( 'first', 'second' ))) {
	return false;
}

$value = ( array )   new Test();
$count = ( int)$string_data;
$sum =( float  )$string_data;
$sum =(  double  )$string_data;

Loop statements with incorrect parentheses spacing

php
<?php
while (  $expr  ) {
    // structure body
}
do {
    // structure body;
} while (   $expr   );

while (   $expr ) {
    // structure body
}

for (    $i = 0; $i < 10; $i++   ) {
    // for body
}

foreach (   $iterable as $key => $value   )          {
    // foreach body
}

foreach (
    $iterable as $key => $value          ) {
    // foreach body
}

Try/catch statements with incorrect parentheses spacing

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
}

Classes, methods, functions and function calls with incorrect parentheses spacing

php
<?php
class ReturnTypeVariations
{
    public function functionName   (    int $arg1, $arg2  ) : string
    {
        return 'foo';
    }

    public function anotherFunction  (
        string $foo,
        string $bar,
        int $baz
    ) : string {
        return 'foo';
    }
    
    public function getFoo(#[FooClassAttrib(20  ) ] $a )  : string
    {
    }
}


function testingSpace   (   $Hello   ){
	echo "HELLO";
    echo '<h1>' . get_string('title') . '</h1>';
}

$closureWithArgs = function  (  $arg1, $arg2  ) {
    // body
};

$closureWithArgsAndVars = function (   $arg1, $arg2  ) 
use  (  $var1, $var2  ) {
    // body
};

$closureWithArgsVarsAndReturn = function  (   $arg1, $arg2) use ($var1, $var2   ) : bool {
    // body
};


callFunction  ( "HI", "HELLO"  );
$this->goAhead  ("one", "two");

callFunction(  );


uses( )->beforeAll( function () {
    // ...
} )->beforeEach( function () {
    // ...
} )->afterEach(function  () {
    // ...
}) ->afterAll(
    function () {
    // ...
});

$test = new TestClass( );
$foo = new Foo( $test );
$nested = new Foo( new Bar( $test ) );
$new_class = new NewClass( $test, $foo, $this->foo( ) );

Multidimensional array with incorrect parentheses spacing

php
<?php
$foo = array (
	'apple' => array ( 		'green' => 'green apple',
		'red' => 'red apple',
	),
	'orange' => array  (
		'big' => 'big orange',
		'small' => 'small orange',
		'outher' => array  (
			'yellow' => 'yellow orange',
			'green' => 'green orange',
		),
		'more' => array    (   'test'  )
	),
	'banana' => array( 		'yellow' => 'yellow banana',
		'green' => implode(',', $this->getData( )  ),
	),
	'plum' => array   (
		'purple' => 'purple plum',
		'green' => 'green plum',
	)
);

Arithmetic operations with incorrect parentheses spacing

php
<?php
$calc = (1 + 2 )   * 3;
$calc = (344 - 2 )/ 2;
$calc = (344 - 2  )/( 2 + 3 );
$calc =(344 - 2)/(  2 + 3 )* 2;
$calc = 2 *(344 - 2 )/ (  2 + 3  );
$calc = (1 + 2)- 3;
$calc = (1 + 2  )-(3 + 4 );

Released under the MIT License.