per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

Array Declaration must be short array syntax

Open mbniebergall opened this issue 2 years ago • 4 comments

Currently all of the PSR/PER code examples show using PHP short array syntax $thing = [];. PRs (ex: https://github.com/php-fig/per-coding-style/pull/18) also use short array syntax $arr = ['a' => 'A', 'b' => 'B', 'c' => 'C'];. There is no rule though requiring short array syntax.

The short array syntax was adopted for good reasons. According to the RFC:

  • Good for framework development when dealing with long parameterlists
  • Other web languages have similar syntax
  • Readable

Scouring Github repositories, PHP short array syntax is very prevalent in popular PHP frameworks. Using the old syntax $thing = array(...); is relatively rare. Most occurrences are in old repositories or in conversion from old to short syntax tools.

Would creating a PR for adding a MUST use short array syntax section be welcomed?

mbniebergall avatar Sep 13 '22 14:09 mbniebergall

I think that makes sense.

samdark avatar Sep 13 '22 20:09 samdark

PR opened for this https://github.com/php-fig/per-coding-style/pull/47

mbniebergall avatar Sep 16 '22 14:09 mbniebergall

After mulling over it and talking with others about arrays and their ambiguity within the coding styles, I created an alternative pull request https://github.com/php-fig/per-coding-style/pull/48 that is an either-or with https://github.com/php-fig/per-coding-style/pull/47.

PR 47 is smaller scoped and leaves some aspects not clarified. PR 48 clarifies the uses and declarations for arrays.

mbniebergall avatar Sep 16 '22 21:09 mbniebergall

#48 is better. For arrays need several rules and use separate section it's OK for me.

vjik avatar Sep 17 '22 09:09 vjik

#48 has merged

KorvinSzanto avatar Nov 11 '22 17:11 KorvinSzanto