Skip to content

spacing.assignmentfixable

Ensure that assignment operator has consistent spacing

Options

align

Align assignment with adjacent statements

Type: boolean

Default: false

Examples

  • Examples of correct code for this rule using default options

Assignment statements

php
<?php
$var = $a + $b;
$var = $a / $b;
$var = $a - $b;
$var = $a * $b;
$var = $a % $b;
$var = $a ** $b;
$var = $a . $b;
$var = $a . $b . $c;
$var = $a / $b * $c + $d;
$var = $a + $b / $c * $d;

$var = $a + $b % $c ** $d - $e;

$var = $a + 
    $b % $c * $d - $e;

$var =
$a + $b % $c * $d - $e;

$var = ($b = 4) + 5;
$var -= $a;
$var += $a;
$var *= $a;
$var /= $a + $b;
$var %= $a ** $b;
$var **= $a;
$var .= $a;
$var &= $a;
$var |= $a;
$var ^= $a;
$var <<= $a;
$var >>= $a;
$var ??= $a;

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

for($i = 0,$j = 0; $i < 10; $i++,$j++) {
}

foreach ($files as $file) {
    $saves[$file] = array();
    $contents = stripslashes(file_get_contents($file));
    list($assetid, $time, $content) = explode("\n", $contents);
    $saves[$file]['assetid'] = $assetid;
    $var >>= $a;
    
    $var ??= $a;
}

if($test === true){
    $object->property = $a;
    $object->another_property = $b;
    $a = $object->call_function();
}


class Test{
	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'];
	}
}
$associative_array['foo'] = 'qux';
$associative_array['baz'] = 'bar';
$string_array[0] = 'qux';

$access_array = $associative_array['foo'];

$array[0] = $a;
$array[1]['property'] = $b;
$array[2]['another_property'] = $c;
$array[3]['another_property'] = $d;

$object->property = $a;
$object->another_property = $b;
$object->another_property = $c;
$object->another_property->nested_property = $d;

Assignment statements for arrays and objects

php
<?php
foreach ($files as $file) {
    $saves[$file] = array();
    $contents = stripslashes(file_get_contents($file));
    list($id, $time, $content) = explode("\n", $contents);
    $saves[$file]['id'] = $id;
    $var >>= $a;
    
    $var ??= $a;
}

if($test === true){
    $object->property = $a;
    $object->another_property = $b;
    $a = $object->call_function();
}

$array[0] = $a;
$array[1]['property'] = $b;
$array[2]['another_property'] = $c;
$array[3]['another_property'] = $d;

$object->property = $a;
$object->another_property = $b;
$object->another_property = $c;
$object->another_property->nested_property = $d;

function parameters, class properties and method parameters

php
<?php
class Test {
	private $test = 'test';
	public $test2 = 'test2';

	private function test($data = array(), $value = '', $third){
		echo 'test';
	}
}

function test($first, string $second = 'Testing', int $third) {
    echo 'Test';
}

function multiLine(
	$first,
	string $second = 'Testing',
	int $third
) {
	echo 'Test';
}

function multiDefaultParams(
	$first,
	string $second = 'Testing',
	int $third = 1
) {
	echo 'Test';
}
  • Examples of incorrect code for this rule using default options

Assignment statements

php
<?php
$var = $a +  $b;
$var = $a  /$b;
$var= $a-  $b;
$var =   $a *$b;
$var= $a%$b;
$var =  $a  **$b;
$var= $a.$b;
$var = $a.$b.  $c;
$var = $a/$b*$c + $d;
$var= $a +  $b/$c*$d;

$var= $a +$b%$c**$d- $e;

$var =  $a+ 
    $b %  $c*$d  -$e;

$var=
$a    +$b%$c*$d - $e;

$var =   ($b = 4) + 5;
$var  -=$a;
$var+=$a;
$var   *=$a;
$var /= $a + $b;
$var %=$a ** $b;
$var **= $a;
$var.= $a;
$var  &=$a;
$var  |= $a;
$var^=  $a;
$var<<=  $a;
$var>>=$a;
$var??=  $a;

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

for($i=  0,$j = 0; $i<10; $i++,$j++) {
}

