Closed (fixed)
Project:
Coding Standards
Component:
Coding Standards
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
19 Mar 2016 at 12:19 UTC
Updated:
7 Dec 2016 at 17:08 UTC
Jump to comment: Most recent
There are no documented standards for how to handle when an object method is wrapped onto a second line.
e.g.:
$query = db_select('node');
$query->condition('type', 'article')
->condition('status', 1)
->execute();
vs
$query = db_select('node');
$query->condition('type', 'article')
->condition('status', 1)
->execute();
vs
$query = db_select('node');
$query->condition('type', 'article')
->condition('status', 1)
->execute();
The first example appears to be the convention, but it is not standardized.
Once an agreement is reached we'll need to update the Coder rules (#2690325: Indentation incorrect on wrapped method calls). Done, as per comment #20.
Suggested text:
Chained Function Calls
In case you have a fluid interface for a class, and the code spans more than one line, the method calls should be indented with 2 spaces:
$query = db_select('node') ->condition('type', 'article') ->condition('status', 1) ->execute();
Comments
Comment #2
perignon commented+1. I have always used the first example of two spaces.
Comment #3
klausiSome people rather like
I'm not sure we should make up rules for this and just let people use whatever they want?
Although Drupal core mostly uses 2 spaces indentation I believe, so it seems to be sort of a convention already.
Comment #4
damienmckenna@klausi: The same could be said of *all* Drupal's coding standards ;-)
So would anyone else agree to making the first example the standard?
Comment #5
jhodgdon+1 for 2 spaces, as it's consistent with our other indentation standards.
Comment #6
drunken monkey+1 for 2 spaces. It's also what is used in the DB layer documentation – probably the foremost example of such chained calls.
Comment #7
damienmckennaComment #8
dawehnerI agree with the 2 spaces idea, its simply consistent.
Here is a suggested text:
Comment #9
NerOcrO commented+1 for 2 spaces only!
Comment #10
tizzo commentedComment #11
alexpott+1 for 2 spaces.
Comment #12
Crell commented"Me too!" (2 space indent.)
The 3rd variant has some logic to it, but when the first line is not a single variable the alignment point could get very far intended. 2-spaces is nice and consistent and readable.
Comment #13
tizzo commentedThe TWG has reviewed and is ready to approve this issue. Passing to the core queue for core committer sign-off. If this looks good to core please tag it "Core Committer Approved" and pass back to the TWG queue for documentation updates as per the documented process.
Comment #14
klausiSetting to RTBC to make it visible to core committers.
Comment #15
xjmThis rule could be tested programmatically; is there coverage for it in coder?
Comment #16
klausiNo, there is no coverage in Coder yet. Please open an issue in the Coder queue.
Comment #18
klausiAh sorry, of course the Coder issue exists: #2690325: Indentation incorrect on wrapped method calls
Comment #19
xjmThanks @klausi! Postponing on that issue.
Comment #20
klausiThe Coder rule is implemented now.
The next step is to come up with a wording of this rule in the issue summary and where in the coding standards document we are going to put it.
I think no core committer has signed off explicitly yet, although xjm probably did that by asking for the Coder rule? @xjm: can you confirm and add the "Core Committer Approved" tag?
Comment #21
pfrenssen@dawehner has a proposal for the wording in #8.
Comment #22
dawehnerComment #23
pfrenssenComment #24
pfrenssenMarking this RTBC for core committer approval. We can still hash out the details of the wording while awaiting approval.
Comment #25
catchThis was +1'd by alexpott, xjm's points have been done, I'm also +1, so tagging and sending back to TWG.
I'd use 'chained method calls' rather than 'nested function calls' in the docs. Nested function calls I think of as array_keys(array_intersect_key()).
Comment #26
klausiCoding Standards is the correct queue now.
Comment #27
jthorson commentedUpdating issue summary to reflect suggestion to use 'chained' instead of 'nested', and clarifying that this applies specifically in the multi-line case.
Comment #28
jthorson commentedRatified.
Documentation added to the 'Chaining' section on the Object Oriented code page of the coding standards.
Comment #30
tim.plunkettExcept it's "fluent", not "fluid".
Should I just go change the docs page directly?
Comment #31
klausiSure, typo fixes are always allowed :)