Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix continue / break PHP 7.3 warnings #13066

Merged
merged 2 commits into from
Nov 22, 2018
Merged

Conversation

ejegg
Copy link
Contributor

@ejegg ejegg commented Nov 6, 2018

Overview

PHP 7.3 emits warnings on ambiguous use of 'continue' where it
serves as a 'break' statement.

In all of these cases it looked like 'break' would have also worked
since there was nothing after the end of the switch statement but
before the end of the loop. But I used continue 2 to be a bit safer
about it, in case people add more code there.

Before

PHP 7.3 emits warnings, causing tests to fail under strict conditions

After

Fewer warnings

Technical Details

If the PR introduces noteworthy technical changes, please describe them here. Provide code snippets if necessary

Comments

Note that in the contact Query there seems to be a fallthrough bug
with campaign, where a comment says things not matching the tested
cases should fall through to default, but 'website' has been added
in between.

@civibot
Copy link

civibot bot commented Nov 6, 2018

(Standard links)

@civibot civibot bot added the master label Nov 6, 2018
@eileenmcnaughton
Copy link
Contributor

@ejegg What do you think about extracting the big switch clause instead? for the first one e.g something like

      $from = $from . self::getFromClauseForField($mode, $name, $side, $primaryLocation);

where the extracted function looks like


  /**
   * @param $mode
   * @param $name
   * @param $side
   * @param $primaryLocation
   *
   * @return string
   */
  protected static function getFromClauseForField($mode, $name, $side, $primaryLocation) {
    $limitToPrimaryClause = $primaryLocation ? "AND {$name}.is_primary = 1" : '';
    switch ($name) {
      case 'civicrm_address':
        //CRM-14263 further handling of address joins further down...
        return " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id {$limitToPrimaryClause} )";

      case 'civicrm_phone':
        return " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id {$limitToPrimaryClause}) ";

@@ -1564,7 +1564,7 @@ public static function postProcess(&$groupTree, &$params, $skipFile = FALSE) {

case 'File':
if ($skipFile) {
continue;
continue 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code I think this one should be a 'break'

@@ -737,7 +737,7 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
case 'Radio':
//special case if user select -none-
if ($params["price_{$id}"] <= 0) {
continue;
continue 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably be a break too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool, I like the thought of extracting the whole thing

@ejegg
Copy link
Contributor Author

ejegg commented Nov 16, 2018

OK, updated to use 'break' in the CustomGroup and PriceSet, and to extract the function more or less like you suggested

@ejegg
Copy link
Contributor Author

ejegg commented Nov 16, 2018

Ahh, whitespace warnings from checkstyle. I'll fix those, and pull the extra line into the function

PHP 7.3 started warning on 'continue' statements inside switches,
where they act just like 'break' statements. To actually continue
the enclosing loop, it would have to be 'continue 2'. Since that's
a weird syntax and these two cases have nothing after the switch,
I've replaced them here with break.
PHP 7.3 warns if a continue statement is used inside a switch, where
it functions as a break statement. This patch extracts the switch
from the loop, and also fixes the fall-through error from
civicrm_campaign to default, moving the civicrm_website case out of
the way.
@ejegg
Copy link
Contributor Author

ejegg commented Nov 19, 2018

test this please

@eileenmcnaughton
Copy link
Contributor

I re-did the same extraction & compared any differences between my result & this result & they were all cosmetic. Tests coverage is good over this code & approach is sensible. Merging.

yay to supporting php 7.3

@eileenmcnaughton eileenmcnaughton merged commit fd88cc9 into civicrm:master Nov 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants