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
- https://www.drupal.org/u/drunken-monkey (2017-03-01)
- https://www.drupal.org/u/wim-leers (2017-03-01)
- 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
abstractandfinaldeclarations must precede the visibility declaration.When present, the
staticdeclaration must come after the visibility declaration.Extract from PSR-12 section 4.6
Remaining tasks
Create this issue in the Coding Standards queue, using the defined templateAdd supportersCreate a Change RecordReview by the Coding Standards CommitteeCoding Standards Committee takes action as requiredDiscussed by the Core Committer Committee, if it impacts Drupal Core/li>Final review by Coding Standards Committee- Documentation updates
Edit all pages- Not needed
Publish change record Remove 'Needs documentation edits' tag
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
Comment #2
klausiI 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.
Comment #3
drunken monkey+1 for this.
I'd just change the new text a bit to clearly reflect the non-visibility modifiers are optional:
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?
Comment #4
klausiI 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.
Comment #5
Anonymous (not verified) commented+1 for this.
Committing to conforming to PSR-2 sounds like a logical progression.
Comment #6
duaelfr+1 for standards :)
Comment #7
wim leers+1
Comment #8
klausiThanks, with that agreement we can proceed.
Comment #9
bojanz commentedLate +1 :)
Comment #10
dawehner+1
Comment #11
tobiasb+1
Comment #12
borisson_Another +1 here.
Comment #13
tizzo commentedThis issue is being included in today's coding standards announcement, updating status and issue tags to follow the defined coding standards ratification process.
Comment #14
andypost-1 to "must add public" that's too common to skip it as default in language
Comment #15
borisson_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.
Comment #16
NormySan commented+1, and awesome that we can conform to the PSR-standard in this case!
Comment #17
rfulcher commented+1
Comment #18
xjmThis seems like a good standard since it puts the most important information first. It's also minimally disruptive to adopt:
Comment #19
claudiu.cristeaLet's do it.
Comment #20
drunken monkey*bump*
Comment #21
quietone commentedThis 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.
Comment #22
quietone commentedAnd update the title.
Comment #23
borisson_The new text looks great, +1 for inclusion in the coding standards like this.
Comment #24
bbralaAgree, +1 to use the psr12 text.
Comment #25
quietone commentedThis 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".
Comment #26
quietone commentedIssue moved, set to RTBC and now waiting for core committer signoff.
Comment #27
catchSeems fine to me and uncontroversial since we already do it. What's the next step?
Comment #28
alexpottAs per https://www.drupal.org/project/coding_standards
We have the text so let's do it.
Comment #29
alexpottUpdated coding standard - https://www.drupal.org/docs/develop/standards/object-oriented-code#visib...
Crediting everyone involved.
Comment #30
quietone commentedThanks 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.
Comment #31
drunken monkeyQuote from the new docs (bold added by me for emphasis):
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.)
Comment #32
borisson_I agree, that was not intended.
Comment #33
klausialexpott 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.
Comment #34
alexpottlolz someone tricked me!!!! I just copied and pasted from the issue summary! :D
Comment #35
alexpottWe also removed
Which still seems partially relevant. FWIW I'm a huge fan of readonly public properties but that's a PHP 8.1 only thing.
Comment #36
quietone commented@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?
Comment #37
quietone commentedComment #38
alexpottWell 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.
Comment #39
quietone commented@alexpott, you are correct about #4. My brain must be getting ready early for my holiday.
Adding a follow up tag for #38.
Comment #40
xjmComment #41
quietone commentedJust converting to the new template
Comment #42
quietone commentedMatch 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.
Comment #43
quietone commentedThere 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.
Comment #44
quietone commented@borisson_ replied in #coding-standards that they reviewed the changes.
Therefor I am closing this issue as fixed.
Thanks!