Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 31 2019 22:52
    jrfnl commented #2387
  • Jan 31 2019 22:41
    valeryan commented #2386
  • Jan 31 2019 22:40
    valeryan commented #2386
  • Jan 31 2019 22:40
    valeryan commented #2386
  • Jan 31 2019 22:17
    gsherwood commented #2387
  • Jan 31 2019 22:14
    gsherwood milestoned #2387
  • Jan 31 2019 22:13
    gsherwood labeled #2386
  • Jan 31 2019 22:13
    gsherwood commented #2386
  • Jan 31 2019 22:05
    gsherwood milestoned #2388
  • Jan 31 2019 22:03
    gsherwood labeled #2394
  • Jan 31 2019 22:02
    gsherwood commented #2394
  • Jan 31 2019 22:02
    gsherwood closed #2394
  • Jan 31 2019 22:02

    gsherwood on master

    Fixed docblocks Because custom… (compare)

  • Jan 31 2019 22:02
    gsherwood milestoned #2394
  • Jan 31 2019 16:50
  • Jan 31 2019 16:00
    SteepDevs starred squizlabs/PHP_CodeSniffer
  • Jan 31 2019 15:22
  • Jan 31 2019 14:39
    sebastiaanluca commented #2304
  • Jan 31 2019 14:38
    sebastiaanluca commented #2304
  • Jan 31 2019 13:09
    rlauenroth starred squizlabs/PHP_CodeSniffer
Juliette
@jrfnl
@enrmarc Are you using a custom ruleset or passing everything via the command-line ?
On the command-line you can do:
phpcs . --standard=Slevomat --sniffs=Slevomat.Cat.SniffOne,Slevomat.Cat.SniffTwo
to selectively run only a few sniffs.
With a custom ruleset, just don't include the standard, only include the individual sniffs.
Enrique Marcos
@enrmarc

@jrfnl I'm using the ruleset.

    <rule ref="../../../../vendor/slevomat/coding-standard/SlevomatCodingStandard/ruleset.xml">
        <exclude name="SlevomatCodingStandard.Arrays"/>
        <exclude name="SlevomatCodingStandard.Classes"/>
        <exclude name="SlevomatCodingStandard.Commenting"/>
        <exclude name="SlevomatCodingStandard.ControlStructures"/>
        <exclude name="SlevomatCodingStandard.Exceptions"/>
        <exclude name="SlevomatCodingStandard.Files"/>
        <exclude name="SlevomatCodingStandard.Namespaces"/>
        <exclude name="SlevomatCodingStandard.Operators"/>
    </rule>

    <rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration">
        <properties>
            <property name="allAnnotationsAreUseful" value="true"/>
        </properties>
    </rule>