Assignment statements for arrays and objects

php
<?php
foreach ($files as $file) {
    $saves[$file]= array();
    $contents =stripslashes(file_get_contents($file));
    list($id, $time, $content)   = explode("\n", $contents);
    $saves[$file]['id']= $id;
    $var>>=$a;
    
    $var??=   $a;
}

if($test === true){
    $object->property=$a;
    $object->another_property   =$b;
    $a=$object->call_function();
}


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  ]);
	}
}

$associative_array[ 'foo'] = 'qux';
$associative_array[ 'baz'] = 'bar';
$string_array[0] = 'qux';

$access_array = $associative_array  [  'foo'];

$array[0]=$a;
$array[1]['property']=  $b;
$array['string_key']['another_property']  =$d;

$object->property=  $a;
$object->another_property =$b;
$object->another_property->nested_property=$d;

function parameters, class properties and method parameters

php
<?php
class Test {
	private $test=  'test';
	public $test2   =   'test2';

	private function test($data=array(), $value   ='', $third){
		echo 'test';
	}
}

function test($first, string $second='Testing', int $third) {
    echo 'Test';
}

function multiLine(
	$first,
	string $second=    'Testing',
	int $third
) {
	echo 'Test';
}

function multiDefaultParams(
	$first,
	string $second='Testing',
	int $third    =1
) {
	echo 'Test';
}

align

  • Examples of correct code for this rule using align option

Aligned assignment statements

php
<?php
/* taqwim psr/spacing.assignment: {"align": true} */
$var1     = $a + $b;
$var11    = $a / $b;
$var24    = $a - $b;
$var89    = $a * $b;
$var8     = $a % $b;
$var12121 = $a / $b * $c + $d;
$var777   = $a ** $b;
$var568   = $a . $b;
$var7899 .= $a . $b . $c;


$var = $a + $b / $c * $d;

$var = $a + 
    $b % $c * $d - $e;
$stringVar = "hello world";
$var       =
    "Value on new line";

$varOne        = ($b = 4) + 5;
$varTwo       -= $a;
$varThreeFive += $a;
$longVarName **= $a;
$varFour      /= $a + $b;
$short        %= $a ** $b;
$foo         **= $a;
$var          .= $a;
$var          &= $a;

$var        *= $a;
$var0115    /= $a + $b;
$var00      %= $a ** $b;
$var4558   **= $a;
$var1478977 .= $a;
$var14789  **= $a;
$var33      &= $a;
$var778     |= $a;
$var778     ^= $a;
$var       <<= $a;
$var       >>= $a;
$var       ??= $a;

Aligned assignment statements for arrays and objects

php
<?php
/* taqwim psr/spacing.assignment: {"align": true} */
foreach ($files as $file) {
    $saves[$file]              = array();
    $contents                  = stripslashes(file_get_contents($file));
    list($id, $time, $content) = explode("\n", $contents);
    $saves[$file]['id']        = $id;
    $var                     >>= $a;
    
    $var ??= $a;
}

if($test === true){
    $object->property         = $a;
    $object->another_property = $b;
    $a                        = $object->call_function();
}

$array[0]                     = $a;
$array[1]['property']         = $b;
$array[2]['another_property'] = $c;
$array[3]['another_property'] = $d;

$object->property                          = $a;
$object->another_property                  = $b;
$object->another_property                  = $c;
$object->another_property->nested_property = $d;

$loggerResult = $util->setLogger(new class {
    public function log($msg)
    {
        $a      = $msg;
        $foobar = $msg;
        $foo    = function() {
            $a            = $msg;
            $foobar       = $msg;
            $loggerResult = $util->setLogger(new class {
                public function log($msg)
                {
                    $a      = $msg;
                    $foobar = $msg;
                    $foo    = function() {
                        foo(function() {
                            foo(function() {
                                echo 'hi';
                            });
                            $a      = $msg;
                            $foobar = $msg;

                            $foo = function() {
                                $foo    = 1;
                                $barbar = 2;
                            };
                            $barbar = function() {
                                $foo    = 1;
                                $barbar = 2;
                            };
                        });
                        $a      = $msg;
                        $foobar = $msg;
                    };
                    $bar = $msg;
                }

                public function log2($msg)
                {
                    $a      = $msg;
                    $foobar = $msg;
                    $foo    = function() {
                        foo(function() {
                            foo(function() {
                                echo 'hi';
                            });
                            $a      = $msg;
                            $foobar = $msg;

                            $foo = function() {

                                $foo    = 1;
                                $barbar = 2;
                            };
                            $barbar = function() {
                                $foo    = 1;
                                $barbar = 2;
                            };
                        });
                        $a      = $msg;
                        $foobar = $msg;
                    };
                    $bar = $msg;
                }
            });
            $foo = 5;
        };
        $bar = $msg;
    }
});

