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

SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses false positive producing invalid syntax #1672

Open
whitefang57 opened this issue Apr 9, 2024 · 0 comments

Comments

@whitefang57
Copy link

Using the following example:

<?php

declare(strict_types=1);

$foo = false;
$fn  = static fn () => ', World!';

echo 'Hello' . ($foo ? ' There' : $fn());
echo "\n";
echo 'Hello' . ($foo) ? ' There' : $fn();
echo "\n";
echo 'Hello' . (($foo) ? ' There' : $fn());
echo "\n";

Running phpcs using the Doctrine Coding Standard against this file produces the following:

$ phpcs --standard=Doctrine test.php

----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 10 | ERROR | [x] Useless parentheses.
 12 | ERROR | [x] Useless parentheses.
 12 | ERROR | [x] Useless parentheses.
----------------------------------------------------------------------

As you can see, the first block was not flagged as violating the standard while the second and third were.

phpcbf modifies the file in the following way:

<?php

declare(strict_types=1);

$foo = false;
$fn  = static fn () => ', World!';

echo 'Hello' . ($foo ? ' There' : $fn());
echo "\n";
echo 'Hello' . $foo ? ' There' : $fn();
echo "\n";
echo 'Hello' . $foo ? ' There' : $fn();
echo "\n";

Which does not make it syntactically invalid, but does drastically change the meaning of the code.

Before running phpcbf it outputs:

Hello, World!
 There
Hello, World!

After running phpcbf it outputs:

Hello, World!
 There
 There
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant