PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Unpacking associative arrays in PHP 8.1 produces `Squiz.Arrays.ArrayDeclaration.NoKeySpecified` respectively `Squiz.Arrays.ArrayDeclaration.KeySpecified`

Open stlrnz opened this issue 2 years ago • 1 comments

Describe the bug Since PHP 8.1 unpacking of associative arrays is supported. Unpacking an associative array into another results in one of the errors Squiz.Arrays.ArrayDeclaration.NoKeySpecified or Squiz.Arrays.ArrayDeclaration.KeySpecified (depending on the position).

Code sample

<?php

$arr = [
        'a' => 1,
        'b' => 2,
       ];

$merged1 = [
            'c' => 3,
            ...$arr,
           ];

$merged2 = [
            ...$arr,
            'c' => 3,
           ];

Custom ruleset

<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
    <rule ref="Squiz.Arrays.ArrayDeclaration"/>
</ruleset>

To reproduce

-------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-------------------------------------------------------------------------------------------------------------------------
 10 | ERROR | No key specified for array entry; first entry specifies key
    |       | (Squiz.Arrays.ArrayDeclaration.NoKeySpecified)
 15 | ERROR | Key specified for array entry; first entry has no key (Squiz.Arrays.ArrayDeclaration.KeySpecified)
-------------------------------------------------------------------------------------------------------------------------

Expected behavior This should not result in an error:

  • ideally PHP_CodeSniffer can check if the unpacked array is associative
  • if thats not possible, array unpacking should be ignored by this rule

Versions:

  • OS: Windows 10
  • PHP: 8.1
  • PHPCS: 3.6.2
  • Standard: Squiz

stlrnz avatar Mar 10 '22 11:03 stlrnz

Related to #3479

jrfnl avatar Mar 10 '22 13:03 jrfnl