$with_parenthesis       = ($object?->property);
$test                   = $object?->property;
$without_nullsafe       = $object->property;
$with_multiple_nullsafe = $object?->call()?->property2;
  • Examples of incorrect code for this rule using align option

Aligned assignment statements

php
<?php
/* taqwim psr/spacing.assignment: {"align": true} */
$var1=$a + $b;
$var11= $a / $b;
$var24= $a - $b;
$var89 = $a * $b;
$var8= $a % $b;
$var777 = $a ** $b;
$var568=$a . $b;
$var7899 =$a . $b . $c;
$var12121= $a / $b * $c + $d;

$var= $a + $b / $c * $d;

$var   =$a + 
    $b % $c * $d - $e;
$stringVar="hello world";
$var=
    "Value on new line";

$varOne= ($b = 4) + 5;
$varTwo -=   $a;
$varThreeFive+= $a;
$longVarName*= $a;
$varFour /=$a + $b;
$short   %= $a ** $b;
$foo  **= $a;
$var.=   $a;
$var   &= $a;

$var*=$a;
$var0115/= $a + $b;
$var00%=   $a ** $b;
$var4558**=  $a;
$var1478977    .=$a;
$var33&=$a;
$var778|=$a;
$var778   ^=$a;
$var<<=$a;
$var    >>=$a;
$var??= $a;

Aligned assignment statements for arrays and objects

php
<?php
/* taqwim psr/spacing.assignment: {"align": true} */
foreach ($files as $file) {
    $saves[$file]= array();
    $contents = stripslashes(file_get_contents($file));
    list($id, $time, $content) = explode("\n", $contents);
    $saves[$file]['id'] = $id;
    $var  >>= $a;
    
    $var ??= $a;
}

if($test === true){
    $object->property=$a;
    $object->another_property = $b;
    $a = $object->call_function();
}

$loggerResult= $util->setLogger(new class {
    public function log($msg)
    {
        $a = $msg;
        $foobar= $msg;
        $foo=function() {
            $a= $msg;
            $foobar=   $msg;
            $loggerResult= $util->setLogger(new class {
                public function log($msg)
                {
                    $a=   $msg;
                    $foobar= $msg;
                    $foo=function() {
                        foo(function() {
                            foo(function() {
                                echo 'hi';
                            });
                            $a=   $msg;
                            $foobar= $msg;

                            $foo= function() {
                                $foo=1;
                                $barbar= 2;
                            };
                            $barbar= function() {
                                $foo=1;
                                $barbar= 2;
                            };
                        });
                        $a=   $msg;
                        $foobar= $msg;
                    };
                    $bar= $msg;
                }

                public function log2($msg)
                {
                    $a=   $msg;
                    $foobar= $msg;
                    $foo=function() {
                        foo(function() {
                            foo(function() {
                                echo 'hi';
                            });
                            $a=   $msg;
                            $foobar= $msg;

                            $foo= function() {

                                $foo=1;
                                $barbar= 2;
                            };
                            $barbar= function() {
                                $foo=1;
                                $barbar= 2;
                            };
                        });
                        $a=   $msg;
                        $foobar= $msg;
                    };
                    $bar= $msg;
                }
            });
            $foo= 5;
        };
        $bar= $msg;
    }
});

$with_parenthesis = ($object?->property);
$test = $object?->property;
$without_nullsafe = $object->property;
$with_multiple_nullsafe = $object?->call()?->property2;

Released under the MIT License.