squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Sniff Squiz.Arrays.ArrayDeclaration does not add trailing commas to the final index of nested subarrays #3852

Closed arwelcmm closed 1 year ago

arwelcmm commented 1 year ago

Describe the bug

phpcbf is not inserting trailing commas at the final index of nested arrays

Code sample

An example of code before a phpcbf run looks like this

<?php

return [
    'current' => [
        'single' => [
            'credits' => 1,
            'value'   => 8,
        ],
        'both'   => [
            'credits' => 1,
            'value'   => 8,
        ],
    ],
    'historical' => [
        '2023-02-27' => [
            'single'  => 6,
            'both'    => 6,
        ],
        '2020-01-01'    => [
            'single'  => 5,
            'both'    => 7.50,
        ]
    ]
];

And a run, it looks like this;

<?php

return [
    'current' => [
        'single' => [
            'credits' => 1,
            'value'   => 8,
        ],
        'both'   => [
            'credits' => 1,
            'value'   => 8,
        ],
    ],
    'historical' => [
        '2023-02-27' => [
            'single'  => 6,
            'both'    => 6,
        ],
        '2020-01-01'    => [
            'single'  => 5,
            'both'    => 7.50,
        ] // I would expect a trailing comma following this closing array brace
    ],
];

Custom ruleset

<?xml version="1.0" encoding="UTF-8"?>

<ruleset
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
>

    <!-- <arg name="basepath" value="."/> -->
    <!-- <arg name="cache" value=".phpcs-cache"/> -->
    <arg name="colors"/>
    <arg name="extensions" value="php"/>

    <rule ref="PSR12"/>

    <!-- Ignore the 120 character line limit -->
    <!-- NOTE: temporary hack to suppress some the noise -->
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="175"/>
            <property name="absoluteLineLimit" value="220"/>
        </properties>
    </rule>
    <rule ref="Squiz.Arrays.ArrayDeclaration">
        <exclude name="Squiz.Arrays.ArrayDeclaration.KeyNotAligned" />
        <exclude name="Squiz.Arrays.ArrayDeclaration.CloseBraceNotAligned" />
        <exclude name="Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed" />
        <exclude name="Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed" />
        <exclude name="Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned" />
        <exclude name="Squiz.Arrays.ArrayDeclaration.KeySpecified" />
    </rule>
    <rule ref="Squiz.Arrays.ArrayDeclaration.ValueNotAligned">
        <severity>1</severity>
    </rule>
    <file>config/</file>
    <file>src/</file>
    <file>tests/</file>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...

test.php

<?php

return [
    'current' => [
        'single' => [
            'credits' => 1,
            'value' => 8
        ]
    ]
];
  1. Run phpcs test.php ...
  2. See error message displayed
    
    ╰─ ./vendor/bin/phpcs test.php

FILE: /home/arwel/cmm/sites/cmm/public_html/api/test.php

FOUND 2 ERRORS AFFECTING 2 LINES

4 | ERROR | [x] Each line in an array declaration must end in a comma 5 | ERROR | [x] Each line in an array declaration must end in a comma

PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY


## Expected behavior
I expected the final index of the nested array to be appended with a trailing comma, I instead received;
```php
<?php

return [
    'current' => [
        'single' => [
            'credits' => 1,
            'value' => 8 // This index should have a trailing comma
        ],
    ],
];

Versions (please complete the following information)

Operating System Ubuntu 22.04.2 LTS on Windows 10 x86_64
PHP version 7.3.33
PHP_CodeSniffer version 3.7.2
Standard PSR-12
Install type Composer

Additional context

Add any other context about the problem here.

Please confirm:

jrfnl commented 1 year ago

As the Squiz.Arrays.ArrayDeclaration sniff is quite unwieldy to adjust, you may want to consider using an external sniff instead.

The NormalizedArrays.Arrays.CommaAfterLast sniff from PHPCSExtra should be able to handle this correctly and is configurable, as in you can indicate via properties whether you want to enforce this for single-line and/or multi-line arrays.

arwelcmm commented 1 year ago

As the Squiz.Arrays.ArrayDeclaration sniff is quite unwieldy to adjust, you may want to consider using an external sniff instead.

The NormalizedArrays.Arrays.CommaAfterLast sniff from PHPCSExtra should be able to handle this correctly and is configurable, as in you can indicate via properties whether you want to enforce this for single-line and/or multi-line arrays.

Thank you so much for all the help! This has been my first experience with reporting issues to a community so I was worried I'd get eaten alive - you and @fredden have been nothing but helpful. I'll poke around with this now, and hopefully will be able to close.

arwelcmm commented 1 year ago

Hi guys - all working as planned. Thank you to both @jrfnl and @fredden for all the help!