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

[Feature Request]: Enforce no spaces in array brackets #91

Closed
1 task done
antonioeatgoat opened this issue Apr 22, 2024 · 4 comments · Fixed by #96
Closed
1 task done

[Feature Request]: Enforce no spaces in array brackets #91

antonioeatgoat opened this issue Apr 22, 2024 · 4 comments · Fixed by #96
Assignees
Milestone

Comments

@antonioeatgoat
Copy link
Contributor

antonioeatgoat commented Apr 22, 2024

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

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Chrico
Copy link
Member

Chrico commented Jul 23, 2024

The SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace, sounds more consistent the me. 👍🏻

@tfrommen
Copy link
Member

tfrommen commented Jul 23, 2024

The Slevomat sniff is not about array access, but array declaration.

It will flag things like $a = [ 'space-before', 'no-space-after'], but not $a[ 'space-before-but-not-ater'].

We may want to use Squiz.Arrays.ArrayBracketSpacing, which is part of PHP_CodeSniffer.

@antonioeatgoat
Copy link
Contributor Author

@tfrommen you are correct about Slevomat. I was going to add it and then I realized the problem. I was trying Squiz.Arrays.ArrayBracketSpacing locally but I had some problem with it. If it works as expected, it is fine to add it for me.

@tfrommen tfrommen self-assigned this Jul 24, 2024
@tfrommen tfrommen linked a pull request Jul 25, 2024 that will close this issue
3 tasks
@tfrommen
Copy link
Member

Done in #96.

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