Updated: Comment #7

Problem/Motivation

Drupal 8 allows the use of Traits, since it is adopting PHP 5.4. We need to define standards for naming and documenting them.

Proposed resolution

a) All Traits should have names ending in "Trait" (just like interfaces end in "Interface"). This should be added to
https://drupal.org/node/608152

[Note that existing patches adding Traits appear to be following this standard.]

b) Documentation standard to be added to https://drupal.org/node/1354

- A PHP 5.4 Trait should have an @see reference to any interfaces whose methods it implements in the main trait documentation header block. (Some Traits do not implement interfaces, in which case this can be omitted.)

 ...
 * @see \Full\Namespaced\Interface\Name
 */
trait MyTraitName {

- Trait methods that provide standard implementations for methods from an interface should be documented like this:

/**
 * Implements \Full\Namespaced\Interface\Name::methodName().
 */
 function methodName() { }

- Trait methods that do not implement an interface but rather are "helper" methods should be documented like any other class methods. However, if there is a method on another class or interface that has the same purpose, parameters, and return value, you can reference that method's documentation like this, rather than re-documenting everything in the Trait's method:

   /**
    * (Standard one-line description of method here.)
    *
    * See \Full\Namespaced\Class\Name::methodName() for details.
    */
   function methodName() {}

Remaining tasks

1. Agree on standards.
2. Update standards docs.

User interface changes

None.

API changes

None.

Original report by @EclipseGc

We now have the ability to begin using php 5.4.x. This gives us the ability to leverage traits, however traits don't implement interfaces, so our standard {@inheritdoc} seems unlikely to solve our typical method documentation problems. We need to decide how we want to solve this. The easiest solution seems to be to use the same documentation standard we were using earlier in the 8.x life cycle of:


 /**
   * Implements \Drupal\Core\Thingy\ClassInterface::method().
   */

In addition to this though, it seems likely that we will utilize abstract methods that also implement methods from an interface. I don't know if we'd want to document those any differently, but I think it's worth calling out that this distinction exists and we should decide up front how to handle it.

Eclipse

Comments

EclipseGc’s picture

jhodgdon’s picture

Title: Documenting trait methods from an interface » [policy, no patch] Figure out how to document trait methods
Issue tags: +Coding standards

Setting up title/tags, as this is a coding standards (or at least documentation standards) decision.

So... Why is it that you think inheritdoc is not the right solution? It seems as though the class has a "use" statement for the trait, so it should be able to inherit the docs for the trait, just like it can for the interface? In theory, anyway.

The API module and/or Grammar Parser will need to be updated for this too, I'll file a separate issue for that.

damiankloip’s picture

This is for methods on the actual trait. Not overriding methods. E.g a trait could help implement methods on the interface for the class its included in, using that interface - Not implemented by the trait.

jhodgdon’s picture

Status: Active » Needs review

Oh... I see. Obviously, I know very little about Traits. :)

In that case, I think the proposed solution is about all we can do. That will at least link to the method that is being implemented by the trait.

So, here is a proposal for what to put on https://drupal.org/node/1354#classes:

------
- A PHP 5.4 Trait should have an @see reference to any interfaces whose methods it implements in the main trait documentation header block.

 ...
 * @see \Full\Namespaced\Interface\Name
 */
