Closed
Description
Hello, I think my team found an issue with the SwitchDeclaration sniffer. This happens when we try to define a SWITCH statement inside an anonymous function inside a ternary operator.
Here is a script with examples:
<?php
/**
* Testing document
*/
$diff_lines = ['@', '4', '+', '-', '23', 'adsf'];
$diff2 = $diff_lines ?
function ($line) {
switch ($line[0]) {
case 'what':
$type = 'what';
break;
case '@':
$type = 'section';
break;
case '+':
$type = 'add';
break;
case '-':
$type = 'subtract';
break;
default:
$type = 'ignore';
}
return $type;
} :
null;
/**
* Test function
*
* @return array
*/
function test(): void
{
global $diff_lines, $diff2;
$diff = $diff_lines ?
array_map(
function ($line) {
switch ($line[0]) {
case '@' :
$type = 'section';
break;
case '+':
$type = 'add';
break;
case '-':
$type = 'subtract';
break;
default:
$type = 'ignore';
}
return $type;
},
$diff_lines
) :
['section' => 'No changes found.'];
$result = [
'response' => [
'blog_id' => 1,
'timestamp' => 2323,
'diff' => $diff,
'diff2' => $diff2,
],
];
return $result;
}
print_r(test());
Running this code against latest version of phpcs returns errors:
$ php --version
PHP 7.2.24 (cli) (built: Oct 22 2019 11:15:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
$ vendor/bin/phpcs --standard=PSR2 test_file.php
FILE: test_file.php
-------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
-------------------------------------------------------------------------------------------------------
1 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no
| | other side effects, or it should execute logic with side effects, but should not do
| | both. The first symbol is defined on line 34 and the first side effect is on line 6.
10 | ERROR | CASE statements must be defined using a colon
42 | ERROR | CASE statements must be defined using a colon
-------------------------------------------------------------------------------------------------------
Time: 103ms; Memory: 6MB
Please advice if you need further information. Thank you.