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
| Comment | File | Size | Author |
|---|
Issue fork drupal-3550884
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
Comment #2
mstrelan commentedComment #4
mstrelan commentedThis has some failing tests, so obviously need to revert or adjust some of the changes.
Comment #5
mstrelan commentedFixed the fail, was just one method that didn't want to update.
Comment #6
mondrakeSpot 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.
Comment #7
needs-review-queue-bot commentedThe 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.
Comment #8
mondrakeInspired by this (thank you!), I have opened #3551596: Add return types to core/tests code via Rector - round 1 to try tackling test code.
Comment #10
mstrelan commentedComment #12
mstrelan commented@sivaji I think we should split this up. I've already opened one child ticket, maybe can add others similar to that.