Add @see comments on function name strings, to facilitate "find usages" in IDEs

Created on 11 October 2016, over 8 years ago
Updated 18 February 2025, about 2 months ago

See also πŸ“Œ Use C::class instead of 'C' for strings holding class names, to facilitate "find usages" in IDEs. Needs work .

Problem

Sometimes function names are specified as strings.

In some cases, the IDE (*) can actually figure out that these are function names. E.g. in the snippet below, the IDE understand that function_exists() has a parameter which is a function name.

if (function_exists('foo')) {
  [..]

In other cases, there is really no way for an IDE to know that a string is a function name.

function hook_menu() {
  $items['example'] = array(
    'title' => 'Example Page',
    'page callback' => 'example_page',
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

(this is Drupal 7, but we might still have code like this in Drupal 8).

(*) When I say IDE I usually talk about the IDE that I use myself, which is PhpStorm. In general, we should assume that IDEs evolve over time, but that there are limitations for how far they can go.

Proposed solution

Put a @see comment in the line above the string, like so:

function hook_menu() {
  $items['example'] = array(
    'title' => 'Example Page',
    /* @see example_page() */
    'page callback' => 'example_page',
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

With this added @see comment, the IDE can now
- "find usages" for function example_page().
- navigate to the function if you click the @see comment.

I am using the /* */ comment syntax instead of inline // comment syntax, because my IDE ignores the @see if it is in //. I had a discussion about this somewhere in the PhpStorm issue queue.

The exact syntax might require some bikeshedding. And maybe some part of the discussion should happen elsewhere.

πŸ“Œ Task
Status

Postponed: needs info

Component

Coding Standards

Created by

πŸ‡©πŸ‡ͺGermany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    There hasn't been discussion here for 8 years and the only comment was not supportive.

    If there is interest in pursuing this, add the new issue template and complete the proposed resolution.

    Is there interest in this?

  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    I’m also doing this whenever I remember, and it really does help. And while the use of function strings is definitely getting less commons (as functions are in general), dynamic references to methods have the same problem. But not sure whether it’s worth it to have a standard on this. I guess it depends on the number of actual places this would affect in the code base.

Production build 0.71.5 2024