phpphpstormdocblocks

How to make PHP getter/setter inherit DocBlock from their field?


How can I make getter/setter methods to automatically use DocBlock description from their respective fields? Or maybe there are any other ways to solve this: see DocBlocks on getters/setters without manually copy-pasting every single one.

Consider this example:
enter image description here

There is some comment on field $name (1), however, it will almost never be seen as the field is private and intended to be used with getter/setter methods. However, on those methods IDE does not show any comments from field variables (2).

You can, of course, simply copy-paste same description to both methods. But it becomes very difficult to manage when object has a dozen or more properties, all with getters/setters and extensively commented. It requires a lot of manual work to fix the same text in 3 places on every change and is very error-prone.

IDE is PhpStorm.
I'd prefer to do it without magic __get __set methods.


Solution

  • The only option I know is using @see or @link. We can use them inline as well. This is how it looks in Phpstorm:

    https://i.sstatic.net/bfSg4.png

    The link in the description is clickable. Without a mouse, we can use a tab key to move a cursor on the link (3 x tab and enter).

    First popup

    Final view:

    Final popup