Needs work
Project:
Drupal core
Version:
main
Component:
user system
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
14 Jun 2021 at 10:28 UTC
Updated:
27 Aug 2024 at 03:51 UTC
Jump to comment: Most recent, Most recent file

Comments
Comment #2
grahlComment #3
joachim commentedI think the intent with saying 'int' in the docs is that the value is something that can be treated as an integer, even if sometimes PHP's handling of types gives you the integer value as a string.
Presumably when we add PHP return types for Drupal 10, we'll change this to
getOwnerID(): int;and then PHP will cast the string to an int.Comment #4
sakr commentedI am agree with #3
The change in phpdoc that reflect the possible types of return value that may be int or string make confuse, since I do the invocation and waiting result as int value.
PHP do type casting process depend on treatmimg of the value .
Comment #5
grahlTo clarify, this issue came up when trying to use strict typing. It doesn't look like that will get into core generally anytime soon (#3060914: All new Drupal 8/9 code must use strict type hints where possible) but we are encountering this with custom code that is stricter.
That automatic conversion based on the return type would be possible was new to me. I wanted to try that out but couldn't make a test work. So I think an explicit cast is necessary to get that, even without strict typing being active. Here's the example, if you think I missed something:
So, if we need additional code on top of the type hint in D10, the correct return type with the code as-is would have to be "function test(): int|string|null {", which is what the patch proposed for phpdoc.
Comment #6
joachim commentedWhat I meant by the implicit cast is something like this:
That runs fine on PHP 7.4 and 8.
We're never going to return a string that's characters, always numeric.
> but we are encountering this with custom code that is stricter.
If it's a problem that sometimes the return value of this method is a string of numbers rather than an int, then the fix would be to have the method cast the value before returning, rather than tweak the docs.
Comment #7
grahlAh, sorry, I see where my confusion was, that was dumb.
Strict typing still breaks it though:
I presumed that casting the value was less likely to get in since other places in entity API also don't do it but that would definitely be my preferred solution, patch attached.
Comment #9
praveen saini commented#6 agreed with your comment.
The above issue does not propose to fix the type hint. May be there can be another issue, which can be opened for this type hint issue discussion.
As for the above issue, I believe #2 patch can help to tweak the documentation.
And for type hinting I suggest to open an new issue with details for the discussion.
Comment #13
smustgrave commentedSeemed last patch had some failures.
Comment #15
hitchshockDefinitely, this is a major issue, because it can cause mismatching of the result types and can cause mistakes in the conditions.
For example, a condition like
$current_account->id() === $entity->getOwnerId()could be invalid because of type mismatching in the case when it must be valid according to the hin for the function's result type.Also, it can cause similar mistakes in the core, for example #3424593: Media entity owner access check
The patch #2 is more valid at the moment I think, so I hide the patch #7.
The patch #7, in general, is a good idea, but we need to recheck and fix all issues that it will cause, like test failures.
My opinion is to merge patch #2 ASAP to not confuse developers and to give a correct type hint and create a separate ticket for #7
Set a ticket to RTB to hurry up the process a bit
Comment #16
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #19
prashant.cComment #20
prashant.cJust wondering, when this returns
stringand when it returnsintvalue. Thinking in the context of why we need have both the return typesintandstringalso.Comment #21
smustgrave commentedI don't believe it's suppose to return a string, that seems like a bug. Don't think we should update the docs to be wrong to just to cover the bug that's currently there.
Comment #22
prashant.cShould we cover the bug in this issue itself or create a new one?