Problem/Motivation

With the current typehinting an IDE can't provide proper autocompletions for methods and other inspections.

Steps to reproduce

class C1 {
  protected \Drupal\node\NodeStorageInterface $nodeStorage;

  public function m1() {
    foreach ($this->nodeStorage->loadMultiple() as $node) {
      $node->isPromoted();
    }
  }
}

The problem is that several method signature are defined in the EntityStorageInterface or ConfigEntityStorageInterface or ContentEntityStorageInterface.
The implementer classes use {@inheritdoc} for ::load() and for other methods as well.
That means an IDE still thinks that the return value of a NodeStorageInterface::load() is null|EntityInterface instead of null|NodeInterface
That is why an IDE can't provide more accurate help.

Proposed resolution

Add @method PhpDoc entries for every EntityStorageInterface descendants.

Issue fork drupal-3323252

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

Sweetchuck created an issue. See original summary.

longwave’s picture

edit: ignore, wrong issue

sweetchuck’s picture

Status: Active » Needs review
smustgrave’s picture

Think that looks correct but will wait for another +1 RTBC

quietone’s picture

How does the API module handle @method, since @method is not part of the Drupal coding standards?

Ah, there is one PHP file using @method, \Drupal\Component\Datetime\DateTimePlus. It looks like this in the API docs. That is unreadable.

Looking in the Coding standards project. Yes, there is an issue, #2920333: Allow PHPDoc @method annotations in class headers.. That needs to be resolved first before changing core code. If approved, then changes to core core would not be made by single files but by some sensible, larger scope.

In summary, this is a won't fix and effort should go into the issue in the Coding standards project.

smustgrave’s picture

So should we close this?

joachim’s picture

Status: Needs review » Postponed (maintainer needs more info)

I don't understand what is needing to be fixed here.

I copy-pasted the sample code from the IS into my IDE (VSCode) and loadMultiple() is picked up and showing me the documentation for that method from EntityStorageInterface.

sweetchuck’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs review
sweetchuck’s picture

@joachim Yes, the ::loadMultiple() is picked up, but the problem is that the $node->isPromoted() is not.

sweetchuck’s picture

@quietone I don't understand why we can't use @method now, and enjoy the benefits.
Why do we have to wait for "API docs" project?
Patch #3 is green. That means there is nothing against the coding standard.

quietone’s picture

It is not just API docs project, there is also an existing Coding Standards issue that is still open about adding @method.

On a practical level, we should not be adding API documentation that will look like this on api.drupal.org.

@method $this add(\DateInterval $interval) @method static array getLastErrors() @method $this modify(string $modify) @method $this setDate(int $year, int $month, int $day) @method $this setISODate(int $year, int $week, int $day = 1) @method $this setTime(int $hour, int $minute, int $second = 0, int $microseconds = 0) @method $this setTimestamp(int $unixtimestamp) @method $this setTimezone(\DateTimeZone $timezone) @method $this sub(\DateInterval $interval) @method int getOffset() @method int getTimestamp() @method \DateTimeZone getTimezone()

smustgrave’s picture

Status: Needs review » Postponed

Postponing on #2920333: Allow PHPDoc @method annotations in class headers. though there hasn't been movement on that in 5 years so not super promising.

sweetchuck’s picture

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.

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.