$drupal_user = User::create();
$drupal_user->setUsername($username);

In example above we will have $drupal_user variable of \Drupal\Core\Entity\EntityInterface type and will not have an autosuggestion feature which is provided by a lot of editors/IDEs.

Proposition is to add PHPDoc annotations to entity definitions with overridden return type.

Example:

/**
 * @method static User load($id)
 * @method static User create(array $values = [])
 * @method static User[] loadMultiple(array $ids = NULL)
 * @method User createDuplicate()
 */
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

BR0kEN created an issue. See original summary.

BR0kEN’s picture

Assigned: BR0kEN » Unassigned
Status: Active » Needs review
FileSize
579 bytes

Status: Needs review » Needs work

The last submitted patch, 2: user-override_phpdoc-2794733-2.patch, failed testing.

The last submitted patch, 2: user-override_phpdoc-2794733-2.patch, failed testing.

dpi’s picture

Status: Needs work » Closed (won't fix)

This issue seems is with better IDE intelligence.

In recent versions of PHPStorm, returning static in an interface will correctly hint to the called class.

$user = \Drupal\user\Entity\User::create();
// PHPStorm interprets $user as a \Drupal\user\Entity\User
$user->[hints correctly]
$user->setUsername(); //etc

Related: https://youtrack.jetbrains.com/issue/WI-19953, https://youtrack.jetbrains.com/issue/WI-23435

dpi’s picture