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;
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
Comments
Comment #2
mondrakeComment #4
mondrakeComment #5
nicxvan commentedI think these are great, I wonder if we can document these rector rules for contrib somewhere central.
Comment #6
dcam commentedI left feedback in the MR.
I didn't make a suggestion for the first couple of
?objectreturn types because in both instances a newusestatement will be necessary too.It made a bunch of lowercase `null` insertions. Not sure what was up with that. I ended up grepping the diff file to find them all. There should be suggestions for all of them.
Aside from those things, this looks ok to me. I read through the entire diff, but I didn't verify every single return type. I assume that the test would blow up with a fatal error if one happened to be wrong.
Comment #7
nicxvan commentedIs this a beta target change too?
Comment #8
mondrakeThank you!
The annotation code is under phpcs ignore so that's why, I assume, phpcbf does not make fixes there.
I really wish we had #3439962: Stop using uppercase TRUE, FALSE, NULL literals. IMHO, spending time on this is waste of resources.
Comment #9
needs-review-queue-bot commentedThe 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.
Comment #10
mondrakeComment #11
dcam commentedFeedback has been addressed. The additional changes to qualifying
MockObjectslook good to me too. And the tests are green. So I'm marking this one as RTBC.Comment #12
mondrakeLet's call this issue 'round 1'. A 'round 2' issue would increase type coverage level to max (I checked and that will add a lot more goodness, including argument typing based on inference). 'Round 3' or more would be a manual check/refactor of the leftovers at that stage.
Comment #13
mstrelan commentedThis may be a duplicate or could be a child issue of #3455548: [meta] Add return typehinting to the test codebase or #3455549: Add return typehints to protected test helper methods.
I think the key consideration in that meta is "first where non-disruptive, then where possibly disruptive (base classes, traits, etc.)"
Comment #14
quietone commentedI do think the parent should change. The parent issue here is for test modules but this is for core/tests so I am changing the parent.
Comment #15
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 #16
nod_Comment #17
mondrakerebased and updated baseline, back to RTBC
Comment #18
longwaveNeeds rebase.
Comment #19
mondrakeRebased and reverted changes to DocParserTest, back to RTBC.
Comment #20
mondrakeComment #21
mondrakeComment #22
longwaveTwo nits about a return type that should only be a string, and also why is this reverted/added to the baseline?
Comment #23
longwaveComment #24
mondrakeserializeDefinition(array $service_definition)returns a string inOptimizedPhpArrayDumperTestand an array in its childPhpArrayDumperTest, which is quite weird for a serialization function. However, not in scope here to change the test logic, so typed the return value of both tostring|arrayto catch all.Comment #25
smustgrave commentedWould be good to land this one before needs another rebase.
Comment #26
longwaveThis is a good time to get sweeping fixes like this in.
Committed and pushed 73792d7654a to 11.x and dc40b4e721f to 11.3.x. Thanks!