Comment style for Enum

Created on 26 November 2024, 4 months ago

Problem/Motivation

The use of comments for each case in a Enum is not mentioned in the standard.

There is a sniff, SlevomatCodingStandard.Classes.EnumCaseSpacing

Benefits

If we adopted this change, the Drupal Project would benefit by ...

Three supporters required

  1. https://www.drupal.org/u/ β†’ {userid} (yyyy-mm-dd they added support)
  2. https://www.drupal.org/u/ β†’ {userid} (yyyy-mm-dd they added support)
  3. https://www.drupal.org/u/ β†’ {userid} (yyyy-mm-dd they added support)

Proposed changes

Provide all proposed changes to the Drupal Coding standards β†’ . Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:

1. Enums β†’

Enums follow the same conventions as classes with the addition that for their cases the enums must use UpperCamelCase.

enum Exists {

  case ErrorIfExists;
  case ErrorIfNotExists;
  case ReturnEarlyIfExists;
  case ReturnEarlyIfNotExists;

  // Do stuff.
}

Enums follow the same conventions as classes with the addition that for their cases the enums must use UpperCamelCase.

enum Exists {

  //  Throw an exception when the entity exists.
  case ErrorIfExists;

  //  Thrown an exception when the entity does not exist.
  case ErrorIfNotExists;

  //  Return early if the entity exists.
  case ReturnEarlyIfExists;

  //  Return early if the entity does not exist.
  case ReturnEarlyIfNotExists;

  // Do stuff.
}

2. Repeat the above for each page or sub-page that needs to be changed.

Remaining tasks

  1. Add supporters
  2. Create a Change Record
  3. Review by the Coding Standards Committee
  4. Coding Standards Committee takes action as required
  5. Discussed by the Core Committer Committee, if it impacts Drupal Core
  6. Final review by Coding Standards Committee
  7. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  8. If applicable, create follow-up issues for PHPCS rules/sniffs changes

For a full explanation of these steps see the Coding Standards project page β†’

πŸ“Œ Task
Status

Active

Component

Coding Standards

Created by

