Skip to content
On this page

spacing.separationfixable

Ensure consistent instruction spacing (comma, semicolon, etc.)

Examples

  • Examples of correct code for this rule using default options

Arrays ans lists with correct separation spacing

php
<?php
$info = array(  'coffee', 'brown', 'caffeine');
list($drink, , , $power) = $info;
list( , , $tea) = $hot_drinks;
list($ice, 
 , $cold) = $other;

$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', 'another', 5, 'more'   )
	), 'banana' => array( 		'yellow' => 'yellow banana',
		'green' => 'green banana',
	), 'plum' => array   (
		'purple' => 'purple plum', 'green' => 'green plum',
	)
);

Functions and methods with correct separation spacing

php
<?php
define("FOO", "something");
define("FOO2", "something else");
define("FOO_BAR", "something more");

const CONSTANT = 'constant value', CONSTANT2 = 'another constant';
const CONSTANT1 = 'constant value', CONSTANT2 = 'another constant';


function test($a, $b, $c, $d) {
		return;
}

function test2($a, $b, $c, $d = 'default' ) {
  echo "test {$a}", "echo statement";
  echo "one more";
  return Testing;
}

class Test {
    const CONSTANT = 'constant value', CONSTANT2 = 'another constant';
    
	public function getData($a, $b, $c, $d) {
		echo "$a";
        echo $b;
        return $d;
	}
	
	public function getData2($a, $b, $c, $d = 'default') {
		echo $a, $c;
	}
}

class ClassName
{
    public function aVeryLongMethodName(
        ClassTypeHint $arg1, 
        &$arg2,
        array $arg3 = []
    ) {
        return $arg1->foo($arg2, $arg3);
    }
}

Foo::bar($arg1,
$arg2, $arg3, $arg4);

$foo->bar(
    $longArgument,
    $longerArgument,
    $muchLongerArgument
);

$foo->bar($arg1, "string {$arg2} string", $arg3)->
    bar($arg4)->moreArguments($arg5, $arg6, $arg7);
    
$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2, $var3) {
    return;
};

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

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

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

goto, label, continue, case, break and for statements with correct separation spacing

php
<?php
switch ($i) {
    case 0:
        echo "i equals 0"; 
        
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:        echo "i equals 2";
        break;
  default:
    echo "Default statement"; break;
}

switch($season){
  case 'spring':
}

foreach ($arr as $key => $value) {
    if (!($key % 2)) { // skip even members
        continue;
    }
    do_something_odd($value);
}

$i = 0;
while ($i++ < 5) {
    echo "Outer<br />";
    while (1) {
        echo "Middle<br />";
        while (1) {
            echo "Inner<br />";
            continue 3;
        }
        echo "This never gets output.<br />";
    }
    echo "Neither does this.<br />";
}

for($i=0, $j=50; $i<100; $i++) {
  while($j--) {
    if($j==17) goto end;} 
 $test = 'value';
}

for($i, $j; $i>50; $i++){
    echo $i;
}

for($i; $i>50; $i--){
    echo $i;
}

for(
    $i  = 0; $i>50; $i--):
    echo $i;
endfor;

for(;;$i++){
    echo $l;
}


goto a;
echo 'Foo';
 
a:
echo 'Bar';

echo "i = $i";
end:
echo 'j hit 17';

declare, namespace, yield, globals, traits, properties, exit, new and unset statements with correct separation spacing

php
<?php
declare(encoding='ISO-8859-1')    
;
declare(ticks=1);


namespace foo\bar\foo;
use My\Full\Classname as Another;
use My\Full\Classname1, My\Full\NSname as second;
use My\Full\Classname2;

namespace foo\bar\foo;

// exit program normally
exit;
exit();
exit(0);

//exit with an error code
exit(1);
exit(0376); //octal

unset($foo1, $foo2, $foo3);
unset($foo1,
$foo2, $foo3);

function gen_one_to_three() {
    for ($i = 1; $i <= 3; $i++) {
        yield $i;
    }
}

function Sum() {
    global $a, $b;
    $b = $a + $b;
}

class User {
    public int $id, $identifier;
    public ?string $name;
    
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
        B::bigTalk as talk;
    }
    
    use reflectionReturnInfo, anotherTrait;
    
    
    public function __construct(int $id, ?string $name) {
       
        $this->id = $id;
        $this->name = $name;
    }
}

new User(1, 'John Doe');
new User($id, new User(1, 'John Doe'));

include and assign statements with correct separation spacing

php
<?php
include "file4.php";
require "file5.php";
require_once dirname(__DIR__, 1) . 'file3.php';
include_once "file2.php";
require_once "file6.php";


$assign = 1;
$test = 5;
$string_var = "This is a test";

$multiline_string = "This is a test
of a multiline string
in PHP";


$new_string = $string_var;

Match arms with correct separation spacing

php
<?php
match ($food) {
    'apple' => 'This food is an apple', 'orange' => 'This food is a orange',
    'cake' => 'This food is a cake',
};

match($type){};

match($type){
    'apple' => 'This food is an apple',
};

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

$result = match ($x) {
        // This match arm:
        $a, $b,
        $c => 5,
        // Is equivalent to these three match arms:
        $a => 5,
        $b => 5, $c => 5,
    };

$expression_result = match ($condition) {   1, 2 => foo(), 3, 4 => bar(),
        default => baz(),
    };

$condition = 5;
try {
    match ($condition) {
        1, 2 => foo(), 3, 4 => bar(),  };
} catch (\UnhandledMatchError $e) {
    var_dump($e);
}

Enum cases with correct separation spacing

php
<?php

enum Months {
	case January;
	case February;
	case March;
	case April;
	case May;
	case June;
	case July;
	case August;
	case September;
	case October;
	case November;
	case December;
}

enum Seasons {
	case Spring;
	case Summer;
	case Autumn;
	case Winter;
}

enum Weekdays {
	case Monday;
	case Tuesday;
	case Wednesday;
	case Thursday;
	case Friday;
}
  • Examples of incorrect code for this rule using default options

Arrays ans lists with incorrect separation spacing

php
<?php
$info = array(  'coffee','brown'   ,     'caffeine');
list($drink,     ,,$power) = $info;
list(    ,    , $tea) = $hot_drinks;
list($ice, 
 , $cold) = $other;

$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', 'another', 5, 'more'   )
	),'banana' => array( 		'yellow' => 'yellow banana',
		'green' => 'green banana',
	),'plum' => array   (
		'purple' => 'purple plum','green' => 'green plum',
	)
);

Functions and methods with incorrect separation spacing

php
<?php
define("FOO", "something")  ;
define("FOO2", "something else") ;
define("FOO_BAR", "something more")  ;

const CONSTANT = 'constant value'   ,CONSTANT2 = 'another constant'   ;
const CONSTANT1 = 'constant value'
 ,    CONSTANT2 = 'another constant'    ;


function test($a   ,$b,   $c
 ,    $d) {
		return     ;
}

function test2($a   ,     $b , $c,     $d = 'default' ) {
  echo "test {$a}"  , "echo statement"   ;
  echo "one more"  ;
  return Testing ;
}

class Test {
    const CONSTANT = 'constant value'   ,CONSTANT2 = 'another constant'   ;
    
	public function getData($a , $b , $c,   $d) {
		echo "$a";
        echo $b ;
        return $d   ;
	}
	
	public function getData2($a,  $b  ,$c
	,   $d = 'default') {
		echo $a,$c  ;
	}
}

class ClassName
{
    public function aVeryLongMethodName(
        ClassTypeHint $arg1  ,
        &$arg2 ,
        array $arg3 = []
    ) {
        return $arg1->foo($arg2,$arg3)  ;
    }
}

Foo::bar($arg1  ,
$arg2,$arg3,   $arg4)  ;

$foo->bar(
    $longArgument  ,
    $longerArgument ,
    $muchLongerArgument
)  ;

$foo->bar($arg1,"string {$arg2} string",$arg3)->
    bar($arg4)->moreArguments($arg5   ,$arg6   ,  $arg7)   ;
    
$closureWithArgsAndVars = function ($arg1,$arg2) use ($var1 ,   $var2,$var3) {
    return    ;
}    ;

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

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

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

goto, label, continue, case, break and for statements with incorrect separation spacing

