Problem/Motivation

The documentation gives the incorrect suggestion that user ID are given as integers, when they can be loaded as strings from the database.

Steps to reproduce

Load entity from database, observe string response:
type error

Proposed resolution

Change the phpdoc to reflect the possible types.

Remaining tasks

-

User interface changes

-

API changes

-

Data model changes

-

Release notes snippet

-

Issue fork drupal-3218667

Command icon 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

grahl created an issue. See original summary.

grahl’s picture

Title: EntityOwnerInterface » EntityOwnerInterface getOwnerId documents wrong return types
Issue summary: View changes
StatusFileSize
new117.8 KB
new1.36 KB
joachim’s picture

I 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.

sakr’s picture

I 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 .

grahl’s picture

To 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:

<?php

function test(): ?int {
  return 'test';
}
var_dump(test());
# Fatal error: Uncaught TypeError: Return value of test() must be of the type int or null, string returned in 

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.

joachim’s picture

What I meant by the implicit cast is something like this:

<?php
function test(): int {
  return '4';
}
var_dump(test());

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.

grahl’s picture

StatusFileSize
new896 bytes

Ah, sorry, I see where my confusion was, that was dumb.

Strict typing still breaks it though:

<?php

declare(strict_types=1);

function test(): ?int {
  return '4';
}
var_dump(test());

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.

Status: Needs review » Needs work

The last submitted patch, 7: 3218667-7-getownerid-int.patch, failed testing. View results

praveen saini’s picture

Status: Needs work » Needs review

#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.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work

Seemed last patch had some failures.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hitchshock’s picture

Priority: Minor » Major
Status: Needs work » Needs review

Definitely, 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

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The 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.)

samit.310@gmail.com made their first commit to this issue’s fork.

prashant.c’s picture

Status: Needs work » Needs review
prashant.c’s picture

Just wondering, when this returns string and when it returns intvalue. Thinking in the context of why we need have both the return types intand stringalso.

smustgrave’s picture

Status: Needs review » Needs work

I 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.

prashant.c’s picture

Should we cover the bug in this issue itself or create a new one?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.