Allow usage of stdclass instead of object where it makes sense

Created on 18 October 2022, over 2 years ago
Updated 2 April 2023, almost 2 years ago

Problem/Motivation

\Drupal\Sniffs\Commenting\FunctionCommentSniff auto-corrects stdClass to object but they are different types with different meaning. It is pretty well explained here: https://github.com/phpstan/phpstan/discussions/7009

Steps to reproduce

Minimum code example

/**
 * @param \stdClass $bar
 *
 * @return \stdClass
 */
function foo(\stdClass $bar): \stdClass {
  return $bar;
}

Unexpected changes by PHPCBF

/**
 * @param object $bar
 *
 * @return object
 */
function foo(\stdClass $bar): \stdClass {
  return $bar;
}

Proposed resolution

Remove this behavior

Remaining tasks

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Needs work

Component

Coding Standards

Created by

πŸ‡­πŸ‡ΊHungary mxr576 Hungary

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.

  • πŸ‡¦πŸ‡ΉAustria klausi πŸ‡¦πŸ‡Ή Vienna

    This has now been implemented in Coder in ✨ stdclass is auto-corrected to object but it should not be Fixed .

    Raising priority to major as now the documented coding standards are not in sync with Coder.

    Next step: define the wording and necessary changes to coding standards documentation pages in the issue summary.

  • Status changed to Active 13 days ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    use the coding standard template

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

    The next step here is to complete the proposed resolution section.

  • πŸ‡ΊπŸ‡ΈUnited States dww

    So yeah, we'd need to update this: https://www.drupal.org/docs/develop/standards/php/api-documentation-and-... β†’

    To deal with this:

    object (NOT "stdClass")

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

    Adding link to current text and the current text.

    Anyone know what 'stdClass' was excluded?

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

    A bit more accurate title

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

    I think I would rather disallow type-hinting with \stdClass at all to resolve this, then it not being allowed in the PhpDoc would not be a problem. Type-hinting on \stdClass seems like bad practice to me and it should be discouraged if it isn’t yet in our standards.

    Anyone know what 'stdClass' was excluded?

    Seems like this was written in a time when Drupal mostly used anonymous objects and we just wanted to document those as object in the PhpDoc, not as \stdClass. Actual type hints were not allowed in PHP at that time, so the question of a discrepancy between PhpDoc and in-code type hint never arose.
    And we probably couldn’t think of a good reason to actually require a \stdClass object instead of any object with the required (public) properties – same as I do now.

Production build 0.71.5 2024