PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Match Expressions alignments

Open jacobcassidy opened this issue 1 year ago • 6 comments

Should Match Expressions have alignment like those under the Generic.Formatting.MultipleStatementAlignment.NotSameWarning and WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAlign rules?

For example, this is not aligned:

$asset_dir = match ( $asset_type ) {
	'style' => 'build/base/css',
	'script' => 'build/base/js',
	'editor_ui_style' => 'build/blocks/css',
	'editor_ui_script' => 'build/blocks/js',
	default   => 'build/base'
};

The desired alignment:

$asset_dir = match ( $asset_type ) {
	'style'            => 'build/base/css',
	'script'           => 'build/base/js',
	'editor_ui_style'  => 'build/blocks/css',
	'editor_ui_script' => 'build/blocks/js',
	default            => 'build/base'
};

jacobcassidy avatar Aug 09 '24 04:08 jacobcassidy

@jacobcassidy Good question. AFAICS, the PER standard does not have rules about this. Might be an idea to open an issue in the PER repo ?

Independently of the outcome of such a discussion, I'd be open to accepting two different, but related sniffs for match arrow alignment:

  1. To align the arrows.
  2. To disallow alignment and enforce one space either side of the arrows.

Having both sniffs available would allow either style to be enforced, depending on the preference of the coding standard including the sniffs.

P.S.: Please use the appropriate issue template when opening an issue (and do not remove the template).

jrfnl avatar Aug 09 '24 05:08 jrfnl

@jrfnl Sounds good. I'll open an issue with the PER repo, and when I have extra time, I'll review this repo to see how to create the sniffs you mentioned.

P.S.: Please use the appropriate issue template when opening an issue (and do not remove the template).

Noted.

jacobcassidy avatar Aug 10 '24 01:08 jacobcassidy

Any updates on this? I'd like to enforce such alignment in array assignments, just like OPs example.

utdrmac avatar Oct 10 '25 22:10 utdrmac

@utdrmac Double arrow alignment in array assignments is a completely different topic. Have a look at the Squiz.Arrays.ArrayDeclaration sniff for that.

jrfnl avatar Oct 10 '25 22:10 jrfnl

@jrfnl That sniff is called out as "completely broken" https://github.com/PHPCSStandards/PHP_CodeSniffer/discussions/1084 @jacobcassidy did you ever find anything that worked?

utdrmac avatar Oct 11 '25 01:10 utdrmac

@utdrmac As I said before, questions regarding array arrow alignment do not belong in this ticket, which is about match arrow alignment.

jrfnl avatar Oct 11 '25 01:10 jrfnl