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.

Comments

kiamlaluno created an issue. See original summary.

kiamlaluno’s picture

Issue summary: View changes
cilefen’s picture

Category: Bug report » Task
Issue tags: +Novice
cilefen’s picture

Component: other » user.module
kiamlaluno’s picture

Status: Active » Needs review
FileSize
604 bytes
kiamlaluno’s picture

Assigned: kiamlaluno » Unassigned