-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Stop warnings when using count in QueryCompiler in PHP 7.2 #10864
Conversation
- PHP 7.2 has changed the count function to emit warnings when the object being "counted" is not an array or does not implement the Countable interface. The QueryCompiler was using count for objects that did not fall into those two categories. This fixes that problem while maintaining the same logic (returning early when $parts is not set, is null, or has a count of 0).
@@ -125,7 +125,9 @@ public function compile(Query $query, ValueBinder $generator) | |||
protected function _sqlCompiler(&$sql, $query, $generator) | |||
{ | |||
return function ($parts, $name) use (&$sql, $query, $generator) { | |||
if (!count($parts)) { | |||
if (!isset($parts) || |
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.
Wouldn't just checking if (empty($parts))
work?
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.
Not for the full set of conditionals (if that was the question?).
Objects with Countable interfaces and count() == 0 are not considered "empty" by the empty function.
Also I am mimicing the old conditional's (possible) reliance on count returning 1 when an object was not an array or Countable. In other words I know that this new conditional acts exactly the same as !count($parts)
but without the warning emitted.
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.
As a follow on, $parts can be an integer (I've dumped out the type of parts so this is not a hypothetical, sometimes an integer is passed in). So the following case shows why empty would be a change from current behavior:
$parts = 0;
!count($parts) // == false
empty($parts) // == true
(edit - fixed the comment in my little example above - but the end result is still the same !count($parts) != empty($parts)
in some cases)
Codecov Report
@@ Coverage Diff @@
## master #10864 +/- ##
============================================
+ Coverage 94.91% 94.92% +<.01%
- Complexity 12123 12126 +3
============================================
Files 422 422
Lines 30142 30143 +1
============================================
+ Hits 28610 28613 +3
+ Misses 1532 1530 -2
Continue to review full report at Codecov.
|
being "counted" is not an array or does not implement the Countable
interface. The QueryCompiler was using count for objects that did not
fall into those two categories. This fixes that problem while
maintaining the same logic (returning early when $parts is not set, is
null, or has a count of 0).
Fixes #10862