Implement a Result type in Drupal Core

Created on 3 March 2024, 10 months ago
Updated 12 August 2024, 4 months ago

Problem/Motivation

As we start doing more things asynchronously we'll have more batched work that can succeed and fail. In those kinds of scenario's an exception would cancel out the entire batch, even though it's often desirable to have a single item in a batch file while the rest of the batch continues.

Other (functional) languages solve this by providing a Result type that can be Ok or Error. By making clever use of the type-system this allows communicating that something can fail and forces the calling code to either deal with the success and error case (making sure not to forget one) or to propagate the result (optionally transforming the Ok or Error case).

PHP does not have a type like this itself so some custom code is needed. It's a capability that we will want to use in Drupal Core (e.g. for asynchronously processing BigPipe or rendering tasks where item-errors don't stop the entire process) and where contrib will want to standardise on a shared type for interoperability.

Steps to reproduce

Proposed resolution

Implement a Drupal/Core/Result (or should it be Drupal/Component/Result?) type that uses PHPStan types to implement the same logic as is available in functional programming languages (on Error you'll get the Error Type and on Ok you get the Ok Type).

An example implementation exists in https://phpstan.org/r/e9c7213e-b7ad-47f8-93b4-ec19e479b688 which is waiting for a small bit of PHPStan support and not quite ready for implementation in Drupal.

Remaining tasks

  • Write release notes snippet

User interface changes

API changes

Drupal introduces a new Result type which can be used to communicate that the result of an action can be successful or failure and that such a failure is not an exceptional circumstance (i.e. does not warrant an Exception). The Result class is typed in such a way that calling code can get type-hints of the types produced for either case.

Data model changes

Release notes snippet

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡³πŸ‡±Netherlands kingdutch

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

Merge Requests

Comments & Activities

  • Issue created by @kingdutch
  • Status changed to Needs work 10 months ago
  • πŸ‡³πŸ‡±Netherlands kingdutch

    Added a merge request to show the proposed implementation. However this "Needs Work" because we can't quite tell PHPStan what we want to do :)

    However, this allows async issues to start using the type and demonstrate its usefulness.

  • Pipeline finished with Failed
    10 months ago
    Total: 177s
    #109592
  • Status changed to Needs review 10 months ago
  • πŸ‡³πŸ‡±Netherlands kingdutch

    I've asked for some help in the PHPStan discussions on GitHub and Ondrej was very kind in providing more insights: https://github.com/phpstan/phpstan/discussions/10667. It turns out I was running into a known issue https://github.com/phpstan/phpstan/issues/6732.

    I've now pushed a workaround using a type annotation :)

  • Pipeline finished with Failed
    10 months ago
    Total: 163s
    #112625
  • Status changed to Needs work 10 months ago
  • The Needs Review Queue Bot β†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • Status changed to Needs review 10 months ago
  • πŸ‡³πŸ‡±Netherlands kingdutch

    This was failing on some coding standards (parts of which is PHPCS not yet understanding more complex PHPStan types). These were fixed in a commit amend.

  • Pipeline finished with Success
    10 months ago
    Total: 488s
    #112737
  • πŸ‡³πŸ‡±Netherlands kingdutch

    I missed Catch's feedback on the missing bool return types for isOk/isError which I've now added :D

  • Pipeline finished with Success
    9 months ago
    Total: 572s
    #115274
  • πŸ‡«πŸ‡·France andypost

    It looks straightforward so not sure a test needed.

    But it needs CR with example of use to announce it to module developers and maybe follow-up to apply monadic result to some core places

  • πŸ‡³πŸ‡±Netherlands kingdutch

    I wasn’t quite sure how to test it. I suppose we could test that the isOk and isError types do what they say.

    What I’ve previously done is write a few function calls around it with PHPStan at level 9 to make sure all the type annotations had the right behavior. We don’t really have infrastructure for that though and that should be solidified when we adopt it :)

    See the linked PHPStan sandbox for that bit of code

    I’ll write a CR proposal in the coming days unless someone is faster

  • Pipeline finished with Canceled
    9 months ago
    Total: 53s
    #115812
  • Pipeline finished with Success
    9 months ago
    Total: 619s
    #115813
  • Pipeline finished with Success
    9 months ago
    Total: 549s
    #115830
  • πŸ‡³πŸ‡±Netherlands kingdutch

    I've applied the suggested changes to the documentation and removed the sentence about the list example, instead showing it as code which I hope helps it more easily click with developers.

  • Pipeline finished with Success
    9 months ago
    Total: 509s
    #121563
  • Status changed to Needs work 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Nice!

    Shouldn't we have test coverage though to make sure this does what is expected?

  • πŸ‡ΊπŸ‡ΈUnited States Alexander Allen Bushwick, Brooklyn

    I suppose we could test that the isOk and isError types do what they say.

    Yes, the tests don't have to be large or complex for just a few types. Like you say, it's just to test the types do what's advertised, and also to provide the coverage. They can always be expanded for edge cases as needed.

    I'll crank out a test this week, and will make sure PHPStan returns accurate results (part of the tests).

  • πŸ‡ΊπŸ‡ΈUnited States Alexander Allen Bushwick, Brooklyn

    Here's my initial contribution, without any unit tests: https://phpstan.org/r/3964689e-925b-4b7f-bdc5-eca6d4eb7cf5

    • There's two class-level generics, OkT and IdentityValue
    • Generic hints are always lost in a static context, so both static ok and error methods have to define their own local method-level generics
    • PHPStan reports dumped type is Drupal\Core\Result<bool, int>, which means the type hint of the generic IdentityValue is preseved.
    • The OkT generic is locked down to a bool both at the class template and the class property, all compilation warnings have been resolved

    There is some funny business with having to specify:

        /** @var OkT $ok */
        $ok = false;
        return new self($ok, $value);
    

    But that is what was required to resolve the Method Drupal\Core\Result::ok() should return Drupal\Core\Result<T, never> but returns Drupal\Core\Result<T, T> compile error in https://phpstan.org/r/e9c7213e-b7ad-47f8-93b4-ec19e479b688

    Might want to ask Ondrej or one of the smarter guys about that one.

    Now about those unit tests...where to place them...

  • πŸ‡³πŸ‡±Netherlands kingdutch

    Sorry Alexander Allen! Looks like you started off from an older starting point. From a types perspective the MR is already ready to go.

    I've updated the issue summary's remaining tasks to better reflect that.

  • Pipeline finished with Success
    5 months ago
    Total: 1038s
    #221596
  • πŸ‡ΊπŸ‡ΈUnited States Alexander Allen Bushwick, Brooklyn

    Haha @Kingdutch, I played myself there! I'm still laughing over here.

    I rebased 11.x so it's nice and up to date and I'm going to take a crack at writing a basic test that checks for the class instance, does some basic returns, etc. just too see if I can help a bit. Cheers!

  • Pipeline finished with Failed
    5 months ago
    Total: 439s
    #221826
  • Pipeline finished with Failed
    5 months ago
    Total: 159s
    #221856
  • Pipeline finished with Failed
    5 months ago
    Total: 176s
    #221887
  • Pipeline finished with Failed
    5 months ago
    Total: 197s
    #221889
  • Pipeline finished with Failed
    5 months ago
    Total: 346s
    #221897
  • Pipeline finished with Success
    5 months ago
    Total: 578s
    #221934
  • Pipeline finished with Failed
    5 months ago
    Total: 520s
    #221952
  • Pipeline finished with Success
    5 months ago
    Total: 454s
    #221959
  • Status changed to Needs review 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States Alexander Allen Bushwick, Brooklyn
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Ran the test-only feature to verify/show the test coverage is there https://github.com/phpstan/phpstan/issues/6732 which it is!

    Reading the CR, believe all the info is there with examples. Added the default branch of 11.x

    Appears all feedback/threads have been addressed.

    Code review not entirely sure don't see other examples in core of using things like @template T or @param T $value

    I'm a +1 but going to leave in review for a better code review.

  • Status changed to RTBC 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Going to take a leap of faith and mark it

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

    I read the MR and made comments there. This needs work to make sure that the result on a.d.o is readable. This is using tags that a.d.o does not understand and the output is not legible.

  • Status changed to Needs work 4 months ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    Moving to needs work for #19.

  • πŸ‡ΊπŸ‡ΈUnited States Alexander Allen Bushwick, Brooklyn

    I am not sure I can address all the review issues by myself, but I will start by looking into the readability issues created by using generic tags.

    Thank you for the review @quietone.

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

    @alexander allen, the short answer is that I don't know much about how api.drupal.org works. I figure out it by testing on my local version. The API project page β†’ has a link to instructions for setting it up locally.

    Having said that, if we use the tags that are documented in the Coding Standards for comments β†’ , it will work much better. I was only able to make the suggestions I did after testing locally.

Production build 0.71.5 2024