So, If I do not include the first rule, then I get an error about the second one (which is the one I'm interested about): phpcs: ERROR: Referenced sniff "SlevomatCodingStandard.TypeHints.TypeHintDeclaration" does not exist

Juliette
@jrfnl

@enrmarc Ah! You need to register the standard with PHPCS using --config-set installed_paths. See: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-installed-standard-paths
This is a one-time only action.

If you are using PHPCS 2.6.0 or higher, you can even do so from the ruleset:
<config name="installed_paths" value="../../../../vendor/slevomat/coding-standard/"/>

Enrique Marcos
@enrmarc
@jrfnl Thanks! It's working :+1:
Maurício Meneghini Fauth
@mauriciofauth
I ran phpcs on Travis CI and even with phpcs accusing error, the build did not fail.
Am I missing something?
Maurício Meneghini Fauth
@mauriciofauth
Sorry for that, I was actually missing something. I ended up putting an echo after phpcs and script was returning the echo exit code.
Mponos George
@gmponos

Hello, I would like to have a sniff that will check that there is only one space around double arrow operater in order to avoid having arrays alinged...

So what I need to achieve is this to be forbidden:

$array = [
    'my_key'        => 'value',
    'my_bigger_key' => 'val;
];
I tried using the Squiz.Whitespace.OperatorSpacing
but it didn't catch anything like that
Greg Sherwood
@gsherwood
@gmponos There isn't an included sniff to do that, but you could write a sniff that extends AbstractArraySniff, which gives you two methods to override (one for single line arrays and one for multi-line arrays) that pass in the position of all keys, values, and double arrows. So checking that there is a single space around each double array wouldn't be hard to do as you can just loop over the passed array. An example array sniff is here: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Walt Sorensen
@photodude
Is there a rule to enforce the special compiled PHP functions? I've looked but didn't find anything.
background... PHP 7+ has some functions are replaced by opcodes, producing much faster code.
these functions need to be referenced in the root namespace at compile time: Either there is no namespace, or they are prefixed by a \
Here is the exact list of functions that have this behavior:
https://github.com/php/php-src/blob/f2db305fa4e9bd7d04d567822687ec714aedcdb5/Zend/zend_compile.c#L3872
Greg Sherwood
@gsherwood
@photodude I don't know of any sniffs that do this
Juliette
@jrfnl
@photodude Interesting! I think that would be a very useful sniff.
Walt Sorensen
@photodude
@jrfnl I agree
Walt Sorensen
@photodude
The question above was originally asked by @mbabker about PHPCS, it's based on the following issue FriendsOfPHP/PHP-CS-Fixer#3048
Luiz Machado
@odahcam
hey!
how can I know how to recognize the rules format throught php code?
eg: Squiz.Commenting.InlineComment
how can I find how it works or it's sub-configs?
Greg Sherwood
@gsherwood

@odahcam There is very little documentation for the sniffs, but the properties you can change are listed here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties

Some sniffs do have docs built in, which you can access through the command line. For example:

phpcs --standard=Generic --sniffs=Generic.Formatting.MultipleStatementAlignment --generator=text
-----------------------------------------------------------
| GENERIC CODING STANDARD: ALIGNING BLOCKS OF ASSIGNMENTS |
-----------------------------------------------------------

There should be one space on either side of an equals sign used to assign a value to a variable. In
the case of a block of related assignments, more space may be inserted to promote readability.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Equals signs aligned                           | Not aligned; harder to read                     |
----------------------------------------------------------------------------------------------------
| $shortVar        = (1 + 2);                    | $shortVar = (1 + 2);                            |
| $veryLongVarName = 'string';                   | $veryLongVarName = 'string';                    |
| $var             = foo($bar, $baz, $quux);     | $var = foo($bar, $baz, $quux);                  |
----------------------------------------------------------------------------------------------------

When using plus-equals, minus-equals etc. still ensure the equals signs are aligned to one space
after the longest variable name.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Equals signs aligned; only one space after     | Two spaces after longest var name               |
| longestvar name                                |                                                 |
----------------------------------------------------------------------------------------------------
| $shortVar       += 1;                          | $shortVar        += 1;                          |
| $veryLongVarName = 1;                          | $veryLongVarName  = 1;                          |
----------------------------------------------------------------------------------------------------

----------------------------------------- CODE COMPARISON ------------------------------------------
| Equals signs aligned                           | Equals signs not aligned                        |
----------------------------------------------------------------------------------------------------
| $shortVar         = 1;                         | $shortVar        = 1;                           |
| $veryLongVarName -= 1;                         | $veryLongVarName -= 1;                          |
----------------------------------------------------------------------------------------------------

This particular sniff doesn't have docs though, so you'd have to look through the sniff source code to find the error messages it produces, or run it over your code and see if it works the way you want it to. Note that you can also included specific error codes in a ruleset (or exclude them) if you like a sniff but don't like some of the rules it enforces.

Luiz Machado
@odahcam
Thanks for help, I'll try to study more.
This amount of configurations, rules and patterns are very hard to learn as a beginner to them.
Bryan Evans
@BryIsTheGuy_twitter
I am having trouble getting Generic.CodeAnalysis.UnusedFunctionParameter to display. I understand it throws a warning of severity 5. I can get it to display when I specifically set --warning-severity=5 in the command line, but I can not get it to display without that. Having <arg name="warning-severity" value="1" /> in phpcs.xml doesn't help, nor does putting <severity>9</severity> in the rule. <type>error</type> does cause it to display. It's almost as if something else is hiding warnings, but I don't know what.
Bryan Evans
@BryIsTheGuy_twitter
I also have show_warnings = 1 in vendor/squizlabs/php_codesniffer/CodeSniffer.conf
Hmm, adding -w to command line works. But it says that's the default.
Juliette
@jrfnl
@BryIsTheGuy_twitter Could it be that you use -n on the command-line ? Or have it set from within a custom ruleset <arg value="n"/> ?
Bryan Evans
@BryIsTheGuy_twitter
Ahhhh! That's what that line means! Thanks, I'm dumb :( I had <arg value="np"/> which was from the sample file I used.
Hmm, still had to specify warning-severity 5 without the <arg> line.
Which I think is supposed to be the default
Oh well, it works. Thanks!
Juliette
@jrfnl
@BryIsTheGuy_twitter You shouldn't need the warning-severity as long as you don't silence warnings with -n. Anyways, glad to hear you got it working.
James Hoegerl
@la-jamesh
How can I enable unused imports?
Greg Sherwood
@gsherwood
@la-jamesh PHPCS doesnt come with a sniff to check for unused imports so you'd need to find and use another coding standard that provides that functionality. I think the Drupal standard has one, but I haven't used it.
Juliette
@jrfnl
If I remember correctly the Slevomat standard has one too.
Jesse Donat
@donatj
Is there any way to set a sniff property as a CLI parameter?
Greg Sherwood
@gsherwood
@donatj Not on the CLI. Sniff properties can only be set via ruleset files
Jesse Donat
@donatj
@gsherwood Hmm, lol, it'd be real nice if that were an option, not have to create multiple rulesets for steps of my makefile
Ivan Peevski
@ipeevski
Is there a rule to detect empty == statement - such as "$var == 0;" (the intended would be "$var = 0;")
Greg Sherwood
@gsherwood
@ipeevski No, nothing in the included set of sniffs.
Glen
@glen-84
@jrfnl Would it make sense to allow assignments in a condition if it's wrapped in parentheses to make it explicit, like if (($post = $this->getRequest()->getPost())) {?
Juliette
@jrfnl

@glen-84 Been thinking a bit about your question. I don't think that's a good idea as it would take away from the principle of the sniff - forbidding assignments in conditions -. I could imagine we can add a separate errorcode for assignments within arbitrary parenthesis - like there is a separate error code for assignments in a while condition, but it would still take away from the intend of the sniff.

For now, you can whitelist a condition in arbitrary parenthesis with the PHPCS 3.2+ annotations, like // phpcs:ignore Ruleset.Cat.Sniffname.Errorcode. Would that work for you ?

Glen
@glen-84
@jrfnl I am aware of the suppression comments, I just thought that this might be a cleaner solution when you "know what you're doing". Anyway, I respect your decision and thank you for your reply.
Juliette
@jrfnl
@glen-84 Not a decision, just my opinion ;-)
Juliette
@jrfnl
@glen-84 Also - using parentheses does not necessarily mean that someone knows what they are doing and PHPCS cannot analyze whether the dev knew what they were doing or "accidentally" used the syntax.
Petr Bugyík
@o5
Hello! Is there sniff for enforcing only one way to define an array type in phpdocs? I mean "Long form" vs "Short form" which is described in table "Several ways to define an array type" http://www.icosaedro.it/phplint/phplint2/doc/reference/docblocks.htm#H12_Types
Greg Sherwood
@gsherwood
@o5 There is no included sniff to enforce array types in docblocks. I've been waiting for the docblock PSR to be completed before attempting to write any more doc-based standards so I have something to build towards.
Petr Bugyík
@o5
@gsherwood thank you for info!
Mponos George
@gmponos

@gsherwood basically for this issue here: squizlabs/PHP_CodeSniffer#2226

but what about this: https://github.com/guzzle/guzzle/pull/2194/files#r232765653

should have mentioned both on chat and not create an issue, I had forgot about the chat..