After upgrading to PHP 7.2, we noticed, with all versions of webform, ending with the latest dev version, that we now receive a PHP warning for every field value of a submitted webform.

The exact error is (repeated for every field):

Warning: count(): Parameter must be an array or an object that implements Countable in _webform_client_form_validate() (line 3105 of /oursitepath/sites/all/modules/contrib/webform/webform.module).

When reviewing http://php.net/manual/en/migration72.incompatible.php it is seen that you can no longer use count() in the scenario it's being used on line 3105.

The notes in that section state:

// Make sure a value is passed when the field is required.
// A simple call to empty() will not cut it here as some fields, like
// checkboxes, can return a valid value of '0'. Instead, check the
// length if it's a string, and the item count if it's an array. For
// radios, FALSE means that no value was submitted, so check that too.

So, it seems that this count() was being used because nothing else would work. What should we do here instead?

Thanks!

Comments

CrashTest_ created an issue. See original summary.

crashtest_’s picture

Version: 7.x-4.x-dev » 7.x-4.16
Status: Active » Needs review
StatusFileSize
new1.04 KB

Well, my idea for this one is to add an is_array() in the if statement before the count().

Patch provided.

Status: Needs review » Needs work

The last submitted patch, 2: php72-count-2953662.patch, failed testing. View results

liam morland’s picture

Title: PHP 7.2 count() issue. » Only use count() on countables
Version: 7.x-4.16 » 7.x-4.x-dev

Thanks for the patch. Please re-roll in a webform-only repository.

If would be helpful to check for other examples of this problem in webform.

liam morland’s picture

Issue tags: -PHP7.2 +PHP 7.2
crashtest_’s picture

StatusFileSize
new1.02 KB

Here is an updated patch against the git repo on branch 7.x-4.x.

liam morland’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: webform-php72-count-2953662.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

liam morland’s picture

When the variable is not countable, count() returns 0 if it is NULL and 1 otherwise. Adding is_array() does not cause this code to do the same thing.

The patch also adds an extra empty line which it should not.

crashtest_’s picture

Hrm, well, could you suggest a method that would work? The method I proceeded with removes the warnings, but I am sure may not work as you expect. What method do you think might prevent this warning?

liam morland’s picture

$elements['#value'] is sometimes an array and sometimes not. That conditional just needs re-think so that it does the same thing as it does now, but without ever calling count() when it's not an array.

g089h515r806’s picture

StatusFileSize
new955 bytes

here is a new patch.

g089h515r806’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 12: webform-php72-count-2953662-12.patch, failed testing. View results

g089h515r806’s picture

Status: Needs work » Needs review
StatusFileSize
new986 bytes

add case '0'

ac’s picture

#15 works on 7.x-3.x (manually patched) and 7.x-4.x.

ac’s picture

Status: Needs review » Reviewed & tested by the community
liam morland’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.07 KB

Here is another way to do this which I think is more clear. Please check that the code does the same thing as before.

sgourebi’s picture

Patch webform-php72_count-2953662-18.patch work for me on D7.59 and php 7.2

pipicom’s picture

Also for me the patch webform-php72_count-2953662-18.patch works successfully on D7.59 and php 7.2.

  • Liam Morland committed 69fa1ad on 7.x-4.x
    Issue #2953662 by CrashTest_, g089h515r806, Liam Morland: Only use count...
liam morland’s picture

Status: Needs review » Fixed

Thanks everyone!

Status: Fixed » Closed (fixed)

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