php
<?php
switch ($i) {
    case 0 :
        echo "i equals 0"; 
        
        break  ;
    case 1 :
        echo "i equals 1";
        break    ;
    case 2 :        echo "i equals 2";
        break;
  default :
    echo "Default statement"; break ;
}

foreach ($arr as $key => $value) {
    if (!($key % 2)) { // skip even members
        continue   ;
    }
    do_something_odd($value) ;
}

$i = 0;
while ($i++ < 5) {
    echo "Outer<br />" ;
    while (1) {
        echo "Middle<br />";
        while (1) {
            echo "Inner<br />" ;
            continue 3  ;
        }
        echo "This never gets output.<br />";
    }
    echo "Neither does this.<br />";
}

for($i=0,$j=50  ; $i<100   ; $i++) {
  while($j--) {
    if($j==17) goto end   ;
  }  
}

for($i,$j ;$i>50 ;$i++){
    echo $i;
}

for($i;$i>50;$i--){
    echo $i;
}

for(
    $i  = 0 ;
    $i>50;$i--):
    echo $i;
endfor;

for(;;$i++){
    echo $l;
}


goto a    ;
echo 'Foo'   ;
 
a   :
echo 'Bar';

echo "i = $i"   ;
end  :
echo 'j hit 17'   ;

declare, namespace, globals, traits, properties, yield, exit, new and unset statements with incorrect separation spacing

php
<?php
declare(encoding='ISO-8859-1')    
  ;
declare(ticks=1)   ;


namespace foo\bar\foo   ;
use My\Full\Classname as Another ;
use My\Full\Classname1  ,My\Full\NSname as second  ;
use My\Full\Classname2 ;

namespace foo\bar\foo    ;

// exit program normally
exit;
exit();
exit(0)    ;

//exit with an error code
exit(1);
exit(0376); //octal

unset($foo1,$foo2 , $foo3) ;
unset($foo1 ,
$foo2 ,$foo3) ;

function gen_one_to_three() {
    for ($i = 1; $i <= 3; $i++) {
        yield $i   ;
    }
}

function Sum() {
    global $a,$b ;
    $b = $a + $b;
}

class User {
    public int $id,$identifier  ;
    public ?string $name  ;
    
    use A ,B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
        B::bigTalk as talk;
    }
    
    use reflectionReturnInfo  , anotherTrait   ;
    
    
    public function __construct(int $id ,?string $name) {
       
        $this->id = $id ;
        $this->name = $name ;
    }
}

new User(1,'John Doe');
new User($id ,new User(1  ,'John Doe'));

include and assign statements with incorrect separation spacing

php
<?php
include "file4.php"  ;
require "file5.php"  ;
require_once dirname(__DIR__, 1) . 'file3.php' ;
include_once "file2.php" ;
require_once "file6.php" ;


$assign = 1 ;
$test = 5 ;
$string_var = "This is a test" ;

$multiline_string = "This is a test
of a multiline string
in PHP" ;


$new_string = $string_var ;

Match arms with incorrect separation spacing

php
<?php
match ($food) {
    'apple' => 'This food is an apple'  ,'orange' => 'This food is a orange'   ,
    'cake' => 'This food is a cake'   ,
};

match($type){};

match($type){
    'apple' => 'This food is an apple',
};

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

$result = match ($x) {
        // This match arm:
        $a,   $b   ,
        $c => 5 ,
        // Is equivalent to these three match arms:
        $a => 5 ,
        $b => 5,    $c => 5  ,
    };

$expression_result = match ($condition) {   1,2 => foo() ,3,4 => bar(),
        default => baz(),
    };

$condition = 5;
try {
    match ($condition) {
        1,2 => foo(),3,4 => bar(),  };
} catch (\UnhandledMatchError $e) {
    var_dump($e);
}

Enum cases with incorrect separation spacing

php
<?php

enum Months {
	case January ;
	case February  ; 	case March;
	case April  ;
	case May ;	case June  ;
	case July ;
	case August  ;
	case September  ;	case October  ; case November ;	case December  ;
}

enum Seasons {
	case Spring   ;	case Summer  ;
	case Autumn ;
	case Winter   ;
}

enum Weekdays {
	case Monday  ;
	case Tuesday    ;case Wednesday    ;
	case Thursday ;
	case Friday ;
}

Released under the MIT License.