API page: https://api.drupal.org/api/drupal/core%21modules%21user%21src%21UserData...
UserData::set() uses the following code.
public function set($module, $uid, $name, $value) {
$serialized = 0;
if (!is_scalar($value)) {
$value = serialize($value);
$serialized = 1;
}
$this->connection->merge('users_data')
->keys([
'uid' => $uid,
'module' => $module,
'name' => $name,
])
->fields([
'value' => $value,
'serialized' => $serialized,
])
->execute();
}
It is setting $serialized twice, when its value depend from the value returned from a PHP function. The following code does the same without setting twice that variable.
public function set($module, $uid, $name, $value) {
$serialized = (int) !is_scalar($value);
if ($serialized) {
$value = serialize($value);
}
$this->connection->merge('users_data')
->keys([
'uid' => $uid,
'module' => $module,
'name' => $name,
])
->fields([
'value' => $value,
'serialized' => $serialized,
])
->execute();
}
I casted the value to an integer, since the original code is using 0 and 1. It could be removed, if the code still works also with boolean values.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | remove-initialization-2887319-3.patch | 604 bytes | avpaderno |
Comments
Comment #2
avpadernoComment #3
cilefen commentedComment #4
cilefen commentedComment #5
avpadernoComment #6
avpadernoComment #7
naveenvalechaThanks!
Comment #9
catchCommitted/pushed to 8.4.x, thanks!