WordPress / WordPress/WordPress-Coding-Standards
Add rule for list() associative array destructure for multiline formatting
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
Is your feature request related to a problem?
PHP 7.1.0 introduces associative array destructuring using list() construct.
It would be a nice addition to format the associative destructure inside list() with the same rule as arrays.
Currently, list() is not broken into multi line regardless of the line max limit.
Describe the solution you'd like
list() could be formatted like so:
list(
'show' => $show,
'position' => $position,
'align' => $align
) = $array;
- Each pair on a single line
- Opening brackets same line as list construct
- Closing bracket, equal operator and variable on the same line
- Possibly a trailing comma for the last pair
- This rule should not apply for a single line destructure (if it is less than line limit)
Additional context
list() can be written using shorthand syntax:
[
'show' => $show,
'position' => $position,
'align' => $align
] = $array;
But this is not accepted using PHPCS rule 'Generic.Arrays.DisallowShortArraySyntax.Found' and they square brackets are converted to array() which is not a shorthand list() syntax.
This can be "fixed" by letting PHPCS format the code with array() then convert it to square brackets and add this to ignore the rule:
//phpcs:ignore Generic.Arrays.DisallowShortArraySyntax.Found
[
'show' => $show,
'position' => $position,
'align' => $align
] = $array;
But this not ideal if there are many instances of it.
list() also accept multidimensional destructuring.
list(
'show' => $show,
'align' => $align
'position' => [
'top' => $top,
'bottom' => $bottom,
]
) = $array;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the PHP_CodeSniffer array-formatting rule and the handling of PHP list() constructs. Compare the requested single-line and multiline examples, including nested destructuring, and define tests for line limits, pair-per-line formatting, closing syntax, and optional trailing commas.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100