In #652650: Change 'elseif' standard to 'else if' we briefly discussed changing the Drupal coding standards from "elseif" to "else if", but quickly realized the standard is the best choice. However, there is some code in both D8 and D7 that uses "else if", so we need to enforce this. Simple patch, just replace every occurrence of "else if" to "elseif".

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

klausi’s picture

Status: Active » Needs review
Issue tags: -Needs backport to D7
FileSize
3.23 KB

Here is a simple find and replace command to track down all occurences of "else if" excluding Javascript files (execute in a Unix-style environment in your Drupal 8 root folder):
find . -path './.git' -prune -o -not \( -name '*.js' \) -type f -exec sed -i -e "s/else if/elseif/g" {} \;
This also changes core/lib/Drupal/Component/Archiver/ArchiveTar.php, which is a third party library. You can revert that change with
git checkout core/lib/Drupal/Component/Archiver/ArchiveTar.php
Patch attached.

And I think it is not necessary to backport this minor change to Drupal 7, so removing tag.

klausi’s picture

Issue tags: +find and replace

Adding "find and replace" tag, so that I remember where I put those useful find&sed commands. It can be quite tedious to get them actually right.

Niklas Fiekas’s picture

Status: Needs review » Reviewed & tested by the community

Looks pretty straight forward.

jhodgdon’s picture

Wow, really, only 6 places in all of Core? So why were we thinking of going in the other direction? :)

Niklas Fiekas’s picture

Yep, that seams to be all ;)

(Except tons of javascript that needs to stay else if and third party PHP code.)

droplet’s picture

Niklas Fiekas’s picture

Wow, excellent!

jhodgdon’s picture

RE #6 -- um, why is that relevant here?

droplet’s picture

@jhodgdon

the 3rd party script contains "else if" (comment #5)

cosmicdreams’s picture

The new ArchiveTar.php in /core/lib/Drupal/Component/Archiver adds 20 more instances of this issue. Preparing a patch.

cosmicdreams’s picture

FileSize
9.71 KB

Here's a patch that includes those changes. In reading previous comments it seems that these changes were intended to be included but were somehow.

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs review

We should not be doing anything to third-party scripts at all?

cosmicdreams’s picture

Status: Needs review » Reviewed & tested by the community

Also, isn't it a standard to scope elseif's no matter what?

for example:

if ($this->_compress_type == 'gz')
  @gzclose($this->_file);
elseif ($this->_compress_type == 'bz2')
  @bzclose($this->_file);
elseif ($this->_compress_type == 'none')
  @fclose($this->_file);
else
  $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');

Should the previous code be this instead?

if ($this->_compress_type == 'gz') {
  @gzclose($this->_file);
}
elseif ($this->_compress_type == 'bz2') {
  @bzclose($this->_file);
}
elseif ($this->_compress_type == 'none') {
  @fclose($this->_file);
}
else {
  $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
}
klausi’s picture

Do not try to "fix" external third party files, because this will be overwritten anytime the file is updated from upstream.

Reuploading patch from #1.

Niklas Fiekas’s picture

RTBC seconded. #14 is exactly what we need.

webchick’s picture

Assigned: Unassigned » jhodgdon

Tossing to Jennifer since it's coding standards related.

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs work

The patch seems to be fine, although I haven't verified that it catches all the spots...

But I also don't see anywhere on http://drupal.org/coding-standards where it says we should be using elseif as opposed to else if. So I think we need to draft some wording, probably for the section "control structures", before we do this patch?

webchick’s picture

Hm. Is that not already documented by virtue of the code sample directly beneath the "Coding standards" heading? I'm not sure we need to spell it out more than that, do we?

jhodgdon’s picture

I think it should be spelled out in a note below, if it is a standard, especially given that PHP language supports both.

jhodgdon’s picture

If everyone already agrees that it's a de-facto standard (as evidenced by most of core following it), then I can put a note there. Does it need to be in Coder too?

Niklas Fiekas’s picture

Coder already has Use "elseif" in place of "else if".

franz’s picture

+1 for the note.

jhodgdon’s picture

Status: Needs work » Reviewed & tested by the community

I added a brief note to the coding standards. I'll get the patch committed after I verify independently that it gets all the else if spots (except in 3rd-party libraries that we aren't modifying).

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs work

Patch above needs a reroll -- someone has already removed the else if lines from locale.admin.inc.

I have also just verified (I think) that the only places outside of Archiver and .js files that have elseif are:

core/modules/field/modules/options/options.module:211:        else if (!$has_value) {
core/modules/taxonomy/taxonomy.module:1331:        else if ($item['tid'] == 'autocreate') {
core/includes/utility.inc:49:  else if (is_object($var) && get_class($var) === 'stdClass') {

using

grep -R -n "else if" * | grep -v Archiver | grep -v ".js:"
jhodgdon’s picture

Assigned: jhodgdon » Unassigned

Unassigning in hopes that someone will reroll this patch.

cosmicdreams’s picture

FileSize
1.85 KB

Here's a the reroll

cosmicdreams’s picture

Status: Needs work » Needs review
Niklas Fiekas’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, cosmicdreams. Assuming tests pass.

jhodgdon’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed to 8.x, thanks!

Patch needs reroll for 7.x, where the following command

grep -R -n "else if" * | grep -v system.tar.inc | grep -v ".js:"

says there are only two places to patch:

modules/field/modules/options/options.module:211:        else if (!$has_value) {
modules/taxonomy/taxonomy.module:1579:        else if ($item['tid'] == 'autocreate') {

Note that system.tar.inc in d7 I think is the same as Archiver.inc in D8 -- it's a 3rd-party file so we leave it alone.

tim.plunkett’s picture

Status: Patch (to be ported) » Needs review
FileSize
1.22 KB

Okay.

Niklas Fiekas’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x. Thanks!

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

Anonymous’s picture

Issue summary: View changes

typo