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
Luiz Machado
@odahcam
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..
Greg Sherwood
@gsherwood
@gmponos PSR2 doesn't say anything about empty bodies. You can see a readable version here: https://www.php-fig.org/psr/psr-2/
Alex
@aik099

Hi,

I'm trying to find sniff, that would detect attempt to change function/method parameter value assignment, that isn't passed by reference. Anybody aware of such a sniff?

If not found, then what approach for writing that sniff is better performance-wise, e.g.:

  1. listen for T_FUNCTON token
  2. get parameters of that function, that aren't passed by reference
  3. lookup names of these parameters in function body and T_WHITESPACE (optional) + assignment sniffs after it

?

Example code:

function myFunc($param1, &$param2) {
    echo 'test';
    $param1 = 'bad idea';
}

Thanks.

Greg Sherwood
@gsherwood

@aik099 I'm not aware of a sniff to do this, but you could attempt to check for value assignment using the method you've outlined. The problem you will have is developers using dynamic assignments, like:

function myFunc($param1, &$param2) {
    $var ='param1';
    $$var = 'bad idea';
}

You'll need to decide if you care about that or not. Tracking it is not something I've attempted.

Alex
@aik099
@gsherwood, Thanks, I'll try that. Yeah, I will probably write another sniff to disallow $$variable stuff, because it prevents static code analysis tools from working correctly.
Glen
@glen-84
The File::getMethodParamaters() method now includes a has_body array index in the return value
    TRUE if the method has no body (as with abstract and interface methods) or FALSE otherwise
@gsherwood The method name is spelled incorrectly, and I think those Boolean values are swapped as well.
Juliette
@jrfnl
@glen-84 It actually refers to the wrong method - see squizlabs/PHP_CodeSniffer#2326, though I hadn't noticed the value swopping before, I've updated my PR to fix that too. Thanks!
Glen
@glen-84
:thumbsup:
Glen
@glen-84
@jrfnl Regarding Generic.CodeAnalysis.UnusedFunctionParameter, it's not possible to determine whether the method is overriding a method in the parent class?
Juliette
@jrfnl

@glen-84 No, unfortunately not as that would require the specs of the parent class which PHPCS may or may not have information on.
What I mean by that is, that:

  • The parent class may be in a directory not being scanned (vendor).
  • The specs of a PHP native parent class may have changed between PHP versions and the PHP version which PHPCS is being run is not necessarily the same version on which the application should be able to run.
  • The parent class may be scanned after the child class, meaning that the information is not available yet when the child class is scanned.
  • The parent class may be scanned in a different parallel process from the child class.

Does that explain it well enough ?

Glen
@glen-84
@jrfnl Very well explained, thank you. :slight_smile:
Steven Klein
@steven-klein

Hi, I'm having an issue where include patterns seem to be applying differently between my local environment and Travis CI. Has anyone run into that?

Example: the complexity of third party code makes using an include much easier than exclude patters. The following locally only checks directories that start with foo- or files that start with foo-*. But in Travis it's not being respected.

<rule ref="PSR2">
  <include-pattern>*/foo\-*/*</include-pattern>
  <include-pattern>*/foo\-*\.php</include-pattern>
</rule>
Juliette
@jrfnl
@steven-klein Could it be that your organisation or repo has foo- somewhere in the name ? Try adding an echo of the PATH in Travis to see if the path your repo is cloned to could cause the conflict.
Steven Klein
@steven-klein
@jrfnl That was it. Thanks!
Alex
@aik099
Hi. Is there a way to dynamically disable one of errors/warnings in a sniff based on say function name within what an error/warning is reported?
Alex
@aik099
For example I want to ignore “Squiz.Commenting.FunctionXomment.TypeHintMissing” error/warning when a function in question has predefined name.
Alex
@aik099
I've found a hack (or maybe it's officially supported way) to add specific sniff codes as ignored for a specific lines in real time (from "process" method of the sniff like this):
$tokens = $phpcsFile->getTokens();
$phpcsFile->tokenizer->ignoredLines[$tokens[$stackPtr]['line']] = array(
    'CodingStandard.Commenting.FunctionComment.TypeHintMissing' => true,
);
Greg Sherwood
@gsherwood
@aik099 There is no officially supported way of doing this
Alex
@aik099
Any chance the code I’ve shown would continue to work in the future?
Greg Sherwood
@gsherwood
@aik099 I think I changed the way that code worked last year, so it's possible I wont change it again for a while, but I absolutely can't commit to that. If it needs to change to fix a bug or support new features, I'd change it.