Problem/Motivation

There are currently 7866 missing return types in the phpstan baseline.

Adding return types to runtime code has BC concerns, but adding to test modules should be straightforward and can be automated with rector.

See \Rector\Config\Level\TypeDeclarationLevel::RULES for a list of rules. The array index correlates to the level, so 22 gives us all the way up to ReturnNullableTypeRector::class. This is the first level before rector starts to modify param types, which is out of scope here. There are a few other exceptions/additions documented in the proposed resolution.

This removes 409 of the errors, and reduces the baseline by 2454 lines.

Steps to reproduce

$ grep "missingType.return" core/.phpstan-baseline.php | wc -l
7866

Proposed resolution

Use rector with type coverage on core/modules/*/tests/modules.

Install rector

$ composer require --dev rector/rector

Configure rector.php file

<?php
use Rector\TypeDeclaration\Rector\ClassMethod;
return Rector\Config\RectorConfig::configure()
  ->withPaths([
    __DIR__ . '/core/modules/*/tests/modules',
    __DIR__ . '/core/modules/config/tests/config_*',
    __DIR__ . '/core/modules/dynamic_page_cache/tests/dynamic_page_cache_test',
    __DIR__ . '/core/modules/file/tests/file_*',
    __DIR__ . '/core/modules/filter/tests/filter_*',
    __DIR__ . '/core/modules/language/tests/language_*',
    __DIR__ . '/core/modules/language/tests/test_module',
    __DIR__ . '/core/modules/menu_link_content/tests/menu_*',
    __DIR__ . '/core/modules/menu_link_content/tests/outbound_*',
    __DIR__ . '/core/modules/navigation/tests/navigation_*',
    __DIR__ . '/core/modules/node/tests/node_*',
    __DIR__ . '/core/modules/options/tests/options_*',
    __DIR__ . '/core/modules/serialization/tests/serialization_test',
  ])
  // First level before param and property types are affected.
  ->withTypeCoverageLevel(22)
  ->withSkip([
    // Skip as this adds unwanted phpdoc changes.
    ClassMethod\ReturnTypeFromStrictNewArrayRector::class,
  ])
  // Additional rules above level 22 that are useful
  ->withRules([
    ClassMethod\ReturnTypeFromStrictTypedCallRector::class,
    ClassMethod\ReturnUnionTypeRector::class,
    ClassMethod\ReturnTypeFromStrictParamRector::class,
    ClassMethod\ReturnTypeFromStrictFluentReturnRector::class,
    ClassMethod\ReturnNeverTypeRector::class,
  ]);

Run rector
$ ./vendor/bin/rector

Fix code style
$ composer phpcbf

Update baseline
$ ./vendor/bin/phpstan analyse -c core/phpstan.neon.dist --generate-baseline=core/.phpstan-baseline.php --memory-limit=2G

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3550884

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mstrelan created an issue. See original summary.

mstrelan’s picture

Issue summary: View changes

mstrelan’s picture

Issue summary: View changes
Status: Active » Needs work

This has some failing tests, so obviously need to revert or adjust some of the changes.

mstrelan’s picture

Status: Needs work » Needs review

Fixed the fail, was just one method that didn't want to update.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Spot checked and where I looked it all looked good. Tests are green which is indication there are no type variances in hierarchies; besides, these being test modules if tests pass it’s per se indication that all is good.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

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.

mondrake’s picture

Inspired by this (thank you!), I have opened #3551596: Add return types to core/tests code via Rector - round 1 to try tackling test code.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

mstrelan’s picture

sivaji_ganesh_jojodae made their first commit to this issue’s fork.

mstrelan’s picture

@sivaji I think we should split this up. I've already opened one child ticket, maybe can add others similar to that.