If you want me to push this to Drupal core: base: coding standards please just say so, but I want a record keyed to Coder because I would like the rule changed in Coder (assuming it is changed in the standards).

Example:

  /**
   * Tags that may not have children (are self-closing).
   * 
   * @see http://www.standardista.com/html5/xhtml-elements-their-parent-children-attributes-and-default-browser-presentation/
   * @var array
   */
  static public $TAGS_WITH_NO_CHILDREN = array(
    'br',
    'hr',
    'img',
    'base',
    'link',
    'meta',
    'area',
    'col',
    'param'
  );

Coder output:

ERROR   | Class property $TAGS_WITH_NO_CHILDREN should use lowerCamel
     |         | naming without underscores

Using CAPITALS_WITH_UNDERSCORES for static class variables that act as constants is a common convention in many OO languages, especially Java. Examples:

- http://java.about.com/od/javasyntax/a/nameconventions.htm

- http://www.appperfect.com/support/java-coding-rules/namingconvention.html

Please consider at least permitting this variation for static class variables in the Drupal coding standards (and thus in Coder).

Comments

webel’s picture

The Drupal JavaScript coding standards docs state:

Constants

Pre-defined constants SHOULD be all-upercase and words separated by underscores: UPPER_UNDERSCORED.

I am not expressing my preference, I am just pointing out that it is a common convention, and should at least be permitted in PHP for static variables that act as constants.

If one does not use CAPITALS_WITH_UNDERSCORES the question remain how one can easily distinguish static from not-static class variables.

webel’s picture

More interesting Java examples.

From Stackoverflow: http://stackoverflow.com/questions/1417190/should-a-static-final-logger-be-declared-in-upper-case:

Case:
private static final Logger logger = Logger.getLogger(MyClass.class);


The logger reference is not a constant, but a final reference, and should NOT be in uppercase. A constant VALUE should be in uppercase.

private static final Logger logger = Logger.getLogger(MyClass.class);

private static final double MY_CONSTANT = 0.0;

The Java Coding Style Guide states this in paragraph 3.3 Field Naming

Names of fields being used as constants should be all upper-case, with underscores separating words. The following are considered to be constants:

- All static final primitive types (Remember that all interface fields are inherently static final).

- All static final object reference types that are never followed by "." (dot).

- All static final arrays that are never followed by "[" (dot).

Examples:

MIN_VALUE, MAX_BUFFER_SIZE, OPTIONS_FILE_NAME

Following this convention, logger is a static final object reference as stated in point 2, but because it is followed by "." everytime you use it, it can not be considered as a constant and thus should be lower case.


klausi’s picture

Component: Review/Rules » Coder Sniffer
Status: Active » Closed (works as designed)

Hm, in your example you are using a class property which is not a constant so the constant rules do not apply to it? Arrays cannot be declared as constants, so I guess you cannot use the "const" keyword. So this seems to be a special case - not sure what we should do about it?

Drupal 8 core uses lowerCamel naming for public static variables, example in core/lib/Drupal/Core/Language/Language.php. Also the public static $modules variable used in many test cases suggests that the rule "Methods and class properties should use lowerCamel naming." applies here.

So my interpretation is that this is the current standard and Coder behaves correctly according to it. So I'm closing this for now, please reopen if this changed through a core issue and in the coding standards.

webel’s picture

@klausi wrote:

.. in your example you are using a class property which is not a constant so the constant rules do not apply to it?

In some cases I am using (misappropriating) static class variables in PHP as constants that can be post-initialized (such as to apply Drupal t() translation to a shared string), noting that - unlike in Java - one can't apply final to static class variable in PHP, only to methods. If I use CAPITALS_WITH_UNDERSCORES it makes it clearer that they are acting like constants. One can't in this case use a const, because in PHP5.3 it can't be initialized with the result of a computation, and it can't be post-initialised. Please see new examples under my comment at Coder: This requirement is at odds with OO encapsulation: WARNING | Only string literals should be passed to t() where possible.

webel’s picture

Category: Feature request » Support request
Status: Closed (works as designed) » Needs review

Reopened as support request, please close again on reading https://www.drupal.org/node/2306473#comment-9039965. Feedback welcome.

klausi’s picture

Status: Needs review » Fixed

Responded there!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.