Change ProjectBrowserSourceBase's option methods to be enums

Created on 15 May 2024, 8 months ago
Updated 20 June 2024, 7 months ago

Problem/Motivation

ProjectBrowserSourceBase includes three methods (also defined on the interface) which are final and declare lists of options. This is a perfect use case for enums, which are basically sets of options that aren't extensible.

Proposed resolution

Remove those methods, thus reducing API surface, and replace them with enums.

✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

Live updates comments and jobs are added and updated live.
  • API clean-up

    Refactors an existing API or subsystem for consistency, performance, modularization, flexibility, third-party integration, etc. May imply an API change. Frequently used during the Code Slush phase of the release cycle.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @phenaproxima
  • πŸ‡¦πŸ‡ΊAustralia sime Melbourne

    Would you like a separate enum for the IDs and one for the LABELs?

  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    No need -- enums can do __toString()!

  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts
  • πŸ‡¦πŸ‡ΊAustralia sime Melbourne

    I believe that means this...

    enum Status: string
    {
        case Maintained = 'Maintained';
        case Covered = 'Covered by a security policy';
        case Active = 'Active';
        case All = 'Show all';
    }
    

    .. or ...

    enum Status: string
    {
        case maintained = 'Maintained';
        case covered = 'Covered by a security policy';
        case active = 'Active';
        case all = 'Show all';
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    It doesn't.

    The enum could be string-backed to have a string value, and implement __toString() to produce a full human-readable label.

  • πŸ‡¦πŸ‡ΊAustralia sime Melbourne

    Nah it's explicitly not supported. But looks like you can add public methods and have this:

    echo Status::Covered->name; // Covered
    echo Status::Covered->value; // covered
    echo Status::Covered->label(); // Covered by a security policy
    

    I did a sandbox for this which includes a __toString() commented out. If you uncomment it you get.

    Fatal error: Enum Status cannot include magic method __toString in /home/user/scripts/code.php on line 4
    
  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    Hmmm…maybe we just add a regular toString(), then, like the Url object has.

  • Merge request !491Turn Project Status into an enum β†’ (Open) created by sime
  • Pipeline finished with Failed
    8 months ago
    Total: 239s
    #188159
  • Pipeline finished with Failed
    8 months ago
    Total: 635s
    #188162
  • Pipeline finished with Failed
    8 months ago
    Total: 478s
    #188229
  • Pipeline finished with Failed
    8 months ago
    Total: 495s
    #188240
  • Pipeline finished with Success
    8 months ago
    #188250
  • Status changed to Needs review 8 months ago
  • πŸ‡¦πŸ‡ΊAustralia sime Melbourne

    Ok what do you think of this? toString() felt very ambiguous, and it really would be nice to have magic __toString() support here. Either way I think it's at 95% and I'm not offended if you change the implementation.

  • Merge request !493Convert constants to enums β†’ (Merged) created by phenaproxima
  • Pipeline finished with Failed
    8 months ago
    Total: 350s
    #190078
  • Pipeline finished with Canceled
    8 months ago
    Total: 90s
    #190084
  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    I propose a slightly different approach: https://git.drupalcode.org/project/project_browser/-/merge_requests/493

    Backed enums don't need a specialized id() method, they already have ->value to access the backing value. They really just need two things, as far as I can tell: the ability to be represented as a keyed array of translatable labels, and, well, a translatable label per-case. That's what I've implemented. Thoughts?

  • Pipeline finished with Failed
    8 months ago
    Total: 337s
    #190086
  • Pipeline finished with Success
    8 months ago
    Total: 385s
    #190091
  • Pipeline finished with Success
    8 months ago
    Total: 457s
    #190308
  • πŸ‡¦πŸ‡ΊAustralia sime Melbourne

    I rebased and added one commit to your new branch. Just a few values not replaced.

    Backed enums don't need a specialized id() method, they already have ->value to access the backing value.

    One way or another you would have to overload the enum. I don't think id() was any better or worse than toString() or label(). __toString() would have hidden this away for sure.

  • Pipeline finished with Success
    8 months ago
    Total: 7319s
    #190314
  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts
  • πŸ‡ΊπŸ‡ΈUnited States chrisfromredfin Portland, Maine

    chrisfromredfin β†’ changed the visibility of the branch 3447420-enum-options to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    phenaproxima β†’ changed the visibility of the branch 3447420-enum-options to active.

  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    phenaproxima β†’ changed the visibility of the branch 3447420-enum-options to hidden.

  • Status changed to Fixed 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States chrisfromredfin Portland, Maine

    word to your mother

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

Production build 0.71.5 2024