Problem/Motivation

PHP has several modifiers that can be applied to method definitions:
public,protected,private
static
abstract, final

We have not defined the order of those modifiers in the coding standards. In Drupal core we have some typical patterns:
"public" before "static"
"abstract" before "protected"
"public" and "final" mixed

The original proposal here, which became RTBC, used text from PSR-2 which is now deprecated. The new proposal is to use the text from PSR12, section 4.3 and 4.6. 

Benefits

If we adopted this change, the Drupal Project would benefit by ...

Three supporters required

  1. https://www.drupal.org/u/drunken-monkey (2017-03-01)
  2. https://www.drupal.org/u/wim-leers (2017-03-01)
  3. https://www.drupal.org/u/borisson_ (2017-10-18)

Proposed changes

Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:

1. https://www.drupal.org/docs/develop/standards/php/php-coding-standards#v...

Current text

Visibility

All methods and properties of classes must specify their visibility: public, protected, or private. The PHP 4-style "var" declaration must not be used.

The use of public properties is strongly discouraged, as it allows for unwanted side effects. It also exposes implementation-specific details, which in turn makes swapping out a class for another implementation (one of the key reasons to use objects) much harder. Properties should be considered internal to a class.

Proposed text

Visibility

Methods and Functions

Visibility must be declared on all methods.

Method names must not be prefixed with a single underscore to indicate protected or private visibility. That is, an underscore prefix explicitly has no meaning.

Method and function names must not be declared with space after the method name. The closing brace must go on the next line following the body. There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.

Properties

Visibility must be declared on all properties.

The use of public properties is strongly discouraged, as it allows for unwanted side effects. It also exposes implementation-specific details, which in turn makes swapping out a class for another implementation (one of the key reasons to use objects) much harder. Properties should be considered internal to a class.

Extract from PSR-12 section 4.4

abstract, final, and static

When present, the abstract and final declarations must precede the visibility declaration.

When present, the static declaration must come after the visibility declaration.

Extract from PSR-12 section 4.6

Remaining tasks

  1. Create this issue in the Coding Standards queue, using the defined template
  2. Add supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Discussed by the Core Committer Committee, if it impacts Drupal Core/li>
  7. Final review by Coding Standards Committee
  8. Documentation updates
    1. Edit all pages
    2. Not neededPublish change record
    3. Remove 'Needs documentation edits' tag
  9. If applicable, create follow-up issues for PHPCS rules/sniffs changes

For a full explanation of these steps see the Coding Standards project page

Comments

klausi created an issue. See original summary.

klausi’s picture

I think this issue is not really controversial and reflects our current practices anyway, so I went ahead and implemented a rule for Coder to enforce the proposal http://cgit.drupalcode.org/coder/commit/?id=317447a
This will be released with Coder 8.2.11 soon.

drunken monkey’s picture

Issue summary: View changes

+1 for this.
I'd just change the new text a bit to clearly reflect the non-visibility modifiers are optional:

All methods and properties of classes must specify their visibility: public, protected, or private. abstract and final, if present, must be declared before the visibility; static, if present, must be declared after the visibility. The PHP 4-style "var" declaration must not be used.

Also, not sure what our current stance on this is, but don't we want to capitalize the RFC 2119 verbs, at least when making changes to text?

klausi’s picture

I don't like all upper case MUST words, it feels like a document is screaming at me. RFC 2119 is nice to clarify that "must" really means "must", but I would not apply the upper case convention to our more casual coding standards document.

But I don't really care, feel free to change that if it is important to you.

Anonymous’s picture

+1 for this.

Committing to conforming to PSR-2 sounds like a logical progression.

duaelfr’s picture

+1 for standards :)

wim leers’s picture

+1

klausi’s picture

Status: Active » Reviewed & tested by the community
Issue tags: +needs announcement for final discussion

Thanks, with that agreement we can proceed.

bojanz’s picture

Late +1 :)

dawehner’s picture

+1

tobiasb’s picture

+1

borisson_’s picture

Another +1 here.

tizzo’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -needs announcement for final discussion +final discussion

This issue is being included in today's coding standards announcement, updating status and issue tags to follow the defined coding standards ratification process.

andypost’s picture

-1 to "must add public" that's too common to skip it as default in language

borisson_’s picture

-1 to "must add public" that's too common to skip it as default in language

This was already enforced in the coding standards, see also the "old text" in the IS. This is just about adding a consistent order to the other keywords.

NormySan’s picture

+1, and awesome that we can conform to the PSR-standard in this case!

rfulcher’s picture

+1

xjm’s picture

This seems like a good standard since it puts the most important information first. It's also minimally disruptive to adopt:

[ibnsina:drupal | Wed 14:52:10] $ grep -r "static protected" * | grep -v "vendor" | wc -l
      11
[ibnsina:drupal | Wed 14:52:20] $ grep -r "static public" * | grep -v "vendor" | wc -l
       3
[ibnsina:drupal | Wed 14:52:33] $ grep -r "static private" * | grep -v "vendor" | wc -l
       0
[ibnsina:drupal | Wed 14:52:43] $ grep -r "public abstract" * | grep -v "vendor" | wc -l
       0
[ibnsina:drupal | Wed 14:53:19] $ grep -r "protected abstract" * | grep -v "vendor" | wc -l
       0
[ibnsina:drupal | Wed 14:53:27] $ grep -r "public final" * | grep -v "vendor" | wc -l
       0
[ibnsina:drupal | Wed 14:55:00] $ grep -r "protected final" * | grep -v "vendor" | wc -l
       0
claudiu.cristea’s picture

Status: Needs review » Reviewed & tested by the community

Let's do it.

drunken monkey’s picture

*bump*

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review

