Php_codesniffer: Generic.Arrays fails to fix array

Created on 30 Jul 2018  路  4Comments  路  Source: squizlabs/PHP_CodeSniffer

Generic.Arrays fails to fix array if string ends on next line and on the same line as array.

Example code:
test.zip

query(
"
SELECT *
FROM dual
", array(
array('one'),
)
);

Ruleset:
<?xml version="1.0"?> <ruleset name="MyRuleset"> <description>My ruleset</description> <rule ref="Generic.Arrays" /> </ruleset>

Result after running phpcs:

FOUND 4 ERRORS AFFECTING 3 LINES
7 | ERROR | [x] Short array syntax must be used to define arrays
| | (Generic.Arrays.DisallowLongArraySyntax.Found)
8 | ERROR | [x] Array key not indented correctly; expected 4 spaces
| | but found 2
| | (Generic.Arrays.ArrayIndent.KeyIncorrect)
8 | ERROR | [x] Short array syntax must be used to define arrays
| | (Generic.Arrays.DisallowLongArraySyntax.Found)
9 | ERROR | [x] Array close brace not indented correctly; expected 0
| | spaces but found 1
| | (Generic.Arrays.ArrayIndent.CloseBraceIncorrect)
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY

But after running phpcbf it failt to fix array:
C:inetpubwwwrootprogramgk3test.php FAILED TO FIX
A TOTAL OF 2 ERRORS WERE FIXED IN 1 FILE
PHPCBF FAILED TO FIX 1 FILE

Similar problem are with WordPress.Arrays.

Phpcs version: 3.3.1, php version: 7.1.18, Windows 7 iis 7.5.

All 4 comments

I can't comment on WordPress array sniffs, but you can't just include all the Generic array sniffs in a standard. The sniffs you are including by doing that are:

  • Generic.Arrays.ArrayIndent
  • Generic.Arrays.DisallowLongArraySyntax
  • Generic.Arrays.DisallowShortArraySyntax

So you've got one sniff trying to convert your long array into short array syntax and another sniff trying to convert it back to the long array syntax. You'd need to pick one.

In your case, I'd suggest:

<rule ref="Generic.Arrays.ArrayIndent" />
<rule ref="Generic.Arrays.DisallowShortArraySyntax" />

But you'll need a lot more sniffs to format your code correctly.

I've just ran a test with the WordPress sniffs and this is not really a problem in the WP Array sniffs.

WP allows having multi-line function calls with multiple arguments on each line.
WP also bases array indentation off the line on which the array opener is found.

The problem with your code is that the line on which the array opener is, starts in column 1 with the trailing whitespace at the end of your multi-line text string and it's not the responsibility of an array sniff to examine whether what preceeds the array is a multi-line text string and if so, in what column that multi-line text string started.

There are two options here.

Either make a manual fix to have the closing quote of the multi-line text string on the line above or the array opener on its own line, like so:

query(
    "
        SELECT *
        FROM dual"
    , array(
        array('one'),
    )
);
// Or:
query(
    "
        SELECT *
        FROM dual
    ",
    array(
        array('one'),
    )
);

_Whether you have the comma after the text string on the line where the text string ends or before the array opener will not matter as the fixers will work fine with either._

The second option - _this would be my recommendation_ - is to add the following configuration to your custom ruleset:

<rule ref="PEAR.Functions.FunctionCallSignature">
    <properties>
        <property name="allowMultipleArguments" value="false"/>
    </properties>
</rule>

This will force the FunctionCallsignature sniff to put each argument in a multi-line function call on its own line which - again - will allow the WP array fixers to work.

I added allowMultipleArguments configuration and works like expected. Thank you.

@engelmajster Glad to hear it helped.

Was this page helpful?
0 / 5 - 0 ratings