Skip to content
On this page

spacing.bracketfixable

Ensure that square brackets [] have consistent spacing

Examples

  • Examples of correct code for this rule using default options

Arrays and offset lookup

php
<?php

$empty_array = [];
$array = [1, 2, 3];
$string_array = ['foo', 'bar', 'baz'];
$associative_array = ['foo' => 'bar', 'baz' => 'qux'];
$nested_array = ['foo' => ['bar' => 'baz']];
$array_of_arrays = ['single', ['foo' => 'bar'], 'middle', ['baz' => 'qux']];
$nested_array_of_arrays = ['foo' => ['bar' => ['baz' => 'qux']]];

$value_1 = $array[0];
$value_2 = $string_array[1];

class Test extends Testing {
	function construct($info)
	{
		$this->data['test']= $info['test'];
		$this->data['foo'] = $info['foo'];
		$this->data['bar'] = $info['bar'];
		$this->data['baz']['nested'] = $test;
		$this->data['baz']['nested']= $info['baz']['nested'];
		$this->data['baz']['nested'][] = $info['baz']['nested']['foo'];

		parent::__construct(['a' => 1, 'b' => 2]);
	}
}

// Correct because it is handled by `spacing.assignment` rule.
$foo['bar']= 'baz';
$foo['bar']  ='baz';

// Correct because it is handled by `spacing.accessor` rule.
$foo['bar'] ->baz();

Operators

php
<?php
$add = $array[0] + $array[1];
$subtract = $array[0] - $array[1];
$multiply = $array[0] * $array[1];
$divide = $array[0] / $array[1];
$modulus = $array[0] % $array[1];
$concatenate = $array[0] . $array[1];
$exponentiate = $array[0] ** $array[1];
$bitwise_and = $array[0] & $array[1];
$bitwise_or = $array[0] | $array[1];
$bitwise_xor = $array[0] ^ $array[1];
$bitwise_shift_left = $array[0] << $array[1];
$bitwise_shift_right = $array[0] >> $array[1];

$smaller = $array[0] < $array[1];
$smaller_or_equal = $array[0] <= $array[1];
$greater = $array[0] > $array[1];
$greater_or_equal = $array[0] >= $array[1];
$equal = $array[0] == $array[1];
$identical = $array[0] === $array[1];
$not_equal = $array[0] != $array[1];
$not_identical = $array[0] !== $array[1];


$or = $array[0] || $array[1];
$and = $array[0] && $array[1];
$ternary = $array[0] ? $array[1] : $array[2];
$null_coalesce = $array[0] ?? $array[1];
  • Examples of incorrect code for this rule using default options

Arrays and offset lookup

php
<?php

$empty_array = [    ];
$array =  [  1, 2, 3   ]   ;
$string_array =  [ 'foo', 'bar', 'baz'];
$associative_array =   [ 'foo' => 'bar', 'baz' => 'qux'];
$nested_array = [ 'foo' => [ 'bar' => 'baz'  ]];
$array_of_arrays = ['single', [ 'foo' => 'bar'], 'middle', [ 'baz' => 'qux']];
$nested_array_of_arrays = ['foo' => [ 'bar' => [ 'baz' => 'qux']]];
$nested_array_of_arrays = [  ['one' ],   [ 'two', 'three'  ], [  'four', 'five', 'six' ] ];

$value_1 = $array  [ 0  ];
$value_2 = $string_array[   1];

class Test extends Testing {
	function construct($info)
	{
		$this->data[ 'test'] = $info[ 'test'];
		$this->data ['foo'] = $info[  'foo'];
		$this->data ['bar' ] = $info[ 'bar'];
		$this->data  [ 'baz' ]  ['nested'] = $test;
		$this->data[ 'baz']['nested']= $info['baz']  ['nested'];
		$this->data   ['baz'  ][ 'nested'][ ] = $info  [  'baz'] ['nested'  ]   [ 'foo'];
		
		parent::__construct( [ 'a' => 1, 'b' => 2  ]);
	}
}

Operators

php
<?php
$add = $array[0]+$array[1];
$subtract = $array[0]  -$array[1];
$multiply = $array[0]*$array[1];
$divide = $array[0] /$array[1];
$modulus = $array[0]%  $array[1];
$concatenate = $array[0].$array[1];
$exponentiate = $array[0]   **$array[1];
$bitwise_and = $array[0]&$array[1];
$bitwise_or = $array[0]|$array[1];
$bitwise_xor = $array[0]   ^$array[1];
$bitwise_shift_left = $array[0]<<$array[1];
$bitwise_shift_right = $array[0]   >>$array[1];

$smaller = $array[0]<$array[1];
$smaller_or_equal = $array[0]<=$array[1];
$greater = $array[0]>$array[1];
$greater_or_equal = $array[0]   >=$array[1];
$equal = $array[0]==   $array[1];
$identical = $array[0]===   $array[1];
$not_equal = $array[0]!=$array[1];
$not_identical = $array[0]    !==    $array[1];


$or = $array[0]   ||$array[1];
$and = $array[0]&&   $array[1];
$ternary = $array[0]?$array[1]:   $array[2];
$null_coalesce = $array[0]??   $array[1];

Released under the MIT License.