Problem/Motivation
In #2941148: Fix Drupal.Commenting.FunctionComment.MissingReturnType return types were fixed. But this change NULL returns to return; whereas the return in the docblock is NULL. Perhaps we should change those to be explicit NULL instead of current implied NULL.
Steps to reproduce
Proposed resolution
Remaining tasks
- Decide if we want to change this
- ...
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3312049
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 #4
sweetchuckIf we ever want to use proper typehints for function/method parameters and return value, and
declare(strict_types = 1);, then the answer is yes.Comment #5
longwaveComment #6
sweetchuckdeclare(strict_types=1);doesn't affect this behaviour.Comment #7
sweetchuckPhpStorm reported 428 occurrences of this kind of error (not explicitly defined return value).
In the commit 2022-09-27 57b569d I fixed only very few of them.
Several of them would be straightforward as well, like the diff between the
my_nullable_string_typehint_fail
and
my_nullable_string_typehint_ok
see comment #4
But there are quite a few tricky ones.
\Drupal\Core\Template\AttributeValueBase::render returns string only in a certain condition, otherwise returns null – not explicitly, with typehint it would throw a "Fatal error: Uncaught TypeError" –, but according to the PHPDoc it always returns a string.
There are several function/method like this.
For example: \hook_help(). Even the example code is "wrong", so all the implementations.
No to mention that \action_help() returns a
\Drupal\Core\StringTranslation\TranslatableMarkupinstance.One more example: \Drupal\views\Plugin\views\field\EntityField::getFieldStorageDefinition
\Drupal\locale\PluralFormula::loadFormulae
PHPDoc @return array, but the actual return value is void.This one also detected by PHPCS Drupal.Commenting.FunctionComment.InvalidNoReturn.
Comment #8
borisson_I disagree that we need to change the returns to satisfy the type here, I think it's perfectly valid to have
return;but the typehint in that case should be@return string|void, so it's a string or nothing, null is not the same as void/nothing.Comment #9
sweetchuck@borisson_ There is no such a thing in PHP:
string|voidhttps://wiki.php.net/rfc/union_types_v2#supported_types
Comment #10
bbralaI think #9 is the best argument that we need to do this anyways. Although it will be a hard to task to get right everywhere. We probably need to split this task up into manageable steps.
In my humble opinion I don't think there are resons not to do this, at least in the methods that seem like they are simple to fix.
I do think there is merit to have some more eyes on this though, in a way we would be changing contracts. But you could argue we are just being more explicit on contracts.
Comment #11
bbralaOne thing to note.
So it shouldn't break anything that checks return values.
By dwi on slack.
Comment #12
kingdutchI shared this in Slack but never got around to posting to this thread. With regards to whether we should or should not enforce a final
return NULL;, I believe we should.I think we should just follow the wider PHP community and be explicit in our final NULL returns. PHPStan already warns about this exact scenario.
https://phpstan.org/r/848a5b50-4525-415e-a7aa-cadfea465095
Even if I follow the initial hint of "remove NULL because it never returns NULL" then I get "Function test() should return string but return statement is missing." which also indicates that I forgot to think about something.
https://phpstan.org/r/9a0087a9-05ae-47d9-bc25-da93dc1f7cae
Adding a
return NULLat the end indicates that I've thought about my return types and the code is complete. If this should never return NULL then apparently$bnot being always TRUE is an error and I should end with throwing a helpful exception instead.Comment #13
sweetchuck@Kingdutch : Thanks for the comment. :-)
I started to work on this, but I have concerns:
For example:
By adding
return NULL;won't change that how the code behaves, and it is still won't match to the PhpDoc.Is it part of the scope to change
@return object|falseto@return object|null?I think yes, It would be good to clarify in advance.
Comment #14
sweetchuckOther question is how to solve the simple ones.
Original:
Style 1
Style 2
Comment #16
sweetchuckComment #17
smustgrave commentedCan the MR be updated for 11.x please.
11.x is the current development branch, so code has to be committed there first to be backported.
Comment #19
sweetchuckComment #20
smustgrave commentedSo searching for " but return statement is missing" there still appear to be 89 instances in phpstan-baseline. Were those left out specifically?
Comment #21
sweetchuckGive them a closer look.
For example this one: \Drupal\Core\Database\Query\Delete::execute
Comment #22
sweetchuckComment #23
smustgrave commentedThink it needs to be documented why we are skipping these 89 instances
Some functions are
save(),
getFormId(),
validateForm(),
etc
If the inherited docs say it returns a value and we are leaving in the phpstan file think it needs to be stated why.
Comment #24
quietone commentedComment #26
dcam commentedI closed #3561384: ModerationInformationInterface::getDefaultRevisionId missing typing as a duplicate of this one.
Comment #27
dcam commented