Problem/Motivation

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

My suggestion would be to do, for core/tests:

Proposed resolution

Use rector with type coverage on core/tests.

Install rector

$ composer require --dev rector/rector

Configure rector.php file

<?php

declare(strict_types=1);

use Rector\TypeDeclaration\Rector\ClassMethod;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;

return Rector\Config\RectorConfig::configure()
  ->withBootstrapFiles([
      __DIR__ . '/vendor/palantirnet/drupal-rector/config/drupal-phpunit-bootstrap-file.php',
  ])
  ->withPaths([
    __DIR__ . '/core/tests',
  ])
  ->withPreparedSets(typeDeclarations: true)
  ->withSkip([
    TypedPropertyFromCreateMockAssignRector::class,
    TypedPropertyFromDocblockSetUpDefinedRector::class,
    TypedPropertyFromAssignsRector::class,
    TypedPropertyFromStrictConstructorRector::class,
    TypedPropertyFromStrictSetUpRector::class,
  ])
  ->withImportNames(
    importDocBlockNames: false,
    importShortClasses: false,
    removeUnusedImports: false,
  );

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-3562361

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

mondrake created an issue. See original summary.

mondrake’s picture

Issue summary: View changes

quietone’s picture

Version: 11.3.x-dev » 11.x-dev
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Status: Active » Needs review

Created the patch as per the IS, then fixed manually a few errors until green.

The only change that goes a bit outside of pure typing is in core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php where I simplified access to a protected property using reflection, since the addition of typehints to the closure was causing PHPStan complaints - I think it's more readable now anyway.

mondrake’s picture

Issue summary: View changes
mondrake’s picture

Issue summary: View changes
smustgrave’s picture

Is there concern for breaking contrib with typehints on test traits?

dcam’s picture

To reduce the amount of work required to review this I assumed that any individual scalar hint was valid. Also any union of two scalar types. Even then, it took 2-3 hours to go through all the changes, in particular because of the browser lag in GitLab. I focused on checking the unions of multiple types, particularly those that involved classes.

I commented on the unions with 5+ types. I also added notes about a couple of other things I found.

mondrake’s picture

Thanks for your review @dcam!

Replied inline, made some changes, reverted changes to Doctrine annotation test fixtures.

dcam’s picture

I don't know the answer to #9. I planned to search all contrib code for the traits, but for some reason I can't. Traits that have functions with new parameter typehints are:

  • core/tests/Drupal/KernelTests/AssertConfigTrait.php
  • core/tests/Drupal/KernelTests/AssertContentTrait.php
  • core/tests/Drupal/Tests/Composer/Plugin/ExecTrait.php
  • core/tests/Drupal/Tests/Composer/ComposerIntegrationTrait.php
  • core/tests/Drupal/Tests/RandomGeneratorTrait.php
  • core/tests/Drupal/Tests/UiHelperTrait.php

Otherwise this is looking good to me.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

This will need several re-rolls I'm sure but lets see if we can get it landed. Thanks!

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.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Rebased.

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.

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.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Rebased

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new57.08 KB

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.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +no-needs-review-bot
dcam’s picture

It needed a rebase due to changes in core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Avoid commit conflicts

Needs another rebase.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community

rebased

  • catch committed ccf3943d on main
    task: #3562361 Add return types to core/tests code via Rector - round 2...
catch’s picture

Title: Add return types to core/tests code via Rector - round 2 » Add type hints to core/tests code via Rector - round 2
Status: Reviewed & tested by the community » Fixed

Changing the title because this also adds param type hints. I noticed that in some cases, rector is adding param type hints and not return type hints, however I think we can just add those in another issue later, main thing here is to tighten things up and reduce the baseline. Because it's test code it's easier to make smaller changes later.

Committed/pushed to main, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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