This issue has been RTBC for a while and in the interim PSR-2 has been deprecated ans PSR-12 is the recommended alternative. This was discussed at a coding standards meeting, [33294885] where there was agreement to use the text from PSR12, section 4.4 and 4.6. borission_ pointed out that in this issue klausi suggested not using uppercase must/should in the coding standards documentation on d.o. There was no disagreement with that and there is none in this issue. And there is an issue specifically addressing that, #1795750: Revise coding standards to use IETF RFC 2119 standards.

I have updated the IS to show the existing text and the proposed new text. Changing the status to NR.

quietone’s picture

Title: Define order of object method modifiers as in PSR-2 » Define order of object method modifiers as in PSR-12

And update the title.

borisson_’s picture

The new text looks great, +1 for inclusion in the coding standards like this.

bbrala’s picture

Agree, +1 to use the psr12 text.

quietone’s picture

This was discussed at a coding standards meeting, #3298982: Coding Standards Meeting 2022-08-02 2100 UTC. The suggested changes were agreed to and also commented here.

I think this now puts this issue at Step 6. I think it is OK to tag this as approved and 'needs documentation updates'.

Now for step 7. This needs to move to the core queue and marked RTBC awaiting "core committer signoff".

quietone’s picture

Project: Coding Standards » Drupal core
Version: » 10.1.x-dev
Component: Coding Standards » other
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Issue moved, set to RTBC and now waiting for core committer signoff.

catch’s picture

Seems fine to me and uncontroversial since we already do it. What's the next step?

alexpott’s picture

As per https://www.drupal.org/project/coding_standards

Drupal.org coding standards pages are updated for approved proposals. Issues are marked "fixed" and “needs documentation updates” tag removed.

We have the text so let's do it.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs documentation updates

Updated coding standard - https://www.drupal.org/docs/develop/standards/object-oriented-code#visib...

Crediting everyone involved.

quietone’s picture

Project: Drupal core » Coding Standards
Issue tags: +Core Committer Approved

Thanks for the approval.

@alexpott, thanks for updating the documentation and credit.

I am learning the CS standard process so checking the steps. Step 7 states "If approved by core committers, the issues should be left in RTBC, tagged with “Core Committer Approved” and moved back to the coding standards project queue." So, I am adding the tag and moving back to the Coding standards project queue. Step 8 was done by alexpott.

That leaves 9, "An announcement of all active discussions for the period will be made providing links to updated coding standards docs where appropriate." We are still working on how and where to make announcements. And, as this is already in practice we can probably forego it in this case.

drunken monkey’s picture

Version: 10.1.x-dev »
Component: other » Coding Standards
Status: Fixed » Needs work

Quote from the new docs (bold added by me for emphasis):

Method and function names must not be declared with space after the method name. The opening brace must go on its own line, and the closing brace must go on the next line following the body. There must not be a space after the opening parenthesis, and there must not be a space before the closing parenthesis.

I really don’t think this was intended or consensus, reading through the comments here and never seeing a single reference to changing this part of our coding standards. Should urgently be revisited, in my opinion. (Probably that whole paragraph should be removed again.)

borisson_’s picture

I agree, that was not intended.

klausi’s picture

alexpott trying to sneak in PSR coding standards into Drupal, I see what you did there :-D

I reverted the doc page for now to not cause confusion.

Next step: Please formulate a better update in the issue summary that does not change the opening/closing brace coding standards for Drupal.

alexpott’s picture

lolz someone tricked me!!!! I just copied and pasted from the issue summary! :D

alexpott’s picture

We also removed

The use of public properties is strongly discouraged, as it allows for unwanted side effects. It also exposes implementation-specific details, which in turn makes swapping out a class for another implementation (one of the key reasons to use objects) much harder. Properties should be considered internal to a class.

Which still seems partially relevant. FWIW I'm a huge fan of readonly public properties but that's a PHP 8.1 only thing.

quietone’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

@drunken monkey, thanks for catching that!

I have updated the IS to remove the text in bold from #35 and to add a 'Properties' section for the re-added paragraph.

The suggested change has "Method names must not be prefixed with a single underscore to indicate protected or private visibility. That is, an underscore prefix explicitly has no meaning." which is already covered in Naming conventions point 4, "Classes should not use underscores in class names unless absolutely necessary to derive names inherited class names dynamically. That is quite rare, especially as Drupal does not mandate a class-file naming match."

Should we keep both?

quietone’s picture

Status: Needs work » Needs review
alexpott’s picture

That is quite rare, especially as Drupal does not mandate a class-file naming match

Well we do actually - that's PSR-0 and PSR-4...

FWIW the naming conventions point 4 are about class names and not method names. So I think the underscore thing and methods is worth keeping.

quietone’s picture

Issue tags: +Needs follow-up

@alexpott, you are correct about #4. My brain must be getting ready early for my holiday.

Adding a follow up tag for #38.

xjm’s picture

Issue tags: -Needs follow-up +Needs followup

 

quietone’s picture

Issue summary: View changes

Just converting to the new template

quietone’s picture

Issue summary: View changes
Issue tags: -Needs followup

Match formatting of the PSR.
Considering this is updating documentation to what Drupal has been doing for a long time shall we skip the change record?

Re-evaluated the need for a followup and one isn't needed. Naming documentation is fine.

quietone’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

There was no objection to proceeding with this at the last meeting, #3490058: Coding Standards Meeting Wednesday 2025-01-28 2100 UTC

Documentation has been updated. The changes needed to be reviewed.

quietone’s picture

Status: Reviewed & tested by the community » Fixed

@borisson_ replied in #coding-standards that they reviewed the changes.

Therefor I am closing this issue as fixed.

Thanks!

Status: Fixed » Closed (fixed)

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