Great new sniff in the last version of coder, namely ClassFileNameSniff.

However, if one has code examples in README files, this can lead to some annoying false positives.

Steps to reproduce:

- Clone coder.
- Create a folder called test_fails_folder
- Create a file called README.md with a code example with a class in it. In my case it contains the following:

this is not a php file

```php
<?php

/**
 * Class.
 */
class Test {
}
```

- Run phpcs with the following command:

$ ./vendor/bin/phpcs --standard=./coder_sniffer/Drupal test_fails_folder/

FILE: /xxx/coder/test_fails_folder/README.md
------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------
 9 | ERROR | Class name doesn't match filename; expected "class README"
------------------------------------------------------------------------

Time: 124ms; Memory: 8MB

I would expect this case to not give me any errors, since this file is only a markdown file, and even if it contains php code, it would for sure be inconvenient to always name the first code example containing a class after the filename itself to get the code style to pass :)

Patch coming up

CommentFileSizeAuthor
#2 3074186.patch932 byteseiriksm
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

eiriksm created an issue. See original summary.

eiriksm’s picture

Status: Active » Needs review
FileSize
932 bytes

Patch attached. Would be nice with a test, but I do not have time for that today :(

eiriksm’s picture

klausi’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

I think this fix makes sense. We should add a test case with an example *.md or *.txt file and make sure it does not throw errors.

eiriksm’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Added a test in the PR. Still unsure how you prefer code to be posted to this project, but its over there at least :)

klausi’s picture

Status: Needs review » Needs work

Nice, thanks a lot! Left some comments there.

eiriksm’s picture

Status: Needs work » Needs review

Should i set this issue to "needs review" again, now that I followed up on your comments? :)

  • klausi committed 7b3db8a on 8.x-3.x authored by eiriksm
    fix(ClassFileName): Avoid running ClassFileNameSniff on text files (#...
klausi’s picture

Status: Needs review » Fixed

Committed, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.