Skip to content

spacing.pairfixable

Ensure that pair operator => have consistent spacing

Options

align

Align siblings items

Type: boolean

Default: false

Examples

  • Examples of correct code for this rule using default options

Arrays with correct pair operator spacing

php
<?php
$var = array(1, 2, 3);
$var = array(
    1,
    2,
    3,
);

$var = array(
    "first_element" => "bar",
    2,
    "second" => "qux",
    3,
    "last_element_of_the_array",
);

$var = array(1 => 'one', 2 => 'two', 3 => 'three');

$var = array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
);

$var = array(
    1 => 'one',
    2 => 'two',
    /* three */ 3 => 'three',
);

$fruit = array('apple' => array(
        'green',
        'red',
    ),
    'orange' => array(
        'orange',
    ),
    'banana' => array(
        'yellow',
    ),
);


$countries = array(
    'Japan' => array(
        'population' => 125961625,
        'capital' => 'Tokyo',
        'details' => array(
            'languages' => array('Japanese'),
            'original_name' => '日本',
        ),
    ),
    'India' => array(
        'population' => 1393409038,
        'capital' => 'New Delhi',
        'details' => array(
            'languages' => array('Hindi', 'English'),
            'original_name' => 'भारत',
        ),
    ),
    'South Korea' => array(
        'population' => 51780579,
        'capital' => 'Seoul',
        'details' => array(
            'languages' => array('Korean'),
            'original_name' => '대한민국',
        ),
    ),
    'Thailand' => array(
        'population' => 69428524,
        'capital' => 'Bangkok',
        'details' => array(
            'languages' => array('Thai'),
            'original_name' => 'ประเทศไทย',
        ),
    ),
);

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',
    ),
);

Lists with correct pair operator spacing

php
<?php

list("a" => $a) = $b;

list(
    "first" => $a,
    "b" => $b,
"third_variable" => $c    )
= $d;

list($drink, $color, $power) = $info;
list($drink, , $power) = $info;

list($a, list(2 => $two,
3 => $three)) = array(1, array(2, 3));

list(1 => $second, 3 => $fourth) = array(1, 2, 3, 4);

// Multiline and nested list
list(
    $a,
    list("php" => $first_language,
        "javascript" => $second_language,
    )
) = array(1, array("php", "python", "javascript", "c++"));

Match with correct pair operator spacing

php
<?php

match($version) {'php' => 10, 'typescript' => '6', 'python' => 3.9};

match ($food) {
    'apple_juice' => 'This food is an apple',
    'orange_fruit' => 'This food is a orange',
    'cake' => 'This food is a cake',
};

match($month_name) {
    'january' => 31,
    'february' => is_leap_year($year) ? 29 : 28,
    'march' => 31,
    'april' => 30,
    'may' => 31,
    'june' => 30,
    'july' => 31,
    'august' => 31,
    'septemper' => 30,
    'october' => 31,
    'november' => 30,
    'december' => 31,
    default => throw new InvalidArgumentException("Invalid month"),
};

$result = match ($x) {
        $a, $b, $c => 5,
        $a => 5,
        $b => 5, $c => 5,
        $a,
        $b, $c => 5,
    };

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

$condition = 5;
try {
    match ($condition) {
        "first", "second" => foo(),
        3, 4 => bar(),
    };
} catch (\UnhandledMatchError $e) {
    var_dump($e);
}
  • Examples of incorrect code for this rule using default options

Arrays with incorrect pair operator spacing

php
<?php
$var = array(1, 2, 3);
$var = array(
    1,
    2,
    3,
);

$var = array(
    "first_element"=>"bar",
    2,
    "second"=>   "qux",
    3,
    "last_element_of_the_array",
);

$var = array(1=> 'one', 2=>'two', 3 =>'three');

$var = array(
    1 =>  'one',
    2=> 'two',
    3=>'three',
);

$var = array(
    1=> 'one',
    2 =>'two',
    /* three */ 3=> 'three',
);

$fruit = array('apple'=>array(
        'green',
        'red',
    ),
    'orange'  => array(
        'orange',
    ),
    'banana'=> array(
        'yellow',
    ),
);


$countries = array(
    'Japan'=>  array(
        'population' => 125961625,
        'capital'=>  'Tokyo',
        'details' => array(
            'languages' => array('Japanese'),
            'original_name' => '日本',
        ),
    ),
    'India'=>array(
        'population'=> 1393409038,
        'capital'=>  'New Delhi',
        'details'=>  array(
            'languages' => array('Hindi', 'English'),
            'original_name' => 'भारत',
        ),
    ),
    'South Korea'=> array(
        'population'  => 51780579,
        'capital' 
        => 'Seoul',
        'details'=> array(
            'languages' => array('Korean'),
            'original_name' => '대한민국',
        ),
    ),
    'Thailand' =>array(
        'population' => 69428524,
        'capital' => 'Bangkok',
        'details'=>array(
            'languages' => array('Thai'),
            'original_name' => 'ประเทศไทย',
        ),
    ),
);

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',
    ),
);

Lists with incorrect pair operator spacing

php
<?php

list("a"=>$a) = $b;

list(
    "first"=> $a,
    "b" =>    $b,
"third_variable" => $c    )
= $d;


list($a, list(2=> $two,
3 =>   $three)) = array(1, array(2, 3));

list(1=>$second, 3   =>$fourth) = array(1, 2, 3, 4);

list(
    $a,
    list("php"=>$first_language,
        "javascript"=>  $second_language,
    )
) = array(1, array("php", "python", "javascript", "c++"));

Match statements with incorrect pair operator spacing

php
<?php

match($version) {'php'=>10, 'typescript'=> '6', 'python'=> 3.9};

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

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