trait MyTraitName {

- The methods of the trait should be documented like this:

/**
 * Implements \Full\Namespaced\Interface\Name::methodName().
 */
 function methodName() { }

----

EclipseGc’s picture

This seems super sane to me. I really like the @see statement addition.

Eclipse

jhodgdon’s picture

Something else to consider. We have a standard that all Interface names end with "Interface". Do we need a naming convention for Traits as well, like ending them all in "Trait"?

jhodgdon’s picture

Issue summary: View changes

Incidentally, on #6, it appears that all 3 current issues that are referenced in the parent issue that are proposing new Traits are following this naming convention (ending the name with "Trait"). So it seems that the patch authors think this is a good idea, and we should probably add it to
https://drupal.org/node/608152
as part of this issue.

We probably need an issue summary... done!

jhodgdon’s picture

Status: Needs review » Needs work

I referenced this issue and proposed standard on #2079797: Provide a trait for $this->t() and $this->formatPlural() and was told that the trait being proposed there does not actually implement methods from any interface.

So... We need to think about documentation for that case.

ParisLiakos’s picture

i used the same idea/implementation we had already discussed in #2018411-43: Figure out a nice DX when working with injected translation and onwards

Instead of duplicating documentation in case of just wrapper trait methods just add a
See [namespaced link of parent docs] for details

jhodgdon’s picture

Issue summary: View changes
Status: Needs work » Needs review

#9 seems reasonable to me. Updating issue summary accordingly with this proposal.

ParisLiakos’s picture

issue summary looks great to me!

Crell’s picture

I'm not clear on the use case of point b3 in the issue summary. (Helper method that references out to something else but doesn't fulfill an interface.) Do you have a concrete example?

ParisLiakos’s picture

@Crell: $this->t() is a good example. no point duplicating existing docs of t()

Crell’s picture

Well t() should also just be a dumb wrapper by now. It would be duplicating the docs of TranslationManager::translate(), no? (Or whatever the method is.)

jhodgdon’s picture

RE #14 - the docs for how to use t() are still on the t() function, for whatever reason, but that is a separate issue. The point is that the translation trait is a good example of a "trait that is not implementing a particular interface's methods for the benefit of classes to use", so its methods should not be documented in the same way as a "trait that is implementing...".

Crell’s picture

Ah, I follow now. Rereading that paragraph it makes more sense this time, but I was totally confused by it before. Perhaps restructuring the text to be "If the trait is providing default implementations of an interface, do XYZ. If it's not, do ABC."

jhodgdon’s picture

Issue summary: View changes

Yeah, good point in #16. Slightly tweaked the proposed wording for node/1354 docs in the issue summary.

Crell’s picture

Issue summary: View changes

That reads better, thanks. I made a few language tweaks to help the text flow better. I think it makes sense now.

jhodgdon’s picture

Hm.... I don't really like the changes you made in #18 -- the language is not parallel now, in the section about how to document methods. Now, the first bullet point says "Trait methods that provide standard implementations for methods from an interface ...", and the second one (which is supposed to be the opposite case) still says "Trait methods that do not implement an interface..."; previously they both used the "implement an interface" language and it was (I think?) clearer that they were opposite cases.

So pick one or the other -- they should use parallel language so it's clear that they are two opposite alternatives. I don't care which wording is used -- just pick whichever wording you prefer and put it on both of those bullet points. Having them worded differently will only lead to confusion.

ParisLiakos’s picture

might be offtopic here:

do we need to add a note as well, for when using multiple traits in a class, that they should be separated by a newline?

eg see #1289536-263: Switch Watchdog to a PSR-3 logging framework point 8

Maybe we should expand this thread to account for everything about traits not just the methods?

jhodgdon’s picture

#20 is currently off-topic, since this issue is about naming and documentation only, but if there are other standards to discuss, we could broaden it... but that #8 item in that comment on the other issue seems just like regular coding standards, not specific to traits (about whether there should or shouldn't be a blank line at the beginning of a class body).

Crell’s picture

I've opened a thread on the PHP-FIG list to discuss this as part of PSR-5, as well: https://groups.google.com/forum/#!msg/php-fig/hhcTJp6aWWI/dD2Mt_J5JC4J

jhodgdon’s picture

Project: Drupal core » Drupal Technical Working Group
Version: 8.0.x-dev »
Component: documentation » Documentation

Coding standards decisions are now supposed to be made by the TWG

tizzo’s picture

Project: Drupal Technical Working Group » Coding Standards
Component: Documentation » Coding Standards
joachim’s picture

Title: [policy, no patch] Figure out how to document trait methods » [policy, no patch] Figure out how to document traits and trait methods

I think #20 is out of scope here, as this issue is specifically traits and their methods, not places where a trait is used.

(Tweaking the title, as the issue summary's proposal is about the documentation on the trait as a whole as well as the trait's methods.)

+1 from me, all looks good.