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

Squiz.Arrays.ArrayDeclaration.KeyNotAligned w/ short arrays inconsitent #649

Closed
ofbeaton opened this issue Jul 27, 2015 · 4 comments
Closed

Comments

@ofbeaton
Copy link
Contributor

The sniff error message Squiz.Arrays.ArrayDeclaration.KeyNotAligned does not seem to be working properly for short arrays [], it is inconsistent with the same formatting using array().

It is possible that Squiz.Arrays.ArrayDeclaration.CloseBraceNotAligned also needs to be fixed, as end brace spacing is also suspect.

@ofbeaton
Copy link
Contributor Author

ofbeaton commented Aug 7, 2015

I wanted to drop in and add more detail here. I recently had a few problems with the Squiz.Arrays.ArrayDeclaration.ValueNotAligned using short arrays in a non-work project and so I can share the code:

class A {
    protected $validFields = [
        'os',
        'pid',
        'user',
        'process',
    ];
}

The values are reported to need 30 spaces instead of 8. This seems odd, but perhaps I'm in the wrong here.

Nested arrays are also wonky:

    protected $validMods = [
        'os' => [
            '=',
            '!',
        ],
        'pid' => [
            '=',
            '!',
        ],
        'user' => [
            '=',
            '!',
        ],
        'process' => [
            '=',
            '!',
            '~=',
            '~!',
        ],
    ];

and phpcbf really mangles this array. However it should be noted that I've disabled Squiz.Arrays.ArrayDeclaration.KeyNotAligned and Squiz.Arrays.ArrayDeclaration.CloseBraceNotAligned so maybe my second example would be fixed if I had everything turned on (and if the above warnings worked properly with short arrays).

@gsherwood
Copy link
Member

The Squiz sniff wants your arrays formatted in a very specific way. In this case of your first code example, it wants it like this:

<?php
class A {
    protected $validFields = [
                              'os',
                              'pid',
                              'user',
                              'process',
                             ];
}

Which is also the exact way it want's arrays that are defined using the regular syntax.

There are no other sniffs that come with PHP_CodeSniffer that format arrays as no other coding standards actually have a strict standard for arrays. If you don't like the way this particular sniff formats arrays, you'll need to write your own checks.

And yes, your second example would work fine if you enabled the while sniff. You get this code:

<?php
protected $validMods = [
                        'os'      => [
                                      '=',
                                      '!',
                                     ],
                        'pid'     => [
                                      '=',
                                      '!',
                                     ],
                        'user'    => [
                                      '=',
                                      '!',
                                     ],
                        'process' => [
                                      '=',
                                      '!',
                                      '~=',
                                      '~!',
                                     ],
                       ];

@ofbeaton
Copy link
Contributor Author

Verified that phpcbf converted it to that, and that it was considered valid. Thanks Greg, this one wasn't so great on my part, I should have done more careful research. As always you are a fountain of patience and support. As you said, if I don't like this formatting I'll make my own sniff.

@gsherwood
Copy link
Member

No problem. I would like to create a more generic single-indent based sniff for arrays, or even change the Squiz standard in time, but a custom sniff will have to do in the meantime.

jrfnl added a commit to jrfnl/PHP_CodeSniffer that referenced this issue Nov 4, 2024
…r-name

Generic/ConstructorName: fix minor bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants