Problem/Motivation

Following on from #3203611: Wrong typehint in \Drupal\field\Entity\FieldConfig::loadByName, the documented return type of \Drupal\field\Entity\FieldConfig::loadByName is still not exactly correct.

Proposed resolution

The return type should be fully qualified, to not cause tooling like PHPStan to blow up, and PHP Storm to report errors.
Change the return doc to

@return static|null

Issue fork drupal-3251856

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

dpi created an issue. See original summary.

dpi’s picture

jibran’s picture

Priority: Normal » Minor
Status: Active » Reviewed & tested by the community
Issue tags: +Novice, +Quickfix

Thank you for your contribution. This is a huge improvement for the Drupal documentation.

dpi’s picture

Assigned: dpi » Unassigned

Thank you I appreciate that you appreciate me smiley face

navneet0693’s picture

Status: Reviewed & tested by the community » Needs work

I think the correct namespace is \Drupal\Core\Field\FieldConfigInterface

navneet0693’s picture

Status: Needs work » Needs review
StatusFileSize
new590 bytes
dpi’s picture

Maybe this should be @return static, since this class should implement both interfaces, but neither interface extend eachother.

An intersection type is also possible, but ugly.

dpi’s picture

Status: Needs review » Needs work
elber’s picture

Assigned: Unassigned » elber
elber’s picture

Assigned: elber » Unassigned
Status: Needs work » Needs review
StatusFileSize
new89.43 KB
new89 KB
dpi’s picture

Status: Needs review » Needs work

Patch contains changes unrelated to the issue at hand.

elber’s picture

Assigned: Unassigned » elber
elber’s picture

Sorry can you explain me what's your desire? I don't understand.

elber’s picture

Assigned: elber » Unassigned
Status: Needs work » Needs review
StatusFileSize
new552 bytes
beatrizrodrigues’s picture

Status: Needs review » Reviewed & tested by the community

Seems ok to me, the problem from the previous patch was fixed, and the suggestion that was given at #8 was followed. Patch applied perfectly.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I've tried to understand we what #8 means by

since this class should implement both interfaces, but neither interface extend eachother.

I think the patch in #7 is correct. If static really is correct then it needs to be static|null.

dpi’s picture

@AlexPott

\Drupal\field\Entity\FieldConfig implements both \Drupal\Core\Field\FieldConfigInterface and \Drupal\field\FieldConfigInterface. But both of these FieldConfigInterface dont implement eachother. So it wouldn't be ideal to typehint either interface since you'd miss out on methods.

static works since its a static method that will always be returning an instance of self. Short of doing something like \Drupal\Core\Field\FieldConfigInterface&\Drupal\field\FieldConfigInterface, introducing a new interface, or updating either interface to implement another

Ultimately static|null is what we're aiming for. Not sure why #15 removed the null part.

elber’s picture

Assigned: Unassigned » elber
elber’s picture

Assigned: elber » Unassigned
Status: Needs work » Needs review
StatusFileSize
new557 bytes
hmendes’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

The last patch addresses the comment Ultimately static|null is what we're aiming for. Not sure why #15 removed the null part. and applies perfectly.
Changing it to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

@dpi I guess the question I have is why does Drupal\field\FieldConfigInterface not implement Drupal\Core\Field\FieldConfigInterface. I think it should. It seems an oversight. If anyone has done alternate configurable field implementation is not going to only implement Drupal\field\FieldConfigInterface because it'd need to implement Drupal\Core\Field\FieldConfigInterface too - so let's fix this. By adding Drupal\Core\Field\FieldConfigInterface to Drupal\field\FieldConfigInterface.

This would have the benefit of improving all of the typehints listed on http://grep.xnddx.ru/search?text=Drupal%5Cfield%5CFieldConfigInterface&f...

I guess this happened because of how BaseFieldOverride was carved off from configurable fields during D8 development.

elber’s picture

Assigned: Unassigned » elber
dpi’s picture

@alexpott we have #2818877: field's FieldConfigInterface should extends Core's FieldConfigInterface and #2399301: There should be only one FieldConfigInterface already, which cover history and resolutions.

Even if either interface issue is resolved static|null should still be correct?

alexpott’s picture

Imo this is a dupe of #2818877: field's FieldConfigInterface should extends Core's FieldConfigInterface ... once that is done then the docs for the method are correct - because this calls out to \Drupal::entityTypeManager()->getStorage('field_config')->load() you actually have no guarantee that someone has not swapped out the class for something that does not extend from FieldConfig. However the class that is return should definitely implement both of the FieldConfig interfaces.

elber’s picture

Assigned: elber » Unassigned
Status: Needs work » Needs review
StatusFileSize
new579 bytes

Hello @alexpott I'm sending my patch. But I don't know if I understand what is your objective. Can you explain me again?

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

andregp’s picture

I guess @alexpott point on #25 is right. If #2818877: field's FieldConfigInterface should extends Core's FieldConfigInterface is fixed then the type hint will be correct and the change from MR#1458 will be all we need here.

anjali rathod’s picture

Status: Needs review » Closed (duplicate)

Just checked , the changes mentioned above are present in the code base as the result of MR#1458 .

dpi’s picture

Status: Closed (duplicate) » Active

@29 this doesn't seem correct resolution

shubham chandra’s picture

StatusFileSize
new579 bytes

Added patch against #26 in Drupal 10.1.x

dpi’s picture

28 and 29 suggest all we need is the MR changes, which is the leading slash change.

26 and 31 still dont actually fix the leading slash, differing from the MR by removing null return, which doesnt seem right.

kingdutch’s picture

acbramley’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Active » Postponed (maintainer needs more info)

As of #3322743: Fix PHPStan L2 errors "Parameter $foo of method Foo::bar() has invalid type Foo\Baz." and "Method Foo::bar() has invalid return type Foo\Baz. " (committed a week ago) - the missing leading slash has been added which means the original problem of phpstan complaining about invalid types is now fixed.

Should we close this as outdated?

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

As per above, closing.