squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

Inline comment indentation #2314

Closed rodrigoaguilera closed 5 years ago

rodrigoaguilera commented 5 years ago

I have always developed with the Drupal code standard enabled for my code editor. https://packagist.org/packages/drupal/coder Now that I am writing some code with PSR2 and I noticed that the comments that are not indented properly are not reported by phpcs with PSR2 while they are for the Drupal standard. An example:

/**
 * Hola.
 */
function hola() {
// Hola.
    return 'this';
}

The comment should be reported by phpcs I also checked with the PEAR standard. Is this the normal behavior?

For reference this is the sniff for Drupal that detects badly indented comments. https://cgit.drupalcode.org/coder/tree/coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeIndentSniff.php?h=8.x-3.x

gsherwood commented 5 years ago

The PEAR standard does report an error for this line:

$ phpcs temp.php --standard=PEAR

FILE: /Users/gsherwood/Sites/Projects/PHP_CodeSniffer/temp.php
--------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
--------------------------------------------------------------------------------
 1 | ERROR | [ ] Missing file doc comment
 4 | ERROR | [ ] Missing @return tag in function comment
 5 | ERROR | [x] Opening brace should be on a new line
 6 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

The PSR2 standard does not as the standard explicitly excludes all rules relating to comments: https://www.php-fig.org/psr/psr-2/#7-conclusion

I had to actually add a feature to PHPCS to ignore the indentation of all comments just for PSR-2 support because I was told that PHPCS was incorrectly reporting errors for comment indentation.

rodrigoaguilera commented 5 years ago

Thank you for taking the time to look into this. About PEAR: my mistake, I was using 3.3.1. Updating phpcs did solve it.

That seems like a crazy thing from PSR2. What about PSR12? Is it possible to fix it there? It says:

2.4 Indenting Code MUST use an indent of 4 spaces for each indent level, and MUST NOT use tabs for indenting.

But I have the feeling that comments are not considered code. Shouldn't the section 7 (conclusion) from PSR2 exist in PSR12?

I guess there is nothing to do here in this project. You can close this issue. Great project!

gsherwood commented 5 years ago

PSR12 is a bit of an unknown. There are no commenting rules there, but there is no section saying that comments are to be ignored. So I'd need to clarify with the authors the next time it goes into review.