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.

Members fund testing for the Drupal project. Drupal Association Learn more

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
naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

  • catch committed 43af0f4 on 8.4.x
    Issue #2887319 by kiamlaluno: Avoid a useless variable initialization in...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.4.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.