PHPCSStandards / PHP_CodeSniffer

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

Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned doesn't work inside match #475

Closed LastDragon-ru closed 3 months ago

LastDragon-ru commented 4 months ago

Describe the bug

Calculation of the align seems incorrect inside match.

Code sample

<?php declare(strict_types = 1);

// Ok
$a = [
    'a'  => 'a',
    'aa' => 'aa',
];

// Nope
$b = match (true) {
    true => [
        'a'  => 'a',
        'aa' => 'aa',
    ],
};

$c = match (true) {
    true => implode(', ', [
        'a'  => 'a',
        'aa' => 'aa',
    ]),
};

$c = match (true) {
    true => implode(
        ', ',
        [
            'a'  => 'a',
            'aa' => 'aa',
        ],
    ),
};

Custom ruleset

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor-bin/phpcs/vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <rule ref="Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned"/>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
  4. -------------------------------------------------------------------------------------------
    FOUND 6 ERRORS AFFECTING 6 LINES
    -------------------------------------------------------------------------------------------
    12 | ERROR | [x] Array double arrow not aligned correctly; expected 1 space but found 2
    13 | ERROR | [x] Array double arrow not aligned correctly; expected 4 spaces but found 13
    19 | ERROR | [x] Array double arrow not aligned correctly; expected 1 space but found 13
    20 | ERROR | [x] Array double arrow not aligned correctly; expected 1 space but found 13
    28 | ERROR | [x] Array double arrow not aligned correctly; expected 1 space but found 17
    29 | ERROR | [x] Array double arrow not aligned correctly; expected 1 space but found 17
    -------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------

Expected behavior

No error.

Versions (please complete the following information)

Operating System Windows 10
PHP version 8.3
PHP_CodeSniffer version 3.9.2
Standard custom
Install type composer

Please confirm

jrfnl commented 3 months ago

@LastDragon-ru I've just tested myself and it looks like PR #502 should also fix this issue. Testing appreciated.

LastDragon-ru commented 3 months ago

I've tested it my project and seems all good :) Thanks.

jrfnl commented 3 months ago

@LastDragon-ru Thanks for testing!