-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Generic InlineControlStructureSniff can create parse error when case/if/elseif/else have mixed brace and braceless definitions #879
Comments
Yes, there seem to be a syntax error. Maybe code you're testing (that file specifically) is loaded via autoloader somehow? If that is, then it's 2nd occasion, when including Composer auto-loader inside CLI.php of PHP_CodeSniffer has backfired. |
I think the report is actually saying that PHPCBF fixed the file incorrectly. This doesn't have anything to do with autoloading. |
In your code, the |
I figured out the problem, although it is the very definition of an edge case, and I have no idea how to solve it. With this code snippet: <?php
switch ($type) {
case 1: // line 3
if ($foo) {
return true;
} elseif ($baz) // line 6
return true; // line 7
else {
echo 'else';
}
break;
} The I can't find a more simple test case than this as it seems the combination of the mixed braces/non-braces and the use of a case statement is required. |
… error when case/if/elseif/else have mixed brace and braceless definitions
I've fixed this case without breaking any other tests by adding an exception for it in the tokenizer. Your code is now fixed like this: <?php
// test.php
function sth($data, $type)
{
switch ($type) {
case Format::NONE:
return true;
case Format::DATE:
case Format::TIME:
case Format::DATETIME:
case Format::DATETIMEU:
if (empty($data)) {
return true;
} elseif (!is_object($data)) {
$datetime = date_create($data);
return ($datetime!==false);
} elseif ($data instanceof \DateTime) {
return true;
} else {
throw new FormatException('Unknown datetime object: ' . get_class($data));
}
break;
default:
throw new FormatException('Unknown format: ' . print_r($type, true));
}
} Thanks for reporting. |
👍 |
Hi,
I tried to clean the following function:
bin/phpcbf --standard=PSR2 test.php
This results in:
I did the same with
=> this does not create a parse error.
The text was updated successfully, but these errors were encountered: