-
Notifications
You must be signed in to change notification settings - Fork 118
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noted changes; we need a solution that does not use error suppression.
Also, if possible, please provide either unit tests that prove the fix, or data we can use to try and reproduce, to ensure we don't break this in the future.
src/Sql/AbstractSql.php
Outdated
@@ -241,7 +241,7 @@ protected function createSqlFromSpecificationAndParameters($specifications, $par | |||
if (isset($paramSpecs[$position]['combinedby'])) { | |||
$multiParamValues = []; | |||
foreach ($paramsForPosition as $multiParamsForPosition) { | |||
$ppCount = count($multiParamsForPosition); | |||
$ppCount = @count($multiParamsForPosition); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our coding guidelines have a preference for NOT using error suppression. In this case, I'd do one of the following:
- See if
$multiParamsForPosition
is an array, and, if so, returncount($multiParamsForPosition)
- If
$multiParamsForPosition
is an object, test if it isCountable
, and, if so, return$multiParamsForPosition->count()
- Otherwise, raise an exception (invalid data is present!)
I had to patch for one of my projects; you've also missed some ocurrences. Attached the patch I've applied (zend-db 2.8.2), if that can help :) |
Calling the count function triggers warning message: "count(): Parameter must be an array or an object that implements Countable". More info at https://wiki.php.net/rfc/counting_non_countables
Raising an exception breaks backward compatibility - if beforehand we passed a string to the function everything worked fine, now, after the fix it will fail. |
What does it return in such cases? Is it usable? |
It returns 1, and starting from v7.2 it raises a warning. |
Right, and the warning is what the original patch was trying to eliminate. The question I have is: is 1 an appropriate return for this? If so, instead of raising an exception for such uncountable values, we could return 1. But if it's not, the exception is essentially fixing incorrect behavior. |
I think it's best to be stick to PHP's behavior, and it tells us kindly to use "count" in a proper way (with notice or warning), but it doesn't break the functionality of existing applications. It would be very wrong and unpleasent situation when someone will upgrade his ZF minor version, and discover that something is broken. I assume that in further PHP versions, this issue will become an error, than we should fix it in ZF too. But now it gives some time to breathe, reconsider "count" usage and fix the running apps. So - yes, we should return 1 for "string" var (for other types we should check "count" behavior) |
Exception is a proper way to be kindly notified that something needs fixing. Framework throwing new exception is something I can fix. Framework raising a notice or warning is not something I can deal with. If I do not want to take a hit at engine level for having notice rasied, AND cannot realistically deal with production logging now filled with warnings and CI build system that inspects code for engine message break the build, framework raising messages is something i cannot fix. |
|
Sorry, can you please point at the declaration of method you refer to? I can't find it in the changelist. |
The one I meant is related to commit eb6cb76, in which the "string" word was removed from the phpdoc. There is a "count" applied on that parameter too. |
I've just get an issue using something like I do not know enough the framework to be sure we really want a string in that case; but since we got one; calling an exception is probably not the way to go. |
} else { | ||
throw new Exception\UnexpectedValueException(sprintf( | ||
'Parameters must be an array or a countable object, %s given', | ||
gettype($multiParamsForPosition) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should not raise an exception, as this may break some other code, for the string case
At least in zend-paginator test suite
1) ZendTest\Paginator\Adapter\DbTableGatewayTest::testGetItemsWithWhereAndOrderAndGroup
count(): Parameter must be an array or an object that implements Countable
/usr/share/php/Zend/Db/Sql/AbstractSql.php:244
/usr/share/php/Zend/Db/Sql/AbstractSql.php:72
/usr/share/php/Zend/Db/Sql/AbstractPreparableSql.php:34
/usr/share/php/Zend/Db/Sql/Platform/Platform.php:100
/usr/share/php/Zend/Db/Sql/Sql.php:130
/builddir/build/BUILDROOT/php-zendframework-zend-paginator-2.7.0-3.fc28.noarch/usr/share/php/Zend/Paginator/Adapter/DbSelect.php:100
/builddir/build/BUILD/zend-paginator-42211f3e1e8230953c641e91fec5aa9fe964eb95/test/Adapter/DbTableGatewayTest.php:121
2) ZendTest\Paginator\Adapter\DbTableGatewayTest::testGetItemsWithWhereAndOrderAndGroupAndHaving
count(): Parameter must be an array or an object that implements Countable
/usr/share/php/Zend/Db/Sql/AbstractSql.php:244
/usr/share/php/Zend/Db/Sql/AbstractSql.php:72
/usr/share/php/Zend/Db/Sql/AbstractPreparableSql.php:34
/usr/share/php/Zend/Db/Sql/Platform/Platform.php:100
/usr/share/php/Zend/Db/Sql/Sql.php:130
/builddir/build/BUILDROOT/php-zendframework-zend-paginator-2.7.0-3.fc28.noarch/usr/share/php/Zend/Paginator/Adapter/DbSelect.php:100
/builddir/build/BUILD/zend-paginator-42211f3e1e8230953c641e91fec5aa9fe964eb95/test/Adapter/DbTableGatewayTest.php:143
Fixed in #276 |
No description provided.