Add return types to core tests code via Rector

Created on 11 October 2025, about 1 month ago

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.

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;

return Rector\Config\RectorConfig::configure()
  ->withPaths([
    __DIR__ . '/core/tests',
  ])
  // First level before param and property types are affected.
  ->withTypeCoverageLevel(22)
  ->withSkip([
    // Skip as this adds unwanted phpdoc changes.
    ClassMethod\ReturnTypeFromStrictNewArrayRector::class,
    ClassMethod\ReturnTypeFromMockObjectRector::class,
    TypedPropertyFromCreateMockAssignRector::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

📌 Task
Status

Active

Version

11.0 🔥

Component

other

Created by

🇮🇹Italy mondrake 🇮🇹

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024