Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Brainstorm: How to document __get() + @property $xyz + 'calc_xyz()' pattern? #1709

@donquixote

Description

@donquixote

A pattern I use quite a lot in PHP is this:

abstract class ContainerBase {

  private $buffer = array();

  function __get($key) {
    return array_key_exists($key, $this->buffer)
      ? $this->buffer[$key]
      : $this->buffer[$key] = $this->{'calc_' . $key}();
  }
}

/**
 * @property \X $xyz
 */
interface MyContainerInterface {}

class MyContainer extends ContainerBase implements MyContainerInterface {

  /**
   * @return \X
   * 
   * @see \MyContainerInterface::$xyz
   */
  protected function calc_xyz() {..}
}

I made it a habit to always add the @see tags to associate a method with a property. This is kind of ok, but requires too much work to set up, and the IDE has no way to validate it.

I wonder how this could be documented, to allow the IDE to

  • Complain if a 'calc_*()' method for a specific property is missing, and propose to auto-create it.
  • Complain if the @Property for a given 'calc_*()' method is missing, and propose to auto-create it.
  • Complain if the return type of 'calc_*()' does not match the property type.
  • Allow to easily navigate between the @Property and the method.
  • Of course, it should be possible to use a different prefix than 'calc_'. E.g. 'get_' or 'create_'.

(Proposals in comments)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions