Page MenuHomePhabricator

Position of boolean operators inside an if condition
Open, Needs TriagePublic

Description

While reading the code of SpaceyParanthesisSniff.php in CodeSniffer - I found the following snippet at Line 76 :

		if ( ( $nextToken['code'] === T_WHITESPACE &&
				strpos( $nextToken['content'], "\n" ) === false
				&& $nextToken['content'] != ' ' )
			|| ( $nextToken['code'] !== T_CLOSE_PARENTHESIS && $nextToken['code'] !== T_WHITESPACE ) ) {

I was wondering if there is a style guide for where the boolean operator (&&) should be kept - in the beginning of the next line or the end of the current line.

If there is a rule (couldn't find one in https://www.mediawiki.org/wiki/CC/PHP) - maybe there should be a sniff for this ?

Could also be extended for all operators - for example a long string - should the concatenation (dot .) operator be at the end of the line or at the beginning of the new line

$a = "a very long string which spans " .
    "multiple lines.";

OR

$a = "a very long string which spans "
    . "multiple lines.";

Event Timeline

TasneemLo raised the priority of this task from to Needs Triage.
TasneemLo updated the task description. (Show Details)
TasneemLo added a subscriber: TasneemLo.
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptOct 26 2015, 4:08 AM
TasneemLo set Security to None.
TasneemLo updated the task description. (Show Details)Oct 27 2015, 7:15 AM

I think you're looking for https://www.mediawiki.org/wiki/Manual:Coding_conventions#Line_continuation ? The operator should be at the beginning of the next line.

Restricted Application added a subscriber: StudiesWorld. · View Herald TranscriptDec 20 2015, 9:15 AM
Aashaka claimed this task.Mar 25 2016, 11:06 AM

Change 279615 had a related patch set uploaded (by Aashaka):
Add sniff to check if boolean operator on same line inside 'if condition'

https://gerrit.wikimedia.org/r/279615

@Legoktm : Please review :)

@Aashaka: No need to ping about patch review in Phabricator two minutes after adding a patch. Thanks! :)

I understand. At that time, I had forgotten that Reviewer-bot asked reviewers to do patch review too.

https://www.mediawiki.org/wiki/Manual:Coding_conventions#Line_continuation has changed. It now suggests using operator at the end of the previous line. I have changed my patch accordingly!

Restricted Application added a subscriber: TerraCodes. · View Herald TranscriptApr 19 2016, 1:28 PM

Any updates/reviews on this?

I think you're looking for https://www.mediawiki.org/wiki/Manual:Coding_conventions#Line_continuation ? The operator should be at the beginning of the next line.

I agree that that is better for readability. Although it seems the page has been changed since to instead prefer the operator to be at the end of the previous line..