Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
stlrnz opened this issue Mar 10, 2022 · 3 comments · Fixed by #3843

Comments

@stlrnz
Copy link

stlrnz commented Mar 10, 2022

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
@jrfnl
Copy link
Contributor

jrfnl commented Mar 10, 2022

Related to #3479

@drtsb
Copy link

drtsb commented Feb 13, 2023

#3479 issue was closed, but there is still an error

edorian added a commit to edorian/PHP_CodeSniffer that referenced this issue Jun 20, 2023
jrfnl pushed a commit that referenced this issue Jul 11, 2023
Ignore array unpacking when determining whether an array is a list- or dict-shaped.

Includes tests.

Fixes #3557
jrfnl pushed a commit that referenced this issue Jul 11, 2023
Ignore array unpacking when determining whether an array is a list- or dict-shaped.

Includes tests.

Fixes #3557
@jrfnl jrfnl added this to the 3.8.0 milestone Jul 11, 2023
@jrfnl
Copy link
Contributor

jrfnl commented Dec 8, 2023

FYI: the fix for this issue is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants