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

simplified_if_return breaks code in braceless for loop #6230

Closed
rodrigoq opened this issue Jan 10, 2022 · 1 comment · Fixed by #6266
Closed

simplified_if_return breaks code in braceless for loop #6230

rodrigoq opened this issue Jan 10, 2022 · 1 comment · Fixed by #6266
Labels

Comments

@rodrigoq
Copy link

Bug report

  • PHP version: php -v
PHP 7.4.16 (cli) (built: Mar  2 2021 14:06:13) ( NTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
 with Xdebug v3.0.3, Copyright (c) 2002-2021, by Derick Rethans
  • PHP CS Fixer version: php-cs-fixer -V
PHP CS Fixer 3.4.0 Si! by Fabien Potencier and Dariusz Ruminski
  • the command used to run PHP CS Fixer (run with -vvv)
php-cs-fixer fix -vvv --rules=simplified_if_return file.php
PHP CS Fixer 3.4.0 Si! by Fabien Potencier and Dariusz Ruminski
Runtime: PHP 7.4.16
Loaded config default.
Using cache file ".php-cs-fixer.cache".
F                                                                                                           1 / 1 (100%)
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
   1) file.php (simplified_if_return)

Fixed all files in 0.047 seconds, 12.000 MB memory used

Code snippet that reproduces the problem

Before running PHP CS Fixer

<?php

function EqualsAny(string $str, array $letters) : bool
{
   foreach ($letters as $letter)
      if ($str === $letter)
         return true;
   return false;
}

$str = 'b';
$letters = ['a', 'b'];
var_dump(EqualsAny($str, $letters)); //Expected bool(true), Returns bool(true)

$letters = [];
var_dump(EqualsAny($str, $letters)); //Expected bool(false), Returns bool(false)

After running PHP CS Fixer

<?php

function EqualsAny(string $str, array $letters) : bool
{
   foreach ($letters as $letter)
      return (bool) ($str === $letter)

     ;
}

$str = 'b';
$letters = ['a', 'b'];
var_dump(EqualsAny($str, $letters)); //Expected bool(true), Returns bool(false)

$letters = [];
var_dump(EqualsAny($str, $letters)); //Expected bool(false), Returns throws exception!!

Expected output

Leave the code unchanged.

This is a braces problem, if the code has braces (or if you run --rules=braces,simplified_if_return) the code doesn't change and it's ok.

@SpacePossum
Copy link
Contributor

thanks for reporting @rodrigoq , proposed fix is here #6266

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

Successfully merging a pull request may close this issue.

2 participants