Problem/Motivation
(#)
SearchBuilder::flattenKeys throws Array offset Notice in PHP 7.4

When flattenKeys method is invoked this code
$keys = array_filter($keys, function ($key) {
return $key[0] !== '#';
}, ARRAY_FILTER_USE_KEY);generates the error shown.
Example:
Let's says $keys array looks like :
Array
(
[0] => jstestsearch
[#conjunction] => AND
)key: 0 is considered as int and when we try to access $key[0] we are trying to access value considering it a string but it's recognised as an int hence the error.
Proposed resolution
(#)
Changing it to
$keys = array_filter($keys, function (string $key) {
return $key[0] !== '#';
}, ARRAY_FILTER_USE_KEY);notice the argument hint:string in the function, this seems to work fine and fixes the error.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | array-offset-notice-php7.4-3125891-2.patch | 561 bytes | u_tiwari |
| Screenshot 2020-04-08 at 11.41.15 AM.png | 66.41 KB | u_tiwari |
Comments
Comment #2
u_tiwari commentedAttaching a patch for the same.
Comment #3
baysaa commented+1 #2. Simple logical fix.
This is from the the PHP 7.4 migration doc: Trying to use values of type null, bool, int, float or resource as an array (such as $null["key"]) will now generate a notice.
Comment #4
nils.destoop commented+1 Fixes the notice for me
Comment #5
dealancer commentedThis patch works perfectly fine, let's commit it!
Comment #7
skek commented