Skip to content

array.comma-danglefixable

Ensure that trailing commas are consistent in arrays

Options

singleLine

Set the type of comma dangle in single-line arrays

Type: string

Default: never

Possible values: always, never

multiLine

Set the type of comma dangle in multi-line arrays

Type: string

Default: always

Possible values: always, never

Examples

  • Examples of correct code for this rule using default options

Arrays with incorrect comma dangling

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

$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' => 'ประเทศไทย',
        ),
    ),
);
  • Examples of incorrect code for this rule using default options

Arrays with incorrect comma dangling

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

$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' => 'ประเทศไทย'
        )
    ),
);

singleLine

  • Examples of correct code for this rule using singleLine option

Single line arrays with dangling comma

php
<?php
/* taqwim "taqwim/array.comma-dangle": {singleLine: "always"} */
$array = array(1,2,3,);
$array = array("one" => 1, "two" => 2, "three" => 3,);
$array = [1,2,3,];
$array = ["one" => 1, "two" => 2, "three" => 3,];

multiLine

  • Examples of correct code for this rule using multiLine option

Multi lines arrays with no dangling comma

php
<?php
/* taqwim taqwim/array.comma-dangle: {multiLine: "never"} */
$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' => 'ประเทศไทย'
        )
    )
);

Released under the MIT License.