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

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

Closed
2 of 3 tasks
JPry opened this issue Mar 26, 2024 · 3 comments
Closed
2 of 3 tasks

Comments

@JPry
Copy link

JPry commented Mar 26, 2024

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:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@fredden
Copy link
Contributor

fredden commented Mar 26, 2024

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
Copy link
Contributor

jrfnl commented Mar 26, 2024

@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.

@jrfnl jrfnl closed this as not planned Won't fix, can't repro, duplicate, stale Mar 26, 2024
@JPry
Copy link
Author

JPry commented Mar 26, 2024

@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.

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

No branches or pull requests

3 participants