squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Alignment incorrect (maybe?) for null coalesce assignment followed by normal assignment #3944

Closed JPry closed 6 months ago

JPry commented 6 months ago

Describe the bug

I have a series of 4 assignment statements. The first 3 statements are assigned using the null coalesce assignment (??=), while the 4th statement is assigned using normal assignment (=). When checking the file with wpcs, it generates a warning message. I believe this warning is not correct.

The "maybe" part of this issue is that I might have the wrong idea of what should be correct here.

Code sample

<?php

$a['latitude']  ??= '0';
$a['longitude'] ??= '0';
$a['periods']   ??= 2;
$a['periods']   = (int) $a['periods'];

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <description>If you are using a custom ruleset, please enter it here.</description>
  <rule ref="Generic.Formatting.MultipleStatementAlignment"/>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
    FILE: /path/to/test.php
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    6 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 5 spaces but found 3 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Expected behavior

I expected that this series of statements should not trigger a warning.

Versions (please complete the following information)

Operating System MacOS 14.3.1
PHP version 8.1
PHP_CodeSniffer version 3.7.2
Standard custom
Install type Composer local

Please confirm:

fredden commented 6 months ago

When I run phpcbf with this file and ruleset, the equal signs all line up. This is the same behaviour as with the following code snippet:

<?php
$zero   = 0;
$one   |= 1;
$two   &= 2;
$three += 3;
$four  -= 4;

See also the documentation for this sniff, which shows that the equals signs should line up: https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/3.9.0/src/Standards/Generic/Docs/Formatting/MultipleStatementAlignmentStandard.xml#L43-L48

jrfnl commented 6 months ago

@JPry Thanks for opening this issue, but I agree with @fredden that this is by design and that the sniff is working exactly as intended.

JPry commented 6 months ago

@fredden @jrfnl Thanks for the responses. I hadn't personally encountered this situation before so I wasn't sure that it was correct. Good to know it's working as intended.