$expressionResult = 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);
}

align

  • Examples of correct code for this rule using align option

Arrays with correct pair operator spacing

php
<?php
/* taqwim "psr/spacing.pair": {align: true} */
$var = array(1, 2, 3);
$var = array(
    1,
    2,
    3,
);

$var = array(
    "first_element" => "bar",
    2,
    "second"        => "qux",
    3,
    "last_element_of_the_array",
);

$var = array(1 => 'one', 2 => 'two', 3 => 'three');

$var = array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
);

$var = array(
    1 => 'one',
    2 => 'two',
    /* three */ 3 => 'three',
);

$fruit = array('apple'  => array(
        'green',
        'red',
    ),
    'orange' => array(
        'orange',
    ),
    'banana' => array(
        'yellow',
    ),
);


$countries = array(
    'Japan'       => array(
        'population' => 125961625,
        'capital'    => 'Tokyo',
        'details'    => array(
            'languages'     => array('Japanese'),
            'original_name' => '日本',
        ),
    ),
    'India'       => array(
        'population' => 1393409038,
        'capital'    => 'New Delhi',
        'details'    => array(
            'languages'     => array('Hindi', 'English'),
            'original_name' => 'भारत',
        ),
    ),
    'South Korea' => array(
        'population' => 51780579,
        'capital'    => 'Seoul',
        'details'    => array(
            'languages'     => array('Korean'),
            'original_name' => '대한민국',
        ),
    ),
    'Thailand'    => array(
        'population' => 69428524,
        'capital'    => 'Bangkok',
        'details'    => array(
            'languages'     => array('Thai'),
            'original_name' => 'ประเทศไทย',
        ),
    ),
);

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',
    ),
);

Lists with correct pair operator spacing

php
<?php
/* taqwim "psr/spacing.pair": {align: true} */
list("a" => $a) = $b;

list(
    "first"          => $a,
    "b"              => $b,
"third_variable" => $c    )
= $d;


list($a, list(2 => $two,
3 => $three)) = array(1, array(2, 3));

list(1 => $second, 3 => $fourth) = array(1, 2, 3, 4);

list(
    $a,
    list("php"        => $first_language,
        "javascript" => $second_language,
    )
) = array(1, array("php", "python", "javascript", "c++"));

Match with correct pair operator spacing

php
<?php
/* taqwim "psr/spacing.pair": {align: true} */
match($version) {'php' => 10, 'typescript' => '6', 'python' => 3.9};

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

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

$expressionResult = 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);
}
  • Examples of incorrect code for this rule using align option

Arrays with incorrect pair operator spacing

php
<?php
/* taqwim "psr/spacing.pair": {align: true} */
$var = array(1, 2, 3);
$var = array(
    1,
    2,
    3,
);

$var = array(
    "first_element" => "bar",
    2,
    "second" => "qux",
    3,
    "last_element_of_the_array",
);

$var = array(1 => 'one', 2 => 'two', 3 => 'three');

$var = array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
);

$var = array(
    1 => 'one',
    2 => 'two',
    /* three */ 3 => 'three',
);

$fruit = array('apple' => array(
        'green',
        'red',
    ),
    'orange' => array(
        'orange',
    ),
    'banana' => array(
        'yellow',
    ),
);


$countries = array(
    'Japan' => array(
        'population' => 125961625,
        'capital' => 'Tokyo',
        'details' => array(
            'languages' => array('Japanese'),
            'original_name' => '日本',
        ),
    ),
    'India' => array(
        'population' => 1393409038,
        'capital' => 'New Delhi',
        'details' => array(
            'languages' => array('Hindi', 'English'),
            'original_name' => 'भारत',
        ),
    ),
    'South Korea' => array(
        'population' => 51780579,
        'capital' => 'Seoul',
        'details' => array(
            'languages' => array('Korean'),
            'original_name' => '대한민국',
        ),
    ),
    'Thailand' => array(
        'population' => 69428524,
        'capital' => 'Bangkok',
        'details' => array(
            'languages' => array('Thai'),
            'original_name' => 'ประเทศไทย',
        ),
    ),
);

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',
    ),
);

Lists with incorrect pair operator spacing

php
<?php
/* taqwim "psr/spacing.pair": {align: true} */
list("a" => $a) = $b;

list(
    "first" => $a,
    "b" => $b,
"third_variable" => $c    )
= $d;

list($drink, $color, $power) = $info;
list($drink, , $power) = $info;

list($a, list(2 => $two,
3 => $three)) = array(1, array(2, 3));

list(1 => $second, 3 => $fourth) = array(1, 2, 3, 4);

list(
    $a,
    list("php" => $first_language,
        "javascript" => $second_language,
    )
) = array(1, array("php", "python", "javascript", "c++"));

Match statements with incorrect pair operator spacing

php
<?php
/* taqwim "psr/spacing.pair": {align: true} */
match($version) {'php' => 10, 'typescript' => '6', 'python' => 3.9};

match ($food) {
    'apple_juice' => 'This food is an apple',
    'orange_fruit' => 'This food is a orange',
    'cake' => 'This food is a cake',
};

match($month_name) {
    'january' => 31,
    'february' => is_leap_year($year) ? 29 : 28,
    'march' => 31,
    'april' => 30,
    'may' => 31,
    'june' => 30,
    'july' => 31,
    'august' => 31,
    'septemper' => 30,
    'october' => 31,
    'november' => 30,
    'december' => 31,
    default => throw new InvalidArgumentException("Invalid month"),
};

$result = match ($x) {
        $a, $b, $c => 5,
        $a => 5,
        $b => 5, $c => 5,
        $a,
        $b, $c => 5,
    };

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

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

Released under the MIT License.