PHP_CodeSniffer: Squiz.Arrays.ArrayDeclaration throws NoComma error when array value is a shorthand IF statement
I’m trying to check this file:
<?php
declare(strict_types=1);
namespace Test;
class SomeClass
{
protected function transformRow(array $row)
{
return [
'key1' => 'value1',
'key2' => (int) $row['value2'],
'status_verify' => $row['status'] === 'rejected'
? self::REJECTED_CODE
: self::VERIFIED_CODE,
'user_verification_status' => in_array($row['status'], ['notverified', 'unverified'], true)
? self::STATUS_PENDING
: self::STATUS_VERIFIED,
'created_at' => strtotime($row['date']),
];
}
}
With this command:
docker run --rm -v $(pwd):/scripts/ texthtml/phpcs:3.3.0 phpcs --standard=/scripts/phpcs.xml -n -p -s /scripts/test.php
And I’m getting these unexpected errors:
13 | ERROR | [x] Each line in an array declaration must end in a | | comma (Squiz.Arrays.ArrayDeclaration.NoComma) 16 | ERROR | [x] Each line in an array declaration must end in a | | comma (Squiz.Arrays.ArrayDeclaration.NoComma)
Interesting, that if I replace status_verify with key3 f.e., the error on the line 13 will disappear. And if I replace user_verification_status with key4 f.e., the all errors will disappear 🤔
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (10 by maintainers)
@EvgenyOrekhov Your issue is actually by bug #2120 that has been fixed in master. The problem is that the inline if was not being tokenized properly in very specific cases (read the bug report for an example).
So I can confirm that your code is exposing a bug in 3.3.1 but it will be fixed in 3.3.2. Thanks for posting that code.
Still getting this error on version 3.3.1 with the following code:
I was planning to create one 3 weeks ago but I’ve had barely any time to work on PHPCS lately. I hope to have a release out ASAP though.