php-coding-standards icon indicating copy to clipboard operation
php-coding-standards copied to clipboard

[Feature Request]: Enforce no spaces in array brackets

Open antonioeatgoat opened this issue 1 year ago • 0 comments
trafficstars

Is your feature request related to a problem?

At the current moment, we don't have any specific rule for spaces around keys in array square brackets.

This means that all the following lines are accepted by our rules:

  • $array['key'];
  • $array[ 'key' ];
  • $array[$key];
  • $array[ $key ];
  • $array = [ 'test', 'test2']
  • $array = [ 'test', 'test2' ]
  • $array = ['test', 'test2']

Describe the desired solution

No space should be enforced. Specifically, we could use the rule SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace, which allows to configure the number of accepted spaces.

Accepted cases would be:

  • $array['test'];
  • $array[123];
  • $array[$test];
  • $array[TEST];
  • $array['test' . $test];
  • $array = ['test', 'test2']

Describe the alternatives that you have considered

An alternative solution is using the rule WordPress.Arrays.ArrayKeySpacingRestrictions, from WordPress Coding Standard, it enforces no space around the key, except if the key is a variable, a constant or a concatenated string.

In this case, accepted cases would become:

  • $array['test'];
  • $array[123];
  • $array[ $test ];
  • $array[ TEST ];
  • $array[ 'test' . $test ];

Additional context

No response

Props

  • @widoz suggested it.

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

antonioeatgoat avatar Apr 22 '24 10:04 antonioeatgoat