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

Allow array unpacking in ArrayDeclaration multiline Sniff #3843

Closed edorian closed 1 year ago

edorian commented 1 year ago

See #3557

Description

Squiz.Arrays.ArrayDeclaration.NoKeySpecified doesn't accept the following snippet:

$x = [
  'foo' => 'bar',
  ...$baz,
];

It triggers an error as phpcs doesn't detect that the second entry is array unpacking.

Key specified for array entry; first entry has no key (Squiz.Arrays.ArrayDeclaration.KeySpecified)

The following (single line) array validates:

$x = ['foo' => 'bar',  ...$baz];

Suggested changelog entry

Squiz.Arrays.ArrayDeclaration.KeySpecified: Allow array unpacking (...$array) in multi-line key-value array definitions.

Related issues/external references

Fixes #3557

Types of changes

PR checklist

Notes:

The one test failure for PHP 8.2 seems unrelated as it was already present in https://github.com/squizlabs/PHP_CodeSniffer/commit/2e59060b93b3720ff3aab2b57efda0c3ada069b0

jrfnl commented 1 year ago

I did not. I'd be happy to, if you give me a pointer on how I can do this without breaking earlier PHP versions, given it's new syntax.

@edorian Please do add some new tests. The fact that it is new syntax is irrelevant as the PHPCS tokenizer has backported the T_ELLIPSIS token to PHP 5.4, so the test code should tokenize the same in all PHP versions.

(if that is not the case, we have another issue, but better to know about it than not)

edorian commented 1 year ago

Thanks @jrfnl

Without my fix:

PHP_CodeSniffer\Standards\Squiz\Tests\Arrays\ArrayDeclarationUnitTest::testSniff
[LINE 475] Expected 0 error(s) in ArrayDeclarationUnitTest.2.inc but found 1 error(s). The error(s) found were:
 -> No key specified for array entry; first entry specifies key (Squiz.Arrays.ArrayDeclaration.NoKeySpecified)

With my fix: :heavy_check_mark:

edorian commented 1 year ago

@jrfnl I've implemented your suggestions, fixed the KeySpecified error and added test cases.

I've done so by basically saying "if it's array unpacking line don't decide if it's a 'list' or a 'dict' shaped array".

Doesn't look very clean looking at the diff but it passes all tests I could come up with :)

edorian commented 1 year ago

Added your suggested tests and tried to align the two test files, but the diff of that grew to big for this PR.

Happy to do that in a follow up when this is merged :)

@jrfnl Thank you for the fast response an the clear directions!

jrfnl commented 1 year ago

@edorian Uh oh.. the tests are failing now. Could it be that the test case comment was only applied to the one test case file, not to all four ?

edorian commented 1 year ago

@jrfnl Sorry my bad. Didn't commit/push all the changes. Tests are passing again (failing test on master excluded) and I've applied your copy suggestion everywhere

jrfnl commented 8 months ago

FYI: this fix 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).