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

PHP 7.2: count(): Parameter must be an array or an object that implements Countable #4167

Closed
cebe opened this issue Jul 19, 2017 · 26 comments
Closed

Comments

@cebe
Copy link
Member

cebe commented Jul 19, 2017

What steps will reproduce the problem?

A lot of tests on PHP 7.2 are failing with

count(): Parameter must be an array or an object that implements Countable

count() throws a warning since PHP 7.2 in case a scalar or non-countable object is passed. We rely on this behavior in many places so code needs to be adjusted.

Need to check how many places are affected and whether its easy to fix. If its hard to fix in a BC way, PHP 7.1 may be the last version that Yii 1.1 runs on.

More info

@cebe cebe added this to the 1.1.20 milestone Jul 19, 2017
@snowleopard24
Copy link

@cebe Have you opened an issue against this on https://bugs.php.net/ ?
I'm getting the same error as well

@cebe
Copy link
Member Author

cebe commented Sep 1, 2017

this is not a bug, but a feature ;) PHP changed count() behavior in the following RFC: https://wiki.php.net/rfc/counting_non_countables

@pajon
Copy link

pajon commented Dec 5, 2017

How it looks like with upgrade to PHP 7.2 ?

@samdark
Copy link
Member

samdark commented Dec 5, 2017

Not a good idea for now.

@b1rdex
Copy link
Contributor

b1rdex commented Dec 26, 2017

Here are 7.2 compatibility fixes:
b1rdex@713a80e
b1rdex@8a8ac14
b1rdex@972c3b6

@pajon
Copy link

pajon commented Dec 26, 2017

Hello, there are some issues.

  • Function session_status is supported from 5.4, so there is backwards incompatibility.(must be compatible from 5.2)

  • Function count return 0 if null parameter is used before 7.2 and your patches don't fix this case.

There is maybe better solution to create new count (_count for example) function which have behaviour like before 7.2.

@b1rdex
Copy link
Contributor

b1rdex commented Dec 26, 2017

@pajon I have no need to support 5.4, so you are free to add function_exists check.
As per count(null) — well, that sucks. Glad that 7.2 removed this. I don't see possibility for null to be presented in code I've provided fixes for (based on string|array docblock on primaryKey field).

@manumonviru
Copy link

Hi Is there any option slove this pblm
 message: sizeof(): parameter must be an array or an object that implements countable
Php version :- 7.2
Laravel version  5.4.30

Please help me with the details 

@pajon
Copy link

pajon commented Jan 4, 2018

@manumonviru sizeof is just alias to count function.

@rajuginne
Copy link

i have same issue with wordpress

@samdark samdark reopened this Jan 25, 2018
@samdark
Copy link
Member

samdark commented Jan 25, 2018

Partly fixed.

@sokolnikov911
Copy link

sokolnikov911 commented Jan 29, 2018

When can you make release 1.1.20 with this fix?

@samdark samdark closed this as completed Jan 30, 2018
@samdark
Copy link
Member

samdark commented Jan 30, 2018

Hope having it soon. We have 7.2 tests now and these are passing.

@AbuMuhammad
Copy link

There is another one on framework/gii/generators/model/ModelCode.php:371
Fixed it with

...
$pk=$table->primaryKey;
$count=is_array($pk) ? count($pk) : 1;
return ($count === 2 // we want 2 columns
...

samdark added a commit that referenced this issue Feb 3, 2018
@samdark
Copy link
Member

samdark commented Feb 3, 2018

9e5e427

@seregagl
Copy link
Contributor

There is another one on framework/web/CController.php:928

public function renderDynamic($callback)
{
        $n=count($this->_dynamicOutput);
        echo "<###dynamic-$n###>";
...

@samdark
Copy link
Member

samdark commented Apr 16, 2018

@seregagl would you like to fix it and do a pull request? If not, please create a separate issue. Will be lost here in the closed one...

@aztechowski
Copy link
Contributor

There is another issue in CActiveFinder in queryOneMany
in line if(count($fks)!==count($pkTable->primaryKey))
If primaryKey is string the count() doesn't work.
It can be easy fixed with:
if(count($fks)!==count(array($pkTable->primaryKey)))

@samdark
Copy link
Member

samdark commented May 11, 2018

Would you like to send a pull request fixing it?

@aztechowski
Copy link
Contributor

I've checked trunk version and it looks it is already fixed with 05d2c67. The latest version 1.1.19 is not.
What are plans regarding 7.2 compatibility? When the next release that is 7.2 compatible will be released?

@samdark
Copy link
Member

samdark commented May 14, 2018

When we'll fix the rest of https://github.com/yiisoft/yii/issues

@jjdunn
Copy link

jjdunn commented Oct 26, 2018

Please forgive me if this is not the correct issue on which to report problems, but there's at least two places in framework/db/schema/CDbCriteria that got missed:

	public function addInCondition($column,$values,$operator='AND')
	{
                // JJD 10/25/18 patch Yii 1.1.x framework for PHP 7.2
		//if(($n=count($values))<1)
                if (empty($values))
			$condition='0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE
		//elseif($n===1)
                elseif(!is_array($values) || 1===count($values))

similarly:

	public function addNotInCondition($column,$values,$operator='AND')
	{
                // JJD 10/25/18 patch framework for PHP 7.2
		//if(($n=count($values))<1)
                if (empty($values))
			return $this;
		//if($n===1)
                if(!is_array($values) || 1===count($values))
              ....

@rob006
Copy link
Contributor

rob006 commented Oct 26, 2018

$values should be array according to PHPDoc - if you get this error it means that you're calling these methods in a wrong way.

@jjdunn
Copy link

jjdunn commented Oct 26, 2018

$values should be array according to PHPDoc - if you get this error it means that you're calling these methods in a wrong way.

our application has been using Yii since 2009 and it's worked fine - the methods mentioned in the previous comment have been quite robust at handling various arguments. Sometimes it gets called with empty value, or single (non-array) value; or array with 1 element, or array with multiple elements. The first three cases break now in PHP 7.2

The patches in my previous comment are to make the framework more robust and consistent with previous behavior, so the application code does not have to change.

Note that we're running our Dev/Test environment with error_reporting = E_ALL, so the warnings emitted by 7.2 are actually reported as errors. I like this so we can find/fix them now...

@cebe
Copy link
Member Author

cebe commented Oct 26, 2018

If you pass a scalar value as $values the count() will return 1, which gets you into the elseif($n===1) branch. There you have $value = reset($values), which would fail because reset() expects an array.

So I do not see how your code runs fine in other PHP versions < 7.2: https://3v4l.org/nHrpA

@jjdunn
Copy link

jjdunn commented Oct 26, 2018

If you pass a scalar value as $values the count() will return 1, which gets you into the elseif($n===1) branch. There you have $value = reset($values), which would fail because reset() expects an array.

So I do not see how your code runs fine in other PHP versions < 7.2: https://3v4l.org/nHrpA

Thank you @cebe and @rob006 for this feedback.

I have taken this opportunity to audit all our application code that uses CDbCriteria::addInCondition() and ::addNotInCondition(). All instances except one pass an array as the $value parameter. The one case where a non-array is (sometimes) passed, it's always empty. I will fix the application code in this one case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests