Problem/Motivation

Coder has a hard check on 80 characters for any comment line, as per current coding standard. Good or bad standard, that breaks on plugin and entity docblocks.

Plugins and Entities both have annotations, which often specify long class names as values. Those class names, in practice, are longer than 80 characters with alarming frequency. Add to that the necessary padding at the start of the line for the annotation key, indentation, the comment * itself, etc. and so far every single entity I've defined has had annotation lines that cannot ever be less than 80 characters. Yet coder chokes on them and rejects every last one, so any time I touch an entity/plugin class I need to skip coder checks.

The problem also affects the @file block at the top of the file if the class name is long, too.

This is sad making.

Proposed resolution

Whitelist annotation-based docblocks and @file docblocks so that they are not checked for line length.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tatarbj’s picture

Assigned: Unassigned » tatarbj
Status: Active » Needs work
FileSize
1.2 KB

Here is my patch which can handle the mentioned @file block problem.
I'm trying to collect the annotations which can be whitelisted, if someone can help in it it would be great.

tatarbj’s picture

New patch with the test cases, the 6th one should be failed but it doesn't. Any idea why?

tatarbj’s picture

Assigned: tatarbj » Unassigned
Status: Needs work » Needs review

  • klausi committed 7705230 on 8.x-2.x
    Issue #2530920: Allow "Contains ..." file lines to be longer than 80...
klausi’s picture

Status: Needs review » Active

I committed a fix for the "Contains ..." lines in @file comments, so that they are allowed to be longer than 80 characters. I added the test to the "good" cases to make sure it does not interfere with other sniffs (all rules are run on the "good" test files which must never throw errors).

What is left for annotations, do we have an example test case?

pfrenssen’s picture

I have one! I have one!

Here's a real life annotation that triggers it, the admin paths are too long. We can adapt this to be a somewhat realistic example:

/**
 * Defines the Product property type entity.
 *
 * @ConfigEntityType(
 *   id = "product_property_type",
 *   label = @Translation("Product property type"),
 *   handlers = {
 *     "list_builder" = "Drupal\product\ProductPropertyTypeListBuilder",
 *     "form" = {
 *       "add" = "Drupal\product\Form\ProductPropertyTypeForm",
 *       "edit" = "Drupal\product\Form\ProductPropertyTypeForm",
 *       "delete" = "Drupal\product\Form\ProductPropertyTypeDeleteForm"
 *     },
 *     "access" = "Drupal\product\ProductPropertyTypeAccessControlHandler",
 *   },
 *   base_table = "product_property",
 *   config_prefix = "product_property_type",
 *   admin_permission = "administer product properties",
 *   bundle_of = "product_property",
 *   entity_keys = {
 *     "id" = "type",
 *     "label" = "label",
 *   },
 *   links = {
 *     "edit-form" = "/admin/structure/product-property/manage/{product_property}",
 *     "delete-form" = "/admin/structure/product-property/manage/{product_property}/delete",
 *     "collection" = "/admin/structure/product-property"
 *   },
 *   config_export = {
 *     "label",
 *     "type"
 *   }
 * )
 */
pfrenssen’s picture

FileSize
1.07 KB

Here's already a failing test.

pfrenssen’s picture

Status: Active » Needs review
FileSize
3.05 KB

And a fix. I have removed the check for long @Translate lines, these are also part of annotations so we don't need to check for it any more.

klausi’s picture

Status: Needs review » Needs work
  1. +++ b/coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php
    @@ -77,13 +77,24 @@ class Drupal_Sniffs_Files_LineLengthSniff extends Generic_Sniffs_Files_LineLengt
    +                // An annotation maps to a classname, so it starts with a '@'
    +                // symbol, followed by an uppercamelcased class name and ending
    +                // with a '('.
    

    Wrong docs: An annotation does not have to map to a class name. Should be replaced with "An annotation start with a "@" followed by letters and ending with a '('.

  2. +++ b/coder_sniffer/Drupal/Sniffs/Files/LineLengthSniff.php
    @@ -77,13 +77,24 @@ class Drupal_Sniffs_Files_LineLengthSniff extends Generic_Sniffs_Files_LineLengt
    +                preg_match('/^@[A-Z][A-Za-z]*\($/', $tokens[$tag]['content'], $matches);
    +                if (!empty($matches)) {
    +                    return;
    +                }
    

    We don't need $matches here, just compare the result of preg_match() === 1.

This patch also means that comments after an annotation will not throw error if they exceed 80 characters, example:

/**
 * Defines the My Fancy Entity entity type.
 *
 * By pure chance this happens to have an annotation that exceeds 80 characters.
 *
 * @ConfigEntityType(
 *   id = "my_fancy_entity_type",
 *   label = @Translation("My fancy entity type"),
 *   links = {
 *     "edit-form" = "/admin/structure/my-fancy-entity/manage/{my_fancy_entity}",
 *     "delete-form" = "/admin/structure/my-fancy-entity/manage/{my_fancy_entity}/delete",
 *     "collection" = "/admin/structure/my-fancy-entity"
 *   }
 * )
 *
 * Now of course I'm trolling here with another of those lengthy and useless comments exceeding 80 characters.
 */

But I think that is an acceptable trade-off for now since we rarely have comments after annotations, right?

  • klausi committed 26762b2 on 8.x-2.x
    Issue #2530920: Fixed line length check for overlong stuff in...
klausi’s picture

Status: Needs work » Fixed

Committed a raw regex fix that should cover the long cases.

benjy’s picture

There is a bug in the regex that checks the "Contains" line, it should also match underscore characters.

  preg_match('/^Contains [a-zA-Z_\\\\.]+$/', $tokens[($stackPtr - 2)]['content']) === 1)
klausi’s picture

Status: Fixed » Active
benjy’s picture

Status: Active » Needs review
FileSize
573 bytes
1.59 KB

Lets try this.

klausi’s picture

Status: Needs review » Fixed

Your FAIL patch is not actually failing ;-)

Fixed the test and committed it, thanks!

  • klausi committed 80c806d on 8.x-2.x authored by benjy
    Issue #2530920 by benjy, klausi: Fixed line length checks for namespaces...

Status: Fixed » Closed (fixed)

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