πŸ‡³πŸ‡ΏNew Zealand quietone

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @quietone
  • πŸ‡ΊπŸ‡ΈUnited States dww

    Do we even need this issue? πŸ˜… Can't we assume that inline comments are allowed in any PHP class, whether or not it's an enum? I'm inclined to say we don't need the whole formal process for this change, and we should configure the sniff to optionally allow for comments. But if we need the formal process for this, I'm formally a supporter now. πŸ˜‚

    Thanks,
    -Derek

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I do think we need an issue because of the variety of implementations in core, which I have added to the issue summary.

    We should be able to say, at least, if a blank line is required between cases whether a comment is present or not.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Shouldn't enum cases have docblocks rather than line comments, the same as class constants and properties do?

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    +1 to #4 if we must have documentation on the cases. I'm not really a fan of the proposed solution, though, unless it can be optional like constructor parameters or even as discussed in πŸ“Œ Allow omitting doxygen when type and variable name is enough Active .

    Going with the example on phpwatch, I think an enum like this is fine:

    enum Suit {
        case Clubs;
        case Diamonds;
        case Hearts;
        case Spades;
    }
    

    If we're forced to document obvious enums like the above, then we're back to square one.

  • πŸ‡ΊπŸ‡ΈUnited States dww

    +1 to allowing optional comments.
    +1 to allowing those comments to be formatted as a formal docblock if needed for something.
    + 1 to allowing optional new lines where appropriate for legibility.
    -1 to requiring comments.
    -1 to requiring comments to be formal "docblock" style. Inline should be fine in the majority of cases.
    -1 to requiring newlines.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Using the simple style from @dww

    +1 to allowing optional comments.
    +1 to allowing those comments to be formatted as a formal docblock
    + 1 to allowing optional new lines where appropriate for legibility.
    -1 to requiring comments.
    +1 to requiring comments to be formal "docblock" style.
    -1 to requiring newlines.

    We should have one comment style instead of 3, that isn't done anywhere else.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Shouldn't these be long docblocks, the same as class properties?

    enum Exists {
    
      /**
        *  Throw an exception when the entity exists.
        */
      case ErrorIfExists;
    
  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    +1 to allowing optional comments.
    +1 to allowing those comments to be formatted as a formal docblock
    + 1 to allowing optional new lines where appropriate for legibility.
    -1 to requiring comments.
    +1 to requiring comments to be formal "docblock" style.
    -1 to requiring newlines.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    The comments so far are in favor of optional comments, using doc block style, new lines as needed. I have updated the proposal resolution with that.

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    I'm a big -1 to requiring doc blocks. Having one line comments makes scanning enums with many cases much easier. It would add a lot more scrolling if a Dev wanted to document each case but was forced to use doc blocks.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    +1 to allowing optional comments.
    +1 to allowing those comments to be formatted as a formal docblock
    + 1 to allowing optional new lines where appropriate for legibility.
    -1 to requiring comments.
    0 to requiring comments to be formal "docblock" style.
    -1 to requiring newlines.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    +1 to allowing optional comments.
    +1 to allowing those comments to be formatted as a formal docblock
    + 1 to allowing optional new lines where appropriate for legibility.
    -1 to requiring comments.
    0 to requiring comments to be formal "docblock" style.
    -1 to requiring newlines.

    -1000% for requiring comments.

    Noting that Symfony, has chosen this also. But it doesnt seem to be enforced by rulesets.

    Expanding on new lines. New line before comment, so long as there is a comment. Otherwise enums bumping into eachother is allowed.
    And new line after and before the enum brace, same as classes.

    More importantly, enums should be self explanatory. Or otherwise easily explainable by an optional class-level/top-level comment.

    
    /**
     * Optional comment.
     */
    enum Thing {
      
      case Foo1;
      case Foo2;
      case Foo3;
      
      /**
       * Description.
       */
      case Foo4;
      case Foo5;
    }
    
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    We currently have 4 supporters of doc style and 4 against the style.

    Just trying to see what it could look like if inline comments are used instead of a doc block in the unlikely case of multi line comments.

    enum Exists {
    
      /**
       * Throw an exception when the entity exists.
       *
       * Can add more information here. The quick brown fox jumped over the lazy
       * dog.
       */
      case ErrorIfExists;
    
      // Throw an exception when the entity exists.
      // 
      // Can add more information here. The quick brown fox jumped over the lazy
      // dog.
      case ErrorIfExists;
    
      // Throw an exception when the entity exists. Can add more information here.
      // The quick brown fox jumped over the lazy dog.
      case ErrorIfExists;
    
      // Do stuff. 
    }
  • πŸ‡¦πŸ‡ΊAustralia acbramley

    Re #14 - I think doc blocks are still the right approach for multiline comments. But inline comments are the right approach for 1 line comments.

    For the core FileExists enum, we currently have this (8 lines)

    enum FileExists {
    
      /* Appends a number until name is unique. */
      case Rename;
      /* Replace the existing file. */
      case Replace;
      /* Do nothing and return FALSE. */
      case Error;
    

    Enforcing doc blocks + new lines it'd be (16 lines, double the current line count)

    enum FileExists {
    
      /**
       * Appends a number until name is unique. 
       */
      case Rename;
      
      /**
       * Replace the existing file. 
       */
      case Replace;
      
      /**
       * Do nothing and return FALSE. 
       */
      case Error;
    

    Allowing inline comments would keep the line count at 8, or if we want to enforce newlines between inline commented cases it would be 10.

    Doubling the number of lines in a 3 case enum doesn't seem worthwhile when it doesn't improve the readability (IMO). For Enums with more cases this gets compounded further.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    I thought I'd chime in to clarify why I'm undecided on requiring comments to be formal "docblock" style. I think that enum case names should generally be self documenting, but in the case that they need further explanation they probably should have a thorough, well thought out comment. But perhaps there are cases where a single-line comment is fine (e.g. the existing FileExists comments). Ultimately I think it's probably better not to be prescriptive about this, so changing my vote to -1.

Production build 0.71.5 2024