Enforce return types in all new methods and functions

Created on 15 July 2024, about 2 months ago
Updated 31 August 2024, 8 days ago

Problem/Motivation

Implementing strict typing in existing code is challenging. If we don't enforce it on new code we'll constantly be chasing our tail. We get missing type checks out of the box with phpstan level 6, but we're currently stuck on level 1.

This issue proposes enabling just the rules required to enforce return types, while remaining at the current level.

Steps to reproduce

N/A

Proposed resolution

  • Enable MissingFunctionReturnTypehintRule and MissingMethodReturnTypehintRule phpstan rules
  • Ignore missingType.iterableValue
  • Update the phpstan baseline to include all violations of this rule

Remaining tasks

Review the changes to phpstan.neon.dist and what's currently being added to the baseline, even if it's not mergeable, to verify that we're only adding violations of identifier: missingType.return.

For example:

# Check how many new errors are ignored.
$ git diff 11.x | grep '^\+$ignoreErrors\[]' | wc -l
12378

# Check how many 'missingType.return' errors are added.
$ git diff 11.x | grep '+.*// identifier:' | grep 'missingType.return' | wc -l
12378

# Check what other lines are removed.
$ git diff 11.x | grep '^\-\s'
-	// identifier: variable.undefined
-	'message' => '#^Variable \\$patterns might not be defined\\.$#',
-	'message' => '#^Missing cache backend declaration for performance\\.$#',
-	// identifier: variable.undefined
-	'message' => '#^Variable \\$inner_count might not be defined\\.$#',

Baseline size comparison

Before: 2640 lines, 115K
After: 76908 lines, 3.7M

User interface changes

API changes

Data model changes

Release notes snippet

All new functions and methods must have return types defined.

✨ Feature request
Status

RTBC

Version

11.0 πŸ”₯

Component
OtherΒ  β†’

Last updated about 8 hours ago

Created by

πŸ‡¦πŸ‡ΊAustralia mstrelan

Live updates comments and jobs are added and updated live.
  • Needs framework manager review

    It is used to alert the framework manager core committer(s) that an issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy draft for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

  • Needs release manager review

    It is used to alert the release manager core committer(s) that an issue significantly affects the overall technical debt or release timeline of Drupal, and their signoff is needed. See the governance policy draft for more information.

  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @mstrelan
  • Pipeline finished with Success
    about 2 months ago
    Total: 479s
    #224203
  • Status changed to Needs review about 2 months ago
  • πŸ‡«πŸ‡·France andypost

    Stats for baseline looks big but looks like it should be better then growing amount of newcode

    + 75718
    βˆ’ 1390
    
  • Status changed to Needs work about 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    MR appears to be unmergable

  • Status changed to Needs review about 2 months ago
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Obviously it will need constant rebasing. Setting back to NR to get actual review.

  • Status changed to Needs work about 2 months ago
  • The Needs Review Queue Bot β†’ tested this issue. It no longer applies to Drupal core. 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 about 2 months ago
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Let's just review the idea and the changes to phpstan.neon.dist. If we're happy with that then we can regenerate the baseline. We can also review what's currently being added to the baseline, even if it's not mergeable, to verify that we're only identifier: missingType.return.

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

    Enable MissingFunctionReturnTypehintRule and MissingMethodReturnTypehintRule phpstan rules

    What if we broke these out into 2 tickets would that be easier?

  • πŸ‡¦πŸ‡ΊAustralia mstrelan
    $ grep "Method" 8763.diff | wc -l
    10753
    $ grep "Function" 8763.diff | wc -l
    7335
    

    Not sure there's any difference reviewing 10000 or 17000 changes.

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

    Would it be easier to breakout into groups of components or group of modules?

    Just brainstorming to help keep the idea moving.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    I appreciate that the MR is massive but really the only change that needs review at all is to phpstan.neon.dist. Everything else is proven by the phpstan job passing. I don't think committing in chunks will help, this probably just needs "Needs ____ review" but I don't know what ____ is, maybe FM?

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

    Framework would probably be best. And would just ping them.

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

    Can you add the current size of the baseline and the size with this MR?

  • πŸ‡¦πŸ‡ΊAustralia mstrelan
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Rebased, re-baselined and rewrote the issue summary. FWIW I don't know why some lines were removed from the baseline, but that can only be a good thing as long as the phpstan job still passes.

  • Pipeline finished with Success
    11 days ago
    Total: 970s
    #266467
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    So what would be the scope or game plan to address these eventually? So people aren’t doing a bunch of tickets

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    This issue is not about adding the return types to existing methods, it's just about preventing new methods from being added that don't have a return type. I think adding return types to methods in run time code needs BC consideration most of the time.

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    Big +1 on this and agree with #12. The longer we wait to do this the larger the potential baseline as more and more code gets added, I know we're pretty good at asking for return types but this would enforce it.

  • πŸ‡³πŸ‡±Netherlands Spokje

    *jumps on the +1 bandwagon*

    I think adding return types to methods in run time code needs BC consideration most of the time.

    Having tried to work on PHPStan lvl 1 issues in the current baseline in a previous life, I very much think this is the case.

    I see a slight risk of people adding a new missingType.return error to the baseline, since they're seeing the current ones also being in the baseline, but with a explanatory CR and, since almost nobody will read that, a vigilant NR and core committers crew (so no problem there :) we will be fine.

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

    Let’s see if it can get in

  • Status changed to RTBC 11 days ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Also imagine if this gets in a number of tickets may get sent back

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    This is a great step imo to start enforcing.

    This might also need an update to the Coding Standards. And if this is enforced do we need to say something about new classes that might not be able to add since they for example implement an interface or extend a base class? There might be place where this is imossoble.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Due to covariance you can add a return type to a child class when the parent or interface doesn't have one. See https://www.php.net/manual/en/language.oop5.variance.php

Production build 0.